0

I am trying my forst jsp login program. It shows the login page correctly, but when I submit it gives the error:

HTTP Status 404 - /UserPass/username

type Status report

message /UserPass/username

description The requested resource is not available.

Here's my code:

username.java

package sid;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class username extends HttpServlet{

    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
    {
        response.setContentType("text/html");

        String user=request.getParameter("username");
        String pass=request.getParameter("password");

        if(user.equals("sid") && pass.equals("hello"))
        {
            System.out.println("Welcome");
        }
        else
        {
            System.out.println("Invalid username or password");
        }
    }
}

user.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html">
<title>Insert title here</title>
</head>
<body>
<h1>Login Page</h1> 
<center> 

<form action="username" method="GET"> <br/>
User :<input type="text" name="username"> <br/>
Pass :<input type="password" name="password"> 
<br/><input type="submit" value="Submit"> </form> 
</center>
</body>
</html>

web.xml

<web-app>
    <servlet>
    <servlet-name>login</servlet-name>
    <servlet-class>sid.username</servlet-class>
    </servlet>

    <servlet-mapping>
    <servlet-name>login</servlet-name>
    <url-pattern>/username/*</url-pattern>
    </servlet-mapping>
</web-app>

My user.jsp code is in WebContect, and my web.xml is in WebContent/WEB-INF/lib. I am using Eclipse Indigo SR2 and Apache Tomcat.

Siddharth
  • 884
  • 3
  • 11
  • 27
  • What happens for `/UserPass/username/foo`? Additionally, while this may be a useful exercise to learn how servlets work, I strongly recommend against hand-writing them. For real-world programs, use a framework such as Spring MVC that handles all the boilerplate details for you. – chrylis -cautiouslyoptimistic- Oct 27 '14 at 04:53
  • Nothing happens there. After login, I just want it to display on console. – Siddharth Oct 27 '14 at 04:57

4 Answers4

2

You should put your web.xml in /WEB-INF, not in lib.

Also, you will need to restart the server for your changes to reload and reflect.

Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
Kumar Gaurav
  • 1,287
  • 3
  • 19
  • 47
1

Change your servlet mapping

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

to

<servlet-mapping>
    <servlet-name>login</servlet-name>
    <url-pattern>/username</url-pattern>
    </servlet-mapping>
mahesh
  • 1,311
  • 1
  • 13
  • 26
0

I have pointed 2-3 things in web.xml.

first put web.xml in /WEB-INF not in lib

second check servlet name in web.xml. It should be username instead of login.

<servlet>
<servlet-name>username</servlet-name>
<servlet-class>sid.username</servlet-class>
</servlet>

third change this

<servlet-mapping>
<servlet-name>username</servlet-name>
<url-pattern>/username</url-pattern>
</servlet-mapping>
Santosh Karna
  • 119
  • 1
  • 3
  • 12
-1

By nomenclature class name should start with capital letter and try changing as

form action="/username" method="GET"> <br/>

User :
Pass :

in login.jsp and see if this works

Naresh kumar
  • 1,069
  • 1
  • 7
  • 21