Login filter

Login filter


Posted in : Servlet Posted on : December 2, 2010 at 6:24 PM Comments : [ 0 ]

This section contains the details about Login Filter.

Login filter

As we discuss earlier, a filter is an object that can transform a request or alter a response. It process request before it reaches to a servlet and can process response before it leaves to a servlet.

This filter performs pre-login activity to determine if the user trying to log-in is not the restricted user. The restricted user list is reside in a text file. Given below the example which have a html login form, a servlet & a filter to restrict some user :

username.txt(Restricted user list)

Ankit
Kapil
Divya
Vinita

LoginFilter.html

<HTML>
<HEAD>
<TITLE>Login Filter</TITLE>
</HEAD>
<BODY bgcolor="silver">
<form method="post" action="/ankit/servlet/LoginFilterDemo">
<center>
<h3>ENTER USER NAME & PASSWORD</h3>
<table border=1>
<tr><td>Enter Your Name :</td>
<td><input type="text" name="userId" value=""></td>
</tr>
<tr><td>Enter Your PassWord :</td>
<td><input type="password" name="userPassWord" value=""></td>
</tr>
<tr><td><input type="submit" name="Submit" value="Submit"></td>
<td><input type="reset" name="reset" value="Refresh"></td>
<table>
</center>
</form>
</BODY>
</HTML>

LoginFilter.java

import java.io.*;
import java.util.*;
import javax.servlet.*;
import java.io.IOException;
import java.util.StringTokenizer;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

public class LoginFilter implements Filter{
public LoginFilter() {}
public final static String devil = "Ankit";
private FilterConfig filterConfig;

public void init(FilterConfig config) throws ServletException{
this.filterConfig = config;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterchain) throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head><title>Login Filter Example</title></head>");
String username = request.getParameter("userId");
HttpServletResponse httpResponse = null;
if (response instanceof HttpServletResponse){
httpResponse = (HttpServletResponse) response;
}
BufferedReader input = new BufferedReader(new FileReader("C://Username.txt"));
String line = "";
while ((line = input.readLine()) != null) {
if (line.equals(username)) {
httpResponse.sendError(HttpServletResponse.SC_FORBIDDEN,"You are not allowed to
access the servlet!"); 
} 
}
filterchain.doFilter(request, response);
out.println("<body><h3><font color='green'>Passed successfully from IP Filter
<font></h3></body></html>");
}
public void destroy() {}
}

LoginFilterDemo.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 LoginFilterDemo extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)throws 
IOException,ServletException{
PrintWriter out=response.getWriter();
response.setContentType("Text/Html");
String userId=request.getParameter("userId");
String userPassWord=request.getParameter("userPassWord");
out.println("<html>");
out.println("<body bgcolor=silver>");
out.println("<h2> Getting Parameter from User Example</h2>");
out.println("<b><Font color=blue >");
out.println("User Id is -" +userId);
out.println("<br>");
out.println("User PassWord is -" + userPassWord);
out.println("</b></font>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)throws 
IOException,ServletException{
doGet(request,response);
}
}

Output

Restricted user login :

It will display the following message :

If user(For ex.-Neha) is not restricted, it will display following message :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics