Results 1 to 1 of 1
  1. #1
    raj87ng is offline Member
    Join Date
    Mar 2014
    Rep Power

    Default Session check by Filter and redirect page to index.jsp

    HI All,
    i have implemented session check in filter and redirect to index.jsp when session is null.
    After successfull login it is redirecting to login.jsp but it not showing body contents in web browser and not getting why ?

    My App code as :

    1. web.xml
    Java Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" 

    2. index.jsp

    Java Code:
      <form name="login" method="post" action="LoginTest" >
     Username<input name="sapId" type="text" />
      Password<input name="password" type="text" />
      <input type="submit" name="submit" value="Submit" />"

    Java Code:
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    		String username= request.getParameter("sapId");
    		String password =request.getParameter("password");
    		if(username == null  || username==""){
    		if(password == null  || password==""){
    		System.out.println("Username is =" +username);
    		System.out.println("Password is =" + password);
    		if("rajat".equals(username) && "rajat".equals(password))
    			HttpSession session = request.getSession(false);
    			if(session != null){
    			session =request.getSession();
    			String sessionId = session.getId();
    			System.out.println("sessionId " + sessionId);
    Java Code:
    package login;
    import java.util.ArrayList;
    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.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    public class LoginFilter implements Filter{
    	private ArrayList<String> urlList=null;
    	public void destroy() {
    	public void doFilter(ServletRequest req, ServletResponse resp,
    			FilterChain fchain) throws IOException, ServletException {
    		HttpServletRequest request = (HttpServletRequest) req;
            HttpServletResponse response = (HttpServletResponse) resp;
            String url = request.getServletPath();
            System.out.println("URL is in doFilter = "+url);
            boolean allowedRequest = false;
            if(urlList.contains(url)) {
                allowedRequest = true;
            if (!allowedRequest) {
                HttpSession session = request.getSession(false);
                if (null == session || session.getAttribute("currentUser")==null) {
                	System.out.println("session not set");
            else {
            fchain.doFilter(req, resp);
    	public void init(FilterConfig fconfig) throws ServletException {
    		String urls = fconfig.getInitParameter("avoid_urls");
            StringTokenizer token = new StringTokenizer(urls, ",");
            System.out.println("urls are : "+urls);
            urlList = new ArrayList<String>();
            while (token.hasMoreTokens()) {

    Java Code:
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>login page</title>
    String name=null;
    String ID=null;
    name = (String)session.getAttribute("currentUser");
    ID = (String)session.getId(); 
    System.out.println("name is = :"+name);
    User Name is = <%=name%> <br/>
    Session ID is = <%=ID %>
    above code works like when somebody enters whole URL of any page without session set , it will redirect to index.jsp and after successfull login it will show user name and session Id in browser.
    but now after login it is not showing name and ID in browser.
    Please gime me some solution.
    Last edited by raj87ng; 03-19-2014 at 11:06 AM.

Similar Threads

  1. How to redirect to thank you page
    By rashriaz in forum New To Java
    Replies: 4
    Last Post: 03-07-2012, 09:27 AM
  2. redirect to a servlet page
    By niteangell21 in forum New To Java
    Replies: 5
    Last Post: 11-15-2010, 03:53 PM
  3. Redirect To Same Page
    By systech44 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 12-08-2009, 12:27 PM
  4. redirect to html page
    By simo_mon in forum Java Servlet
    Replies: 3
    Last Post: 02-12-2009, 06:18 PM
  5. Redirect page in web service
    By bluesheeva in forum Advanced Java
    Replies: 3
    Last Post: 02-04-2009, 12:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts