0

I am getting 404 error while running this login application using servlets on tomcat in eclipse. I am working on this from 2 days but still this problem is persisting.I am just not able to figure out what exactly is wrong with it. I always get:-

HTTP Status 404 - /GiftCardWebsiteLogin/Login

type Status report

message /GiftCardWebsiteLogin/Login

description The requested resource is not available.

Apache Tomcat/8.0.32

public class Login extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    String email = request.getParameter("email");
    String password = request.getParameter("pass");
    Validate validate=new Validate();
    if(validate.checkUser(email, password))
    {
        request.setAttribute("attributeName",email);
        RequestDispatcher result = request.getRequestDispatcher("Welcome");
        result.forward(request, response);
    }
    else
    {
       out.println("Username or Password incorrect");
       RequestDispatcher result = request.getRequestDispatcher("index.html");
       result.include(request, response);
    }
}  

}

html file 
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>LOGIN</title>
</head>
<body>
        <form method="post" action="Login">
        Email ID:<input type="text" name="email" /><br/>
        Password:<input type="password" name="pass" /><br/>
        <input type="submit" value="Login" />
        </form>
        If you are a new user you can register here.<a     href="register.html">Register</a>       
    </body>
</html>


web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns="http://java.sun.com/xml/ns/javaee"     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <servlet>
    <servlet-name>Login</servlet-name>
    <servlet-class>/Login</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>Welcome</servlet-name>
    <servlet-class>Welcome</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Welcome</servlet-name>
    <url-pattern>/Welcome</url-pattern>
  </servlet-mapping>

<servlet>
        <servlet-name>Register</servlet-name>
         <servlet-class>Register</servlet-class>
     </servlet>
    <servlet-mapping>
        <servlet-name>Register</servlet-name>
        <url-pattern>/Register</url-pattern>
    </servlet-mapping>
<welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>
Vikas Kumar
  • 157
  • 1
  • 7

3 Answers3

1

You added accidentally a slash in your web.xml file when declaring the class:

<servlet>
    <servlet-name>Login</servlet-name>
    <servlet-class>/Login</servlet-class>
</servlet>

Correct should be without the slash(/):

<servlet>
    <servlet-name>Login</servlet-name>
    <servlet-class>Login</servlet-class>
</servlet>

You also have to add the mapping:

<servlet-mapping>
    <servlet-name>Login</servlet-name>
    <url-pattern>/Login</url-pattern>
</servlet-mapping>
Mayday
  • 4,680
  • 5
  • 24
  • 58
0

You need to map your servlet-class, where you have mapped your URL-pattern

 <servlet>
    <servlet-name>Login</servlet-name>
    <servlet-class>Login</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>Login</servlet-name>
    <url-pattern>/Login</url-pattern>
  </servlet-mapping>
Santhosh
  • 8,181
  • 4
  • 29
  • 56
0

According to what you have posted, you have not mapped a URL pattern to a servlet class. There is basically no way for your app to find the Login servlet. Please have a look at the Java Documentation regarding servlets:

https://docs.oracle.com/cd/E13222_01/wls/docs92/webapp/configureservlet.html

In your web.xml you need to create an entry for your servlet mapping and link it to the servlet, for example:

// The servlet: the name must contain the qualified package name as well

<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>com.package.name.servlets.LoginServlet</servlet-class>
</servlet>

// The mapping

<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/login/*</url-pattern>
</servlet-mapping>

The above declaration maps all URLs that contain login to the specified servlet.

I hope this helps you out!

ArnoldKem
  • 100
  • 2