如何在ECLIPSE的所有jsp页面上编写几行代码

时间:2018-07-31 14:48:41

标签: eclipse jsp

我在Eclipse中有一个超过200页的jsp项目。我必须在所有页面中实现一些与会话相关的参数和其他jsp参数。

我必须写几行jsp代码示例:

<% if(request.getSession().getAttribute("username")==null)
   response.sendRedirect("index.jsp"); %>

我大约有200页。

如何在所有页面中编写以上代码??

1 个答案:

答案 0 :(得分:0)

servlet中的实现过滤器

/**
 * @author Sumesh
 * 
 * This class will handle all the incoming request to server
 * thus it will handle authentication & session timeout
 * 
 *
 */
public class AuthenticationFilter implements Filter {
    private FilterConfig filterConfig;

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
        try {
            HttpServletRequest httpRequest = (HttpServletRequest) request;
            //System.out.print("Within AuthenticationFilter ... " + httpRequest.getHeader("sessionId"));
            //System.out.print("\nSession id AuthenticationFilter ... " + httpRequest.getSession().getId());
            String uri = httpRequest.getRequestURI();
            boolean isRest=!uri.contains("servlet");
            boolean isLogin=uri.contains("login");
            if(isRest || isLogin ){
                chain.doFilter(request, response);
            }else{
                if (httpRequest.getRequestedSessionId() != null && !httpRequest.isRequestedSessionIdValid()) {
                    // The session has been expired (or a hacker supplied a fake cookie).
                    System.out.println("Session expired");
                    String json="{\"errorCode\":\"107\"}";
                    JSONObject obj = new JSONObject();
                    obj.put("errorCode", "107");
                    HttpServletResponse httpResponse = (HttpServletResponse) response;
                    httpResponse.setContentType("application/json");
                    httpResponse.setHeader("Cache-Control", "no-cache");
                    httpResponse.setHeader("X-JSON", json);
                    PrintWriter out = response.getWriter();
                    out.print(obj);
                    out.flush();
                }else{
                    System.out.println("Session exists");
                    HttpSession session=httpRequest.getSession(false);
                    if(session!=null){
                        SSWSessionInfo sessionData = (SSWSessionInfo) session.getAttribute("sessionInfo"); // Object stored in session
                        if(sessionData!=null){
                            System.out.println("Logged in user :: "+sessionData.getUserID());
                            chain.doFilter(request, response);
                        }else{
                            System.out.println("Session expired");
                            String json="{\"errorCode\":\"107\"}";
                            HttpServletResponse httpResponse = (HttpServletResponse) response;
                            httpResponse.setContentType("application/json");
                            httpResponse.setHeader("Cache-Control", "no-cache");
                            httpResponse.setHeader("X-JSON", json);
                            PrintWriter out = response.getWriter();
                            out.print(json);
                            out.flush();
                        }
                    }

                }
            }

        } catch (IOException io) {
            System.out.println("IOException raised in AuthenticationFilter");
        } catch (ServletException se) {
            System.out.println("ServletException raised in AuthenticationFilter");
        }
    }

    public FilterConfig getFilterConfig() {
        return this.filterConfig;
    }

    public void setFilterConfig(FilterConfig filterConfig) {
        this.filterConfig = filterConfig;
    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
        // TODO Auto-generated method stub
        System.out.println("AuthenticationFilter initialized");
    }
}

然后,您必须在 web.xml

中添加过滤器映射
 <filter>
        <filter-name>authfilter</filter-name>
        <filter-class>AuthenticationFilter</filter-class>
</filter>
<filter-mapping>
        <filter-name>authfilter</filter-name>
        <url-pattern>/*</url-pattern>
</filter-mapping> 

这里我将会话超时响应用作json。如果您要页面重定向,请在此处进行。