0

I'm newbie to servlet programming. I have checked various other links about the Http 404 error but nothing helped me. So I'm posting my code here.

I have three html forms in WebContent/ folder form1.html, form2.html,form3.html, all these forms have same url pattern because accessing three different forms in the same Http session.

form1.html

<html>
<head>
<title>Adhar Registration Form</title>
</head>
<body style="background-color:orange">
<h1>FORM 1</h1>
<form action="./reg" method="get">

<table>
<tr><td>NAME:</td><td><input type="text" name="id"/></td></tr>
<tr><td>F_NAME:</td><td><input type="text" name="name"/></td></tr>
<tr><td>M_NAME:</td><td><input type="text" name="email"/></td></tr>

<tr><td><input type="submit" name= "NEXT"> </td></tr>
</table>
<input type="hidden" name="fno" value="1">

</form>

</body>
</html>

form2.html

<html>
<head>
<title>Adhar Registration Form</title>
</head>
<body style="background-color:orange">
<h1>FORM 2</h1>

<form action="./reg" method="get">

<table>
<tr><td>CONTACT:</td><td><input type="text" name="id"/></td></tr>
<tr><td>EMAIL:</td><td><input type="text" name="name"/></td></tr>
<tr><td>ADDRESS:</td><td><textarea rows ="10" cols="5" name="address"></textarea></td></tr>

<tr><td><input type="submit" name= "NEXT"> </td></tr>
</table>
<input type="hidden" name="fno" value="2">

</form>

</body>
</html>

<html>
<head>

<title>Adhar Registration Form</title>
</head>
<body style="background-color:orange">
<h1>FORM 3</h1>
<form action="./reg" method="get">

<table>
<tr><td>QUALIFICATION:</td><td><input type="text" name="id"/></td></tr>
<tr><td>PAN NO:</td><td><input type="text" name="name"/></td></tr>

<tr><td><input type="submit" name= "Register"> </td></tr>
</table>
<input type="hidden" name="fno" value="3">

</form>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Adhar</display-name>
  <welcome-file-list>
    <welcome-file>form1.html</welcome-file>
  </welcome-file-list>
  
  <servlet>
    <servlet-name>login</servlet-name>
    <servlet-class>container.RegistrationServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>login</servlet-name>
    <url-pattern>/reg</url-pattern>
</servlet-mapping>
</web-app>

RegistrationServlet.java

package container;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class RegistrationServlet
 */
@WebServlet("/reg")
public class RegistrationServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public static final String URL = "jdbc:mysql://localhost/db";
    public static final String USER = "root";
    public static final String PASSWORD = "12345";
    public static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";

    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegistrationServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        PrintWriter out = response.getWriter();

        HttpSession hs = request.getSession();
        String fno = request.getParameter("fno");

        if(fno.equals("1")){
            String name = request.getParameter("name");
            String f_name = request.getParameter("f_name");
            String m_name = request.getParameter("m_name");

            hs.setAttribute("name", name);
            hs.setAttribute("f_name", f_name);
            hs.setAttribute("m_name", m_name);

            response.sendRedirect("./Form2.html");
        }
        if(fno.equals("2")){

            String contact = request.getParameter("contact");
            String email = request.getParameter("email");
            String address = request.getParameter("address");

            hs.setAttribute("contact", contact);
            hs.setAttribute("email", email);
            hs.setAttribute("address", address);

            response.sendRedirect("./Form3.html");
        }
        if(fno.equals("3")){

            String qual = request.getParameter("qual");
            String pan = request.getParameter("pan");

            String name = (String)hs.getAttribute("name");
            String f_name = (String)hs.getAttribute("f_name");
            String m_name = (String)hs.getAttribute("m_name");

            String contact = (String)hs.getAttribute("contact");
            String email = (String)hs.getAttribute("email");
            String address = (String)hs.getAttribute("address");

            try {
                Class.forName(DRIVER_CLASS);
                System.out.println("Loaded the driver");

                Connection con = DriverManager.getConnection(URL,USER,PASSWORD);
                PreparedStatement ps = con.prepareStatement("INSERT into adharReg values(?,?,?,?,?,?,?,?)");

                ps.setString(1, name);
                ps.setString(2, f_name);
                ps.setString(3, m_name);
                ps.setString(4, contact);
                ps.setString(5, email);
                ps.setString(6, address);
                ps.setString(7, qual);
                ps.setString(8, pan);

                int i = ps.executeUpdate();

                if(i!=0){
                    out.println("<h1>REGISTRATION SUCCESS</h1>");
                }
                else{
                    out.println("<h1>REGISTRATION FAILED</h1>");
                }

            } catch (Exception e) {
                // TODO: handle exception
                out.println("<h1>REGISTRATION FAILED" + e.getMessage() + " </h1>");
            }

        }
    }

}

I'm using tomcat server 8.0. I checked this Link and my tomcat server has exact same settings. And followed this link for any possible mistakes but, I don't know the exact reason why i'm getting Http 404 error. Help me out why i'm getting this error.

Thanks.

karthi13
  • 15
  • 4

2 Answers2

0

check web.xml is exist into WEB-INF folder.

and use servlet mapping into web.xml

And register the servlet instead in web.xml like this:

<servlet>
<servlet-name>yourServlet</servlet-name>
<servlet-class>com.example.YourServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>yourServlet</servlet-name>
<url-pattern>/servlet</url-pattern>  
</servlet-mapping>

for more refer bellow link :-

http://www.beingjavaguys.com/2013/08/jsp-servlet-hello-world-example.html

https://www.javatpoint.com/servlet-with-annotation

Anshul Sharma
  • 3,432
  • 1
  • 12
  • 17
0

The issue is caused by a conflict between web.xml configuration and the WebServlet annotation. The web.xml is delcaring a servlet called login that target to the /reg url, but also in the RegistrationServlet there is a declaration throught @WebServlet annotation that reference to the same url /reg.

One possible solution is to remove the servlet declaration from the web.xml, that means that the web.xml content should be like this.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Adhar</display-name>
  <welcome-file-list>
    <welcome-file>form1.html</welcome-file>
  </welcome-file-list>

</web-app>

Just let the Servlet declared by annotation only. Hope this helps.

Daniel C.
  • 5,418
  • 3
  • 23
  • 26