Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.jspI'm getting HTTP Status 404.

Help will be appreciated.

Thanks

like image 300
M A. Avatar asked Nov 22 '15 07:11

M A.


People also ask

Why my servlet is not working in Eclipse?

Add the server runtime to the web project to fix the HttpServlet not found Eclipse error. This will add all the JAR and ZIP files used by the servlet engine to the web project's runtime path. When this step is complete, rebuild the project and the HttpServlet not found Eclipse error message will go away.

What are the problems with servlets?

For non-java developers, servlet is not suitable as they required to have a broad knowledge of Java servlet. HTML code is mixed up with Java code therefore, changes done in one code can affect another code. Writing HTML code in servlet programming is very difficult. It also makes servlet looks bulky.

How do I redirect a servlet page in HTML?

The sendRedirect() method of HttpServletResponse interface can be used to redirect response to another resource, it may be servlet, jsp or html file. It accepts relative as well as absolute URL. It works at client side because it uses the url bar of the browser to make another request.


2 Answers

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

like image 90
Prince Avatar answered Oct 16 '22 10:10

Prince


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 enter image description here

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

like image 2
Dev Avatar answered Oct 16 '22 09:10

Dev