Newsletter

Java Servlet login Example In Eclipse

Servlets » on Jan 19, 2013 { 17 Comments } By Sivateja

Let us discuss one simple login application using servlet and jsp, friends please refer previous articles if you still have any doubts regarding strvlets flow 🙂

Directory Structure

Files Required

  • OnServletLogin.java
  • index.html
  • web.xml

index.html

<form action="login" method="post">
   <table>
   <tr>
      <td><font face="verdana" size="2px">Name:</font></td>
      <td><input type="text" name="userName"></td>
   </tr>
   <tr>
      <td><font face="verdana" size="2px">Password:</font></td>
      <td><input type="password" name="userPassword"></td>
   </tr>
   </table>
      <input type="submit" value="Login">
</form>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

 <servlet>
   <servlet-name>loginServlet</servlet-name>
   <servlet-class>java4s.OnServletLogin</servlet-class>
 </servlet>

 <servlet-mapping>
   <servlet-name>loginServlet</servlet-name>
   <url-pattern>/login</url-pattern>
 </servlet-mapping>

 <welcome-file-list>
   <welcome-file>index.html</welcome-file>
 </welcome-file-list>

</web-app>

OnServletLogin.java

package java4s;

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 OnServletLogin extends HttpServlet  
{
    protected void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
	{
		PrintWriter pw=res.getWriter();
		res.setContentType("text/html");

		String user=req.getParameter("userName");
		String pass=req.getParameter("userPassword");

		if(user.equals("java4s")&&pass.equals("java4s")) 
                 pw.println("Login Success...!"); 
                else
                 pw.println("Login Failed...!");
		 pw.close();

	}

}

Output

 

​ ​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

17 Responses to “Java Servlet login Example In Eclipse”
  1. Sireesha says:

    Can you please talk about the tag welcome-file-list. How will the parameters defined under that tag be used. How will the webserver understand that the submit button on the index.html corresponds to the servlet-mapping LoginServlet with url pattern /login???

  2. Subair says:

    It is very helpful

  3. brijesh says:

    what are the benifits of servletconfig

  4. naresh says:

    .html file and .xml file url should same????

  5. huynh says:

    Login correct username and password but it’s redirect login failed

  6. Ankit Srivastava says:

    Can You explain the flow of this Program, how it works.

  7. akula says:

    Thanks you very much for explaning this concept,here one more thing i didn’t understand.i,e DAO.here u didn’t used…
    will u pls explain this once.

  8. Aneeq Azam says:

    it is very helpful but I needed to check entered username and password is equal to param values entered in xml
    so If you update it with that it’ll be more helpful
    thanks alot

  9. Poornachander Kota says:

    It is easy to understand..

  10. Parameswari says:

    Could you pls explain the code flow.. Am getting HTTP 405 errorHTTP method GET is not supported by this URL. Thanks!!

  11. viswanath says:

    Could you please tell me what is the difference between servlet and webservice? and which is better?

    • mahesh says:

      servlet directly takes request from the end-user(browser) but webservice doesn't takes the request directly from the end-user & it takes the request from another servlet (or) any service.webservice is very important becoz it is interoperable(platform independent,language independent) & distributed……………

  12. avi says:

    nice and very helpful

  13. Chitharanjan says:

    Is there any jar files I need to add for executing this code.

  14. Rammi says:

    hello,it is occurring error offer login

    what is error?

    HTTP Status 500 – Error instantiating servlet class java4s.ServletLogin

    type Exception report

    message Error instantiating servlet class java4s.ServletLogin

    description The server encountered an internal error that prevented it from fulfilling this request.

    exception
    javax.servlet.ServletException: Error instantiating servlet class java4s.ServletLogin
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2521)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2510)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Unknown Source)

    root cause
    java.lang.ClassNotFoundException: java4s.ServletLogin
    org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1333)
    org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1167)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2521)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2510)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Unknown Source)

    note The full stack trace of the root cause is available in the Apache Tomcat/8.0.44 logs.

  15. meera says:

    Tell me how to solve HTTP Status 500 ? Internal Server Error

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.