Failed to get to Servlet page

Good afternoon,

I just configured tomcat and used java servlet pages. I am new to this and could not get to the index page successfully, but if I directly tried to click on the form action and pass a certain parameter, I could see the results. Please guide me if I missed 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 click this localhost:9080/CrunchifyJSPServletExample/Crunchify.jsp , I get an HTTP status of 404.

Help will be appreciated.

thanks

+5
source share
5 answers

You need to take out your jsp from WEB-INF and place it directly under the WebContent , and it will work.

+5
source

By looking at your web.xml and reading your comment, you use Crunchify.jsp to send data, but the servlet container cannot find Crunchify.jsp in the correct folder in the war, so you get this 404 error, what you need to do is place Crunchify.jsp in the same folder as index.jsp enter image description here

As shown in the picture above, put Crunchify.jsp as your web page -> Crunchify.jsp now if you call http://localhost:9080/CrunchifyJSPServletExample/Crunchify.jsp , it should work fine

+2
source

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

+1
source

Give it a try. Change the 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 that usually sets the http method, such as get or post. For instance:

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

But the specification of the http method is missing. It is possible that html communicates with the server using the get method, and the servlet cannot be recognized.

Please add code like sysout to the servlet. And no conclusion, the http method is suspicious.

+1
source

could not successfully click on the index page

Say the contents of your index page are in a file called index.html . Follow the instructions below:

  • Place the index.html file in the root directory of the military file ie at the same level using the WEB-INF .
  • Package and application deployment in the context of CrunchifyJSPServletExample .
  • Finally, call the localhost URL : 9080 / CrunchifyJSPServletExample . It should receive the contents of index.html .
+1
source

Source: https://habr.com/ru/post/1236539/


All Articles