943,734 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1453
  • Java RSS
Jul 1st, 2008
0

Need help with Servlets

Expand Post »
I'm trying to step-up a simple servlet that processes a get action from a form.

When I click on the submit button I get the message.

Quote ...
HTTP Status 404 - /jhtp6/welcome1

type Status report

message /jhtp6/welcome1

description The requested resource (/jhtp6/welcome1) is not available.

Apache Tomcat/5.0.25
I also included an image of my file structure.

Here's the java code

java Syntax (Toggle Plain Text)
  1. 3 import javax.servlet.ServletException;
  2. 4 import javax.servlet.http.HttpServlet;
  3. 5 import javax.servlet.http.HttpServletRequest;
  4. 6 import javax.servlet.http.HttpServletResponse;
  5. 7 import java.io.IOException;
  6. 8 import java.io.PrintWriter;
  7. 9
  8. 10 public class WelcomeServlet extends HttpServlet
  9. 11 {
  10. 12 // process "get" requests from clients
  11. 13 protected void doGet( HttpServletRequest request,
  12. 14 HttpServletResponse response )
  13. 15 throws ServletException, IOException
  14. 16 {
  15. 17 response.setContentType( "text/html" );
  16. 18 PrintWriter out = response.getWriter();
  17. 19
  18. 20 // send XHTML page to client
  19. 21
  20. 22 // start XHTML document
  21. 23 out.println( "<?xml version = \"1.0\"?>" );
  22. 24
  23. 25 out.printf( "%s%s%s" , "<!DOCTYPE html PUBLIC",
  24. 26 " \"-//W3C//DTD XHTML 1.0 Strict//EN\"",
  25. 27 " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n" );
  26. 28
  27. 29 out.println( "<html xmlns = \"http://www.w3.org/1999/xhtml\">" );
  28. 30
  29. 31 // head section of document
  30. 32 out.println( "<head>" );
  31. 33 out.println( "<title>A Simple Servlet Example</title>" );
  32. 34 out.println( "</head>" );
  33. 35
  34. 36 // body section of document
  35. 37 out.println( "<body>" );
  36. 38 out.println( "<h1>Welcome to Servlets!</h1>" );
  37. 39 out.println( "</body>" );
  38. 40
  39. 41 // end XHTML document
  40. 42 out.println( "</html>" );
  41. 43 out.close(); // close stream to complete the page
  42. 44 } // end method doGet
  43. 45 } // end class WelcomeServlet

Here's the xml Code

xml Syntax (Toggle Plain Text)
  1. <web-app xmlns= "http://java.sun.com/xml/ns/j2ee"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  4. http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
  5. version="2.4">
  6.  
  7. <!-- General description of your Web application -->
  8. <display-name>
  9. Java How to Program JSP
  10. and Servlet Chapter Examples
  11. </display-name>
  12.  
  13. <description>
  14. This is the Web application in which we
  15. demonstrate our JSP and Servlet examples.
  16. </description>
  17.  
  18. <!-- Servlet definitions -->
  19. <servlet>
  20. <servlet-name>welcome1</servlet-name>
  21.  
  22. <description>
  23. A simple servlet that handles an HTTP get request.
  24. </description>
  25.  
  26. <servlet-class>
  27. WelcomeServlet
  28. </servlet-class>
  29. </servlet>
  30.  
  31. <!-- Servlet mappings -->
  32. <servlet-mapping>
  33. <servlet-name>welcome1</servlet-name>
  34. <url-pattern>/welcome1</url-pattern>
  35. </servlet-mapping>
  36.  
  37. </web-app>

and here's the html document code

html Syntax (Toggle Plain Text)
  1. <?xml version = "1.0"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  4.  
  5. <!-- Fig. 26.7: WelcomeServlet.html -->
  6.  
  7. <html xmlns = "http://www.w3.org/1999/xhtml">
  8. <head>
  9. <title>Handling an HTTP Get Request</title>
  10. </head>
  11.  
  12. <body>
  13. <form action = "/jhtp6/welcome1" method = "get" >
  14. <p><label>Click the button to invoke the servlet
  15. <input type = "submit" value = "Get HTML Document" />
  16. </label></p>
  17. </form>
  18. </body>
  19. </html>

Why do I get this message instead of the servlet response and how do I fix it.
Attached Thumbnails
Click image for larger version

Name:	structure.JPG
Views:	24
Size:	87.7 KB
ID:	6478  
Similar Threads
Reputation Points: 11
Solved Threads: 1
Junior Poster in Training
knight fyre is offline Offline
88 posts
since Feb 2008
Jul 1st, 2008
0

Re: Need help with Servlets

Your servlet's .class file should be in WEB-INF/classes/ is that the case ?

Also once you move your servlets .class file there, you will need to either restart tomcat entirely or you will need to reload you application, by accessing the tomcat manager at http://localhost:8080/manager/html, for the URL I am assuming your tomcat is running on the same machine as you are working on and is configured to listen on port 8080 for requests
Last edited by stephen84s; Jul 1st, 2008 at 4:06 am.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007
Jul 1st, 2008
0

Re: Need help with Servlets

hi,
error no: File Not Found Error.

So, please check the file name and the class name are same, to check correct path to save the file.
save the correct location and try again.....
Reputation Points: 10
Solved Threads: 0
Newbie Poster
paramasivam.b is offline Offline
1 posts
since Jul 2008
Jul 3rd, 2008
0

Re: Need help with Servlets

html Syntax (Toggle Plain Text)
  1. <?xml version = "1.0"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  4.  
  5. <!-- Fig. 26.7: WelcomeServlet.html -->
  6.  
  7. <html xmlns = "http://www.w3.org/1999/xhtml">
  8. <head>
  9. <title>Handling an HTTP Get Request</title>
  10. </head>
  11.  
  12. <body>
  13. <form action = "welcome1" method = "get" >
  14. <p><label>Click the button to invoke the servlet
  15. <input type = "submit" value = "Get HTML Document" />
  16. </label></p>
  17. </form>
  18. </body>
  19. </html>

Change the html given by u and put the above. The <form action="/jhtp6/welcome1" method="get"> has been changed to <form action="welcome1" method="get">

Hope it works
Reputation Points: 13
Solved Threads: 1
Newbie Poster
sanaulla123 is offline Offline
16 posts
since Jul 2008
Aug 29th, 2008
0

Re: Need help with Servlets

Salamo Alaikum Knight

I had the same error as yours; mine was solved that way

instead of <form action = "/jhtp6/welcome1" method = "get" >
it has to be <form method = "get" action = welcome1 >

Hope it works
Please give me feed back
Last edited by mariamAbed; Aug 29th, 2008 at 1:31 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mariamAbed is offline Offline
1 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Need Help
Next Thread in Java Forum Timeline: Read a text file from a website





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC