Redirect All the Pages in Home Page

后端 未结 1 1432
时光说笑
时光说笑 2021-01-27 17:46

I have a web application which includes few jsp pages. And my home page is welcome.jsp And the Application url is like www.test.com

So, whenever a user hit the url (www.

1条回答
  •  星月不相逢
    2021-01-27 18:18

    You can add the following mapping to your web.xml:

    
        welcome
        welcome.jsp
    
    
    
        welcome
        *.jsp
    
    

    This will map all requests for a .jsp file to welcome.jsp.

    Edit:

    If you want to only redirect the users if they haven't already been to the welcome jsp, don't use the code above in your web.xml file. Instead in your jsp set a flag on the user's session in welcome.jsp:

    
    

    Then add create Filter to redirect them like this one RedirectFilter.java:

    @WebFilter("*.jsp")
    public class RedirectFilter implements Filter {
    
    public void destroy() {}
    public void init(FilterConfig fConfig) throws ServletException {}
    
    /**
     * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
     */
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    
        Object sessionStarted = ((HttpServletRequest)request).getSession(true).getAttribute("sessionStarted");
        if(sessionStarted==null){
            request.getServletContext().getRequestDispatcher("welcome.jsp").forward(request, response);
        }else{
            chain.doFilter(request, response);
        }
    }
    }
    

    0 讨论(0)
提交回复
热议问题