Sunday, March 15, 2020

Servlets Login Page Example

Simple Servlet Application for Login Page

In this tutorial, we are creating a simple Servlet Application for Login Page. In this example, we have two important files. The first index.html with a form to submit the request and second, a servlet to handle the request. This application is based upon the request-response model. 

Request-Response model servlet

The form in the index.html page uses the get method to submit the request. LoginServlet uses the doGet(HttpServletRequest request, HttpServletResponse response) method. This method is invoked by the servlet container to service the request. The HttpServletRequest and HttpServletResponse belong to the javax.servlet.http package.


The HttpServletRequest is the interface extending the ServletRequest interface. The request object is used to retrieve request parameters from the incoming request. HttpServletRequest provides the following methods  

  • public String getAuthType()
  • public String getContextPath ()
  • public Cookie [] getCookies ( )
  • public long getDateHeader(String name)
  • public String getHeader(String name)
  • public Enumeration getHeaderNames()
  • public Enumeration getHeaders (String name)
  • public int getIntHeader (String name)
  • public String getMethod()
  • public String getPathlnfo()
  • public String getpathTranslated ()
  • public String getQueryString ()
  • public String getRemoteUser ()
  • public String getRequestedSessionld()
  • public String getRequestURI()
  • public StringBuffer getRequestURL()
  • public String getServletpath ()
  • public HttpSession getSession ()
  • public HttpSession getSession(boolean create)
  • public Principal getUserPrincipal()
  • public boolean isRequestedSessionldFromCookie()
  • public boolean isRequestedSessionldValid()
  • public boolean isUserlnRole (String role)


The HttpServletResponse is the interface extending the ServletRsponse interface. The response object is used to compose the result and display the results to the users. HttpServletResponse provides the following methods 

  • public void addCookie(Cookie cookie)
  • public void addDateHeader (String name, long date)
  • public void addHeader(String name,String value)
  • public void addlntHeader(String name,int value)
  • public boolean containsHeader(String name)
  • public String encodeRedirectURL(String url)
  • public String encodeURL(String url)
  • public void sendError(int sc) throws IOException
  • public void sendError(int Be, String mag) throws IOException
  • public void sendRedirect(string location) throws IOException
  • public void setDateHeader(String name,long date)
  • public void setHeader(String name, String value)
  • public void setlntHeader (String name,int value)
  • public void setStatus(int s)

We are using the Eclipse IDE for this application.

Step 1:
Go to file->new->dynamic web project(if option is unavailable than go to window->perspective->open perspective->J2EE)

Step 2:
Provide your project name, provide runtime(Tomcat Apache) and click next, keep remaining settings unchanged. (To learn how to set runtime visit this link)

Step 3:
Again click next

Step 4:
Check the checkbox to create the web.xml(Deployment descriptor).

The project must be created; you can explore the project explorer to locate the project.

Step 5:
Right-click on the project, new->HTML file, name the file index.html. Write the following code in this file.

<!DOCTYPE html>
<meta charset="ISO-8859-1">
<body bgcolor='yellow'>
<form method="get" action='LoginServlet'>
<input type='text' placeholder="Enter your name" name="name"><br>
<input type="password" placeholder="Enter your password" name="pass"><br>
<input type='submit' value='login'>

Step 6:
Again Right-click on the project, new->servlet, name the servlet as LoginServlet, click next, select the doGet method click finish.

Write the following code.

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

 * Servlet implementation class LoginServlet
public class LoginServlet extends HttpServlet {
     private static final long serialVersionUID = 1L;
    public LoginServlet() {

     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
           //Write Code here
           PrintWriter pout=response.getWriter();
           String str="";
           String name=request.getParameter("name");
           String pass=request.getParameter("pass");
           if(name.equals("Admin") && pass.equals("12345"))
                str="Welcome Admin";
           else {
                str="No valid user";


Step 7:
 Right-click on index.html->run as->run on server


on clicking the submit button

Video Tutorial