Need help with Servlets

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2008
Posts: 88
Reputation: knight fyre is an unknown quantity at this point 
Solved Threads: 1
knight fyre's Avatar
knight fyre knight fyre is offline Offline
Junior Poster in Training

Need help with Servlets

 
0
  #1
Jul 1st, 2008
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.

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

  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

  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

  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
structure.JPG  
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: Need help with Servlets

 
0
  #2
Jul 1st, 2008
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.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1
Reputation: paramasivam.b is an unknown quantity at this point 
Solved Threads: 0
paramasivam.b's Avatar
paramasivam.b paramasivam.b is offline Offline
Newbie Poster

Re: Need help with Servlets

 
0
  #3
Jul 1st, 2008
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.....
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 16
Reputation: sanaulla123 is an unknown quantity at this point 
Solved Threads: 0
sanaulla123 sanaulla123 is offline Offline
Newbie Poster

Re: Need help with Servlets

 
0
  #4
Jul 3rd, 2008
  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1
Reputation: mariamAbed is an unknown quantity at this point 
Solved Threads: 0
mariamAbed mariamAbed is offline Offline
Newbie Poster

Re: Need help with Servlets

 
0
  #5
Aug 29th, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC