简体   繁体   中英

Unable to hit the Servlet page

Good Day,

I have just configured the tomcat and using the java servlet pages. I'm new with this and unable to hit the index page successfully but if I directly tried to hit the form action and passed the defined param then I could see the results. Please guide me if I'm missing something.

JSP - Code

 <div align="center" style="margin-top: 50px;">
    <form action="CrunchifyServlet">
        Please enter your Username:  <input type="text" name="username" size="20px"> <br>
        Please enter your Password:  <input type="text" name="password" size="20px"> <br><br>
        Please enter your Age:  <input type="text" name="age" size="20px"> <br><br>
    <input type="submit" value="submit">
    </form> 
</div>

Java - Code

 public class HelloCrunchify extends HttpServlet {
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // reading the user input
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String age = request.getParameter("age");
        PrintWriter out = response.getWriter();
        out.println (
                  "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" +" +
                      "http://www.w3.org/TR/html4/loose.dtd\">\n" +
                  "<html> \n" +
                    "<head> \n" +
                      "<meta http-equiv=\"Content-Type\" content=\"text/html; " +
                        "charset=ISO-8859-1\"> \n" +
                      "<title> Crunchify.com JSP Servlet Example  </title> \n" +
                    "</head> \n" +
                    "<body> <div align='center'> \n" +
                      "<style= \"font-size=\"12px\" color='black'\"" + "\">" +
                        "Username: " + username + " <br> " + 
                        "Password: " + password + " <br> " +
                        "Age: " + age +
                    "</font></body> \n" +
                  "</html>" 
                );      
        }

}

web.xml

   <display-name>CrunchifyJSPServletExample</display-name>
   <welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
   </welcome-file-list>
<servlet>
    <servlet-name>Hello</servlet-name>
    <servlet-class>com.crunchify.jsp.servlet.HelloCrunchify</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Hello</servlet-name>
    <url-pattern>/CrunchifyServlet</url-pattern>
</servlet-mapping>
 </web-app>

Project Explorer

and if i tried to hit this localhost:9080/CrunchifyJSPServletExample/Crunchify.jsp I'm getting HTTP Status 404.

Help will be appreciated.

Thanks

你需要从WEB-INF取出你的jsp并将它直接放在WebContent下,它会起作用。

After looking at your web.xml and reading your comment you are using Crunchify.jsp to post data ,but servlet container is unable to find Crunchify.jsp in proper folder in the war, so you are getting this error 404, what you need to do is place Crunchify.jsp in same folder as index.jsp 在此输入图像描述

As display in above image put Crunchify.jsp like WebPages --> Crunchify.jsp now if you invoke http://localhost:9080/CrunchifyJSPServletExample/Crunchify.jsp should work fine

Check if you are using the correct context path CrunchifyJSPServletExample . Typically it will be the generated war file name if you are deploying to Tomcat manually or if you use the tomcat configured in eclipse you can specify the path in the Modules tab.

Try it. Change Java Code.

Before:

protected void **doGet**(HttpServletRequest request, HttpServletResponse response) 

After:

protected void **service**(HttpServletRequest request, HttpServletResponse response)

or:

protected void **doPost**(HttpServletRequest request, HttpServletResponse response) 

Html code side, usually setting the http method like get or post. For example:

<form action="CrunchifyServlet" method="POST">

But there is no specification of http method. Perhaps html communicates to server with get method and servlet can't get recognized.

Please add code like sysout in servlet. And there is no output, http method is suspicious.

unable to hit the index page successfully

Let us say that your index page contents are in a file named index.html . Follow the instructions given below:

  1. Place index.html file in the root directory of the war file ie, at the same level with the WEB-INF directory.
  2. Package and deploy the app into the context CrunchifyJSPServletExample .
  3. Finally call the URL localhost:9080/CrunchifyJSPServletExample . It should fetch the contents of index.html .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM