struts2filterweb.xmlrequestdispatcherstruts-action

How to call struts action from doFilter method


My struts.xml has two actions as follows

<action name="dologincheck" class="com.platinum.uac.biz.LoginAction" method="doLogin">
    <result name="success" type="tiles">Homepage</result>
    <!--   <result name="success">/Profile/view/ViewProfile1.jsp</result>-->
    <result name="none">/Profile/add/Failure.jsp</result>              
    <result name="mail">/Profile/add/EmailAuthentication.jsp</result>
</action>
<action name="newuseraction" class="com.platinum.uac.biz.NewUserAction" method="newuser">
    <result name="success" >/Profile/add/ProfileDetails.jsp</result>
    <result name="error" >/index.jsp</result>
</action>

I need to call an action(dologincheck) from doFilter method. I am stuck here because I do not know how to call an action from doFilter method

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws   
      IOException, ServletException {
    try {
    System.out.println("this is do filter before the jsp page");
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = ((HttpServletResponse) res);
    HttpSession session = request.getSession(true);
    String username, password;
    Cookie[] cookies;
    LoginPojo user = (LoginPojo) session.getAttribute("username");
    if (user == null) {
        cookies = request.getCookies();
        if (cookies != null && cookies.length > 0) {
            username = getCookieValue(cookies, "username");
            password = getCookieValue(cookies, "password");
            System.out.println(username);
            System.out.println(password);
            if (username != null && password != null) {
                LoginPojo pojo = new LoginPojo(username, password);
                results = dao.Login(pojo);
                 session.setAttribute("userName", user); // usersession?

               request.getRequestDispatcher("dologincheck").forward(request,
                response);
            } else {
            request.getRequestDispatcher("/index.jsp").forward(request,
            response);
            }
        } else {
        request.getRequestDispatcher("/index.jsp").forward(request,
        response);
        }
    }
    } catch (Exception e) {
    }
    chain.doFilter(req, res);
    //throw new UnsupportedOperationException("Not supported yet.");
}

My Filter part of web.xml is

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
    <filter-name>AutomaticLogin</filter-name>
    <filter-class>com.platinum.uac.biz.AutomaticLogin</filter-class>
</filter>
<filter-mapping>
    <filter-name>AutomaticLogin</filter-name>
    <url-pattern>*.jsp</url-pattern>
    <url-pattern>*.action</url-pattern>
    <servlet-name>action</servlet-name>  
    <dispatcher>REQUEST</dispatcher>
</filter-mapping>

Solution

  • After searching in net I got the answer.

    Use

    response.sendRedirect("dologincheck");
    

    instead of

    request.getRequestDispatcher("dologincheck").forward(request, response);