Practical : 11
Subject : Advanced Java
Aim : Implement Authentication filter using filter API.
index.html Code:
--------------------------------------------------------------------------------
<html>
<head>
<title>Filter API</title>
<style>
.b1
{
width: 370px;
height: 42px;
margin:10px;
color: white;
background-color: #008CBA;
border: 0px solid grey;
cursor: pointer;
}
.side
{
padding: 10px;
margin: 20px;
width: 370px;
}
.b1:hover
{
background-color: #4CAF50;
}
</style>
</head>
<body bgcolor="#222222">
<div class="box">
<form action="filtering" style="text-align:center;">
<h1 style="color:white;font-family: Times;margin-top:160px;">Login Portal</h1>
<input class="side" type="text" required="" name="name" placeholder="UserName"><br>
<input class="side" type="password" required="" name="password" placeholder="Password" style=margin-top:-5px;"><br>
<input class="b1" type="submit" value="Login">
</form>
</div>
</body>
</html>
--------------------------------------------------------------------------------
<html>
<head>
<title>Filter API</title>
<style>
.b1
{
width: 370px;
height: 42px;
margin:10px;
color: white;
background-color: #008CBA;
border: 0px solid grey;
cursor: pointer;
}
.side
{
padding: 10px;
margin: 20px;
width: 370px;
}
.b1:hover
{
background-color: #4CAF50;
}
</style>
</head>
<body bgcolor="#222222">
<div class="box">
<form action="filtering" style="text-align:center;">
<h1 style="color:white;font-family: Times;margin-top:160px;">Login Portal</h1>
<input class="side" type="text" required="" name="name" placeholder="UserName"><br>
<input class="side" type="password" required="" name="password" placeholder="Password" style=margin-top:-5px;"><br>
<input class="b1" type="submit" value="Login">
</form>
</div>
</body>
</html>
--------------------------------------------------------------------------------
MyFilter.java Code:
--------------------------------------------------------------------------------
import java.io.*;
import javax.servlet.*;
public class MyFilter implements Filter
{
@Override
public void init(FilterConfig arg0) throws ServletException {}
@Override
public void doFilter(ServletRequest req, ServletResponse resp,FilterChain chain) throws IOException, ServletException
{
PrintWriter pw=resp.getWriter();
String pwd=req.getParameter("password");
if(pwd.equals("birju"))
{
chain.doFilter(req, resp);
}
else
{
pw.print("Invalid Password");
RequestDispatcher redi=req.getRequestDispatcher("index.html");
redi.include(req, resp);
}
}
@Override
public void destroy() {}
}
--------------------------------------------------------------------------------
import java.io.*;
import javax.servlet.*;
public class MyFilter implements Filter
{
@Override
public void init(FilterConfig arg0) throws ServletException {}
@Override
public void doFilter(ServletRequest req, ServletResponse resp,FilterChain chain) throws IOException, ServletException
{
PrintWriter pw=resp.getWriter();
String pwd=req.getParameter("password");
if(pwd.equals("birju"))
{
chain.doFilter(req, resp);
}
else
{
pw.print("Invalid Password");
RequestDispatcher redi=req.getRequestDispatcher("index.html");
redi.include(req, resp);
}
}
@Override
public void destroy() {}
}
ServletFilter Code:
--------------------------------------------------------------------------------
import java.io.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletFilter extends HttpServlet
{
@Override
public void doGet(HttpServletRequest res,HttpServletResponse resp) throws IOException,ServletException
{
resp.setContentType("text/html;charset=UTF-8");
PrintWriter out = resp.getWriter();
String str = res.getParameter("name");
out.print("<h1>Hello "+str+"</h1><h2>You have sucessfully logged in....</h2>");
}
}
--------------------------------------------------------------------------------
import java.io.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletFilter extends HttpServlet
{
@Override
public void doGet(HttpServletRequest res,HttpServletResponse resp) throws IOException,ServletException
{
resp.setContentType("text/html;charset=UTF-8");
PrintWriter out = resp.getWriter();
String str = res.getParameter("name");
out.print("<h1>Hello "+str+"</h1><h2>You have sucessfully logged in....</h2>");
}
}
--------------------------------------------------------------------------------
web.xml Code:
--------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>ServletFilter</servlet-name>
<servlet-class>ServletFilter</servlet-class>
</servlet>
<welecome-file-list>
<welcome-file>index.html</welcome-file>
</welecome-file-list>
<servlet-mapping>
<servlet-name>ServletFilter</servlet-name>
<url-pattern>/filtering</url-pattern>
</servlet-mapping>
<filter>
<filter-name>f1</filter-name>
<filter-class>MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>f1</filter-name>
<url-pattern>/filtering</url-pattern>
</filter-mapping>
</web-app>
--------------------------------------------------------------------------------
Output:
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>ServletFilter</servlet-name>
<servlet-class>ServletFilter</servlet-class>
</servlet>
<welecome-file-list>
<welcome-file>index.html</welcome-file>
</welecome-file-list>
<servlet-mapping>
<servlet-name>ServletFilter</servlet-name>
<url-pattern>/filtering</url-pattern>
</servlet-mapping>
<filter>
<filter-name>f1</filter-name>
<filter-class>MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>f1</filter-name>
<url-pattern>/filtering</url-pattern>
</filter-mapping>
</web-app>
Output:
index.html
result
Sign up here with your email
ConversionConversion EmoticonEmoticon