首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

如何用filterServlet判断Session是否登录

2013-10-22 
怎么用filterServlet判断Session是否登录?webapps\???????\index.jsp???????\common???????.......???????

怎么用filterServlet判断Session是否登录?

webapps\
???????\index.jsp
???????\common
???????.......
???????\文件夹
根目录只留有一个index.jsp登录用
不过滤掉
过滤相关的文件夹
如:
<filter-mapping>
????<filter-name>hello</filter-name>
????<url-pattern>/common/*</url-pattern>
</filter-mappint>
<filter-mapping>
????<filter-name>hello</filter-name>
????<url-pattern>/xxx/*</url-pattern>
</filter-mappint>

?

import java.io.*;import javax.servlet.*;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;//设置servlet过滤代码段public class SessionFilter implements Filter {protected FilterConfig filterConfig;// 初始化public void init(FilterConfig filterConfig) throws ServletException {this.filterConfig = filterConfig;}public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws java.io.IOException, ServletException {HttpServletRequest httprequest = (HttpServletRequest) request;HttpSession session = httprequest.getSession();HttpServletResponse httpresponse = (HttpServletResponse) response;String UserId = (String) session.getAttribute("UserId");if (UserId == null) {System.out.println("filter:session is null ");/* * httpresponse.sendRedirect(httprequest.getContextPath() + * "/Hello.do"); */httpresponse.setContentType("text/html; charset=UTF-8");PrintWriter out = httpresponse.getWriter();String strURL = "<script   language="javascript">";strURL += "window.open('" + httprequest.getContextPath()+ "/index.jsp" + "','_parent')";strURL += "</script>";out.println(strURL);return;}chain.doFilter(request, response);}public void destroy() {this.filterConfig = null;}}

?

热点排行