Java Servlet Programing Laungage

Java Servlet Projects

Servlet Project 3

adplus-dvertising
Create an application for accessing AuditingFilter in Servlet
Previous Home Next

In this program, an AuditingFilter is used for checking that user have valid session and valid authorization. Steps involve into create filter class and mapping into web.xml file. click here

Application directory structure

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
  xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 <filter>
  <filter-name>AuditFilter</filter-name>
  <filter-class>r4r.Filter.AuditFilter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>AuditFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <servlet>
  <servlet-name>AuditServlet</servlet-name>
  <servlet-class>r4r.Filter.AuditServlet</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>AuditServlet</servlet-name>
  <url-pattern>/AuditServlet</url-pattern>
 </servlet-mapping>
 <session-config>
  <session-timeout>
     30
  </session-timeout>
 </session-config>
</web-app>

Index.jsp

<%-- 
 Document: index.jsp
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
 <head>
  <meta http-equiv="Content-Type" 
       content="text/html; charset=UTF-8">
  <title>r4r.co.in-index</title>
 </head>
 <body>
  <h1>Auditing Filter!R4R</h1>
  <form method="POST">
   <!-- get value from session -->
  <p>Current Session Active for: <b>${AUTH_USER}</b>|
  <a href="<% request.getSession().invalidate();%>">
          Invalid Current Session</a></p>
   <a href="AuditServlet">Forward to Login Page</a>
  </form>
 </body>
</html>

Filter Program

/*
 * Save as a AuditFilter.java
 */
package r4r.Filter;

import java.io.IOException;
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.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *
 * @author R4R
 */
public class AuditFilter implements Filter {

 private static final boolean debug = true;
 private FilterConfig filterConfig = null;

 //default constructor
 public AuditFilter() {
 }

 /**
  * Init method for this filter 
  */
 @Override
 public void init(FilterConfig filterConfig) {
  this.filterConfig = filterConfig;
  if (filterConfig != null) {
if (debug) {
 log("AuditFilter:Initializing filter");
}
  }
 }

 /**
  * doFilter method for this filter
  */
 @Override
 public void doFilter(ServletRequest request,
	 ServletResponse response, FilterChain chain)
throws IOException, ServletException {

  if (debug) {
log("AuditFilter:doFilter()");
  }
  chain.doFilter(request, response);
  if (request instanceof HttpServletRequest) {
HttpSession session = 
	((HttpServletRequest) request).getSession();
log("Session: " + session);
// get session attribute value
Object validUser = session.getAttribute("AUTH_USER");
log("Session Value: " + validUser);
if (validUser != null) {
} else {
 request.getRequestDispatcher
	     ("AuditServlet").forward(request, response);
 return;
}
  }

 }

 /**
  * Return the filter configuration object for this filter.
  */
 public FilterConfig getFilterConfig() {
  return (this.filterConfig);
 }

 /**
  * Set the filter configuration object for this filter.
  */
 public void setFilterConfig(FilterConfig filterConfig) {
  this.filterConfig = filterConfig;
 }

 /**
  * Destroy method for this filter 
  */
 @Override
 public void destroy() {
  filterConfig = null;
 }

 /**
  * log method for print msg over server console
  */
 public void log(String msg) {
  filterConfig.getServletContext().log(msg);
 }
}

Servlet Program

/*
 * Save as a AuditServlet.java
 */
package r4r.Filter;

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;
import javax.servlet.http.HttpSession;

/**
 *
 * @author R4R
 */
public class AuditServlet extends HttpServlet {

 protected void processRequest(HttpServletRequest request,
	 HttpServletResponse response)
throws ServletException, IOException {
  response.setContentType("text/html;charset=UTF-8");
  PrintWriter out = response.getWriter();
  try {
/* TODO output your page here */
out.println("<html>");
out.println("<head>");
out.println("<title>" + getServletInfo() + "</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet AuditServlet at " 
	            + request.getContextPath() + "</h1>");
out.println("<form action=\"AuditServlet\" method=\"POST\">");
out.println("Username:<input type=\"text\" name=\
           "user\" value=\"\" size=\"20\" maxlength=\"10\"/><br/>");
out.println("Password:<input type=\"password\" name=\
           "pass\" value=\"\" size=\"20\" maxlength=\"10\"/><br/>");
out.println("<input type=\"submit\" value=\"Login Me\" />");
out.println("<input type=\"reset\" value=\"Reset\" />");
out.println("</form>");
out.println("<p>Username|password= admin|admin</p>");
out.println("</body>");
out.println("</html>");
  } finally {
out.close();
  }
 }

 @Override
 protected void doGet(HttpServletRequest request,
	 HttpServletResponse response)
throws ServletException, IOException {
  processRequest(request, response);

 }

 @Override
 protected void doPost(HttpServletRequest request,
	 HttpServletResponse response)
throws ServletException, IOException {
  response.setContentType("text/html;charset=UTF-8");
  PrintWriter out = response.getWriter();

  //Get textField value from html page
  String username = request.getParameter("user");
  String password = request.getParameter("pass");
  try {
if (!username.equals("") || !password.equals("")) {
 // username="admin" and password="admin" (hardcore value)
 if (username.equals("admin") && password.equals("admin")) {
  //Create a new session and store some value in it. 
  HttpSession session = request.getSession(true);
  session.setAttribute("AUTH_USER", username);
  request.getRequestDispatcher("index.jsp").forward(request, response);
 } else {
  out.println("Username and Password doesn't match!");
  out.println("<a href=\"loginPage.jsp\">Return to LoginPage</a>");
 }
} else {
 out.println("Don't leave text field empty!");
 out.println("<a href=\"loginPage.jsp\">Return to loginPage</a>");
}
  } finally {
out.flush();
out.close();
  }
 }

 @Override
 public String getServletInfo() {
  return "r4r.co.in-AuditServlet";
 }
}
Output of Program
Previous Home Next