0

I have created a program in eclipse using servlet only where it asks about log in details.if paasword is correct the user name and message is displayed. however when I write user name and password the eclipse displays resource not found. can someone please tell me what the problem is?

index.html

<form action="go" method="get">
Name:<input type="text" name="userName"/><br/>
Password:<input type="password" name="userPass"/><br/>
<input type="submit" value="login"/>

</form>

Simple.java

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.*;
import javax.servlet.http.*;


public class Simple extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        String p=request.getParameter("userPass");
        if(p.equals("servlet")){
            RequestDispatcher rd=request.getRequestDispatcher("welcome");
            rd.forward(request, response);

        }
        else{
            out.print("Sorry username or password error!");
            RequestDispatcher rd=request.getRequestDispatcher("login.html");
            rd.include(request, response);
        }

    }

}

WelcomeServlet.java

import java.io.IOException;
import java.io.PrintWriter;

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


public class WelcomeServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        String n=request.getParameter("userName");
        out.print("Welcome "+n);
    }

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>Simple</servlet-name>
    <servlet-class>Simple</servlet-class>
  </servlet>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>WelcomeServlet</servlet-name>
    <servlet-class>WelcomeServlet</servlet-class>
  </servlet>


  <servlet-mapping>
    <servlet-name>Simple</servlet-name>
    <url-pattern>/go</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>WelcomeServlet</servlet-name>
    <url-pattern>/welcome</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>login.html</welcome-file>
  </welcome-file-list>
</web-app>

2 Answers2

0

Server is not able to map your request to a servlet.In code snippet

<form action="go" method="get">

Here on click of submit button,you are calling servlet "go" which is not available in your application rather i would say not mapped to any servlet you specified here.

Either you can use web.xml for servlet mapping or you can use annotations for that purpose.

Servlets should be registered with servlet container. For that, you should add entries in web deployment descriptor web.xml. It is located in WEB-INF directory of the web application.

If you want to use annotations,You can use @WebServlet to map your servlet.

See Also

Community
  • 1
  • 1
RockAndRoll
  • 2,247
  • 2
  • 16
  • 35
  • thanks a lot..it helped me. the program was executed but when i gave correct password it displayed server at 123 (123 is my project name).what i should do to display a message when i type correct password??? – Mayank Rajput Sep 26 '15 at 07:19
0

I appreciate your effort in start learning from the mere basics of Servlets. Here are few corrections/observations:

  1. You receive resource not found on accessing the application is because of the welcome-file configuration. You have it as login.html and the your login page is index.html. Add an entry index.html to web.xml or rename index.html to login.html
  2. When any user name is entered with password servlet, it shows the welcome message and that is great! But if the password does not match it tries to find login.html and it fails.
James Jithin
  • 10,183
  • 5
  • 36
  • 51