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

    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"
        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_2_5.xsd">
        <display-name>TestPOC</display-name>
         <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
        <filter>
        <filter-name>LoginFilter</filter-name>
        <filter-class>login.LoginFilter</filter-class>
         
        <init-param>
            <param-name>avoid_urls</param-name>
            <param-value>/index.jsp</param-value>
        </init-param>
     
    </filter>
    <filter-mapping>
        <filter-name>LoginFilter</filter-name>
        <url-pattern>*.jsp</url-pattern>
    </filter-mapping>
         
      <servlet>
     
        <servlet-name>LoginTest</servlet-name>
        <servlet-class>login.LoginTest</servlet-class>
      </servlet>
     
      <servlet-mapping>
        <servlet-name>LoginTest</servlet-name>
        <url-pattern>/LoginTest</url-pattern>
      </servlet-mapping>
      
    </web-app>
    2. index.jsp
    Java Code:
    <body>
     
      <form name="login" method="post" action="LoginTest" >
     Username<input name="sapId" type="text" />
    <br/>
      Password<input name="password" type="text" />
      <br/>
      <input type="submit" name="submit" value="Submit" />"
     </form>
       
      
    </body>
    3. LoginTest.java
    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==""){
                response.sendRedirect("index.jsp");
                return;
            }
            if(password == null  || password==""){
                response.sendRedirect("index.jsp");
                return;
            }
            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.invalidate();
                }
                session =request.getSession();
                session.setAttribute("currentUser",username);
                 
                String sessionId = session.getId();
                System.out.println("sessionId " + sessionId);
                response.sendRedirect("login.jsp");
                //return;
            }
        }
    4. LoginFilter.java
    Java Code:
    package login;
     
    import java.io.IOException;
    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");
                    response.sendRedirect("index.jsp");
                    //return;
                }
            }
            else {
            fchain.doFilter(req, resp);
             return;
            }
            
        }
     
        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()) {
                urlList.add(token.nextToken());
      
            }
             
        }
     
    }
    5.login.jsp
    Java Code:
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>login page</title>
    </head>
    <%
    String name=null;
    String ID=null;
     
    name = (String)session.getAttribute("currentUser");
    ID = (String)session.getId();
    System.out.println("name is = :"+name);
    %>
    <body>
     
    User Name is = <%=name%> <br/>
    Session ID is = <%=ID %>
     
     
    </body>
    </html>
    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.

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    835
    Rep Power
    6

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

    create a session with true as a parameter and check
    Ramya:cool:

Similar Threads

  1. Replies: 0
    Last Post: 03-19-2014, 09:58 AM
  2. How to redirect to thank you page
    By rashriaz in forum New To Java
    Replies: 4
    Last Post: 03-07-2012, 08:27 AM
  3. Redirect To Same Page
    By systech44 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 12-08-2009, 11:27 AM
  4. Redirect page in web service
    By bluesheeva in forum Advanced Java
    Replies: 3
    Last Post: 02-03-2009, 11:56 PM

Tags for this Thread

Posting Permissions

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