Click here to Skip to main content
15,883,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys,

I'm doing a java web project. In my project to access all users must login to he system. once user logged he can go to other pages. so i want to check every request using 'filter' in java(apply filter to every request). can anyone give me steps to do that?
Here my Filter.java file code.I did not complete it yet. need steps to continue. if there are any mistakes please tell me. I'm appreciating them.

Java
package com.mobitel.bankdemo.web;

import java.io.IOException;
import java.util.logging.Filter;
import java.util.logging.LogRecord;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/FilterRequest")
public class FilterRequest extends HttpServlet implements Filter{
	private static final long serialVersionUID = 1L;
	FilterConfig filterConfig = null;
	
    public FilterRequest() {
        super();
        // TODO Auto-generated constructor stub
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	public void init(FilterConfig filterConfig) throws ServletException {
		this.filterConfig = filterConfig;
	}
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
		HttpServletRequest httpReq = (HttpServletRequest) req;
		String user = httpReq.getRemoteUser();
		if (user!= null) {
			httpReq.getRequestURI();
			filterConfig.getServletContext().log(user);
		}
		else{
			String message = "Please Login!";				
			req.setAttribute("loginMsg", message);
			RequestDispatcher rd = getServletContext().getRequestDispatcher("/login2.jsp");
			rd.forward(req, resp); 
		}
		chain.doFilter(req, resp);//request pass
	}
	@Override
	public boolean isLoggable(LogRecord arg0) {
		// TODO Auto-generated method stub
		return false;
	}
	public void destroy() {
		// do cleanup stuff
		//filterConfig = null;
		}
}


thank you in Advance!
Posted

1 solution

To create filter
Java
public class LogFilter implements Filter {...}

Only implements the Filter , no need to extends the Servlet

Once you create a filter map the filter defination in web. xml file .

To learn more
http://viralpatel.net/blogs/tutorial-java-servlet-filter-example-using-eclipse-apache-tomcat/[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900