Servlet that checks user details

Estudies4you
Servlet that checks user details
Example2 : Servlet that checks user details
  • This example explains how to validate login details and send back the response accordingly
  • Create following files in a folder (Please check next slide for code)
    • Java File       LoginServlet.java             Contains logic - check user details and send response
    • DD web.xml              Contains deployment details. Deployment Descriptor
    • HTMLFile     index.html          Contains html that allows user to enter details
  • Compile the Java File as you did in earlier example
  • Create below structure with files that you created above
    • Folder "LoginServlet" contains
    • I__ "WEB-INF" Folder and index.html
    • I__"WEB-INF" Folder contains "classes" Folder and web.xml File
    • I__"classes" Folder contains "LoginServlet.class" File
  • Deploy the LoginServlet folder inside webapps folder of Tomcat
  • Bounce the Server
  • Send request using "http://localhost:8550/LoginServlet/index.html"

LoginServlet.java : This prepares the response as HTML file that displays if user provided values are correct or not
import java.io.IOException;
import java.io. PrintWriter;
import java.sql.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet. http.HttpServlet;
import javax.servlet. http.HttpServletRequest;
import javax.servlet. http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, I0Exception {
response.setContentType("text/html");
PrintWriter out = response.getWriter();               
String n=request.getParameter("username");
String p=request.getParameter("userpass");
if(validate(n, p)){
out.printin(" <html>");
out.println("</head>");
out.printin("<body>");
outprintln("<h1>Hello "+n+"</h1>");
outprintln("!!!!! Our Site is under construction. Please come back!!!!!");
out.println("</body>");
out.println("</html>");
}
else {
out.println("<html>");
out.println("<body>");
out.println("<h1>Check your credentials</h1>");
outprintln("!!!!! I am very strong.. I cannot allow strangers !!!!!");
out.println("</body>");
out. println("</html>");
}
out.close();

}
public static boolean validate(String name,String pass){
if(name.equals("arun") && pass.equals("password1"))
return true;
else if(name.equals("kiran") && pass.equals("password2"))
return true;
else
return false;
}
}
  • web.xml
    • <servlet> tag maps your servlet class with internal name
    • <servlet-mapping> tag maps internal name with url-pattern that your invoke

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<servlet>
<servlet-name>loginexample</servlet-name>
<servlet-class> LoginServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>loginexample</servlet-name>
<url-pattern>/loginpage.doqurl-pattern>
</servlet-mapping>
 </web-app>

index.html
<html>
<body>
<h1  align="center">Welcome to Our Website</h1>
<h2 align="center">Please provide your credentials to login</h2>
<form action="loginpage.do" method="post">
Name : <input type="text" name="username"/> <br/> <br/>
Password: <input type="password" name="userpass"/><br/><br/>
<input type="submit" value="login"/>
</form>
</body>
</html>

Output: http://localhost:8550/LoginServlet/index.html
Servlet that checks user details

In our servlet class, we used static username/password. Instead you can validate the data against the details from database also.

To Top