0

I am new to servlet and making my first servlet using eclipse.I have made Index.html, Login.java and WelcomeServlet.java. But whenever I am trying to access the using

localhost:8080/ServletExample/

It shows 404 error.Here are the codes..

Index.html

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

Login.java

public class Login extends HttpServlet {  

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

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

        String n=request.getParameter("userName");  
        String p=request.getParameter("userPass");  

        if(p.equals("servlet")) {  
            RequestDispatcher rd=request.getRequestDispatcher("WelcomeServlet");  
            rd.forward(request, response);  
        } else {  
            out.print("Sorry UserName or Password Error!");  
            RequestDispatcher rd=request.getRequestDispatcher("/index.html");  
            rd.include(request, response);  
        }  
    }  
}  

WelcomeServlet.java

package java.io;
import java.io.*;  
import javax.servlet.*;  
import javax.servlet.http.*;  

public class WelcomeServlet extends HttpServlet {  

    public void doPost(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>  
    <servlet>  
        <servlet-name>Login</servlet-name>  
        <servlet-class>Login</servlet-class>  
    </servlet>  
    <servlet>  
        <servlet-name>WelcomeServlet</servlet-name>  
        <servlet-class>WelcomeServlet</servlet-class>  
    </servlet>  

    <servlet-mapping>  
        <servlet-name>Login</servlet-name>  
        <url-pattern>/Login</url-pattern>  
    </servlet-mapping>  
    <servlet-mapping>  
        <servlet-name>WelcomeServlet</servlet-name>  
        <url-pattern>/WelcomeServlet</url-pattern>  
    </servlet-mapping>  

    <welcome-file-list>  
        <welcome-file>index.html</welcome-file>  
    </welcome-file-list>  
</web-app>  
morgano
  • 17,210
  • 10
  • 45
  • 56
SPGuar
  • 353
  • 6
  • 17
  • 1
    why you set package name to `java.io` in WelcomeServlet? remove it or set full class name in web.xml in `servlet-class` attribute. – user1516873 Feb 07 '14 at 12:27
  • Sounds like you're missing either the servlet mapping or the context, hit http://localhost:8080/ServletExample/Login or http://localhost:8080/ServletExample/WelcomeServlet to see if you can get a response – morgano Feb 07 '14 at 12:36
  • thank you , there is a mistake in web.xml. I have corrected. Thank you for the hint. – SPGuar Feb 07 '14 at 14:30

3 Answers3

1

package java.io;

why u put this line in WelcomeServlet.java.

0

You're mapping to 'WelcomeServlet' not 'ServletExample'.

Try going to localhost:8080/WelcomeServlet

EDIT: There shouldn't be a trailing slash, sorry!

Dave Clarke
  • 2,677
  • 4
  • 22
  • 35
-2

Make sure ur projrct name is ServletExample.

localhost:8080/ServletExample/index.html