| | |
Need help with Servlets
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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.
I also included an image of my file structure.
Here's the java code
Here's the xml Code
and here's the html document code
Why do I get this message instead of the servlet response and how do I fix it.
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
Here's the java code
java Syntax (Toggle Plain Text)
3 import javax.servlet.ServletException; 4 import javax.servlet.http.HttpServlet; 5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.http.HttpServletResponse; 7 import java.io.IOException; 8 import java.io.PrintWriter; 9 10 public class WelcomeServlet extends HttpServlet 11 { 12 // process "get" requests from clients 13 protected void doGet( HttpServletRequest request, 14 HttpServletResponse response ) 15 throws ServletException, IOException 16 { 17 response.setContentType( "text/html" ); 18 PrintWriter out = response.getWriter(); 19 20 // send XHTML page to client 21 22 // start XHTML document 23 out.println( "<?xml version = \"1.0\"?>" ); 24 25 out.printf( "%s%s%s" , "<!DOCTYPE html PUBLIC", 26 " \"-//W3C//DTD XHTML 1.0 Strict//EN\"", 27 " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n" ); 28 29 out.println( "<html xmlns = \"http://www.w3.org/1999/xhtml\">" ); 30 31 // head section of document 32 out.println( "<head>" ); 33 out.println( "<title>A Simple Servlet Example</title>" ); 34 out.println( "</head>" ); 35 36 // body section of document 37 out.println( "<body>" ); 38 out.println( "<h1>Welcome to Servlets!</h1>" ); 39 out.println( "</body>" ); 40 41 // end XHTML document 42 out.println( "</html>" ); 43 out.close(); // close stream to complete the page 44 } // end method doGet 45 } // end class WelcomeServlet
Here's the xml Code
xml Syntax (Toggle Plain Text)
<web-app xmlns= "http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <!-- General description of your Web application --> <display-name> Java How to Program JSP and Servlet Chapter Examples </display-name> <description> This is the Web application in which we demonstrate our JSP and Servlet examples. </description> <!-- Servlet definitions --> <servlet> <servlet-name>welcome1</servlet-name> <description> A simple servlet that handles an HTTP get request. </description> <servlet-class> WelcomeServlet </servlet-class> </servlet> <!-- Servlet mappings --> <servlet-mapping> <servlet-name>welcome1</servlet-name> <url-pattern>/welcome1</url-pattern> </servlet-mapping> </web-app>
and here's the html document code
html Syntax (Toggle Plain Text)
<?xml version = "1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <!-- Fig. 26.7: WelcomeServlet.html --> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title>Handling an HTTP Get Request</title> </head> <body> <form action = "/jhtp6/welcome1" method = "get" > <p><label>Click the button to invoke the servlet <input type = "submit" value = "Get HTML Document" /> </label></p> </form> </body> </html>
Why do I get this message instead of the servlet response and how do I fix it.
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
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 ?"
"How to ask questions the smart way ?"
•
•
Join Date: Jul 2008
Posts: 16
Reputation:
Solved Threads: 0
html Syntax (Toggle Plain Text)
<?xml version = "1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <!-- Fig. 26.7: WelcomeServlet.html --> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title>Handling an HTTP Get Request</title> </head> <body> <form action = "welcome1" method = "get" > <p><label>Click the button to invoke the servlet <input type = "submit" value = "Get HTML Document" /> </label></p> </form> </body> </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
![]() |
Similar Threads
- How to deploy servlets in a jakarta tomcat server (Java)
- servlets - javax.servlets does not exist (JSP)
- running servlets (JSP)
- Servlets and HTML (HTML and CSS)
- suggest some good books for servlets&jsp (Java)
- about java servlets (Java)
Other Threads in the Java Forum
- Previous Thread: Need Help
- Next Thread: Read a text file from a website
| Thread Tools | Search this Thread |
Tag cloud for Java
affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth businessintelligence chat class classes client code component database desktop draw ebook eclipse encode equation error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer intersect j2me java javaexcel javaprojects jmf jni jpanel julia linked linux list loop mac main map method methods mobile netbeans newbie number object open-source oracle parameter print problem program programming project properties recursion reference replaysolutions rotatetext scanner score screen scrollbar server set size sms socket sort sql string superclass swing template test threads time tree windows working xstream






