过滤器Servlet类:
package servlet;
import java.io.IOException;
//import java.io.PrintWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
//import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginFilter implements Filter {
private static final String LOGON_URI = "LOGON_URI";
private static final String REGISTER_URI = "REGISTER_URI";
private String logon_page;
private String register_page;
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
resp.setContentType("text/html;");
resp.setCharacterEncoding("utf-8");
HttpSession session = req.getSession();
// PrintWriter out = resp.getWriter();
// 得到用户请求的URI
String request_uri = req.getRequestURI();
// 得到web应用程序的上下文路径
String ctxPath = req.getContextPath();
// 去除上下文路径,得到剩余部分的路径
String uri = request_uri.substring(ctxPath.length());
// 判断用户访问的是否是登录页面
if (uri.equals(logon_page) || uri.equals(register_page)) {
chain.doFilter(request, response);
return;
} else {
// 如果访问的不是登录页面,则判断用户是否已经登录
if (null != session.getAttribute("curUser")
&& "" != session.getAttribute("curUser"))
{
chain.doFilter(request, response);
return;
}
else {
String msg = "您未登录!";
String url = "Errorer";
req.setAttribute("err", msg);
RequestDispatcher dis = req.getRequestDispatcher(url);
dis.forward(req, resp);
}
//out.println("<script language=/"javaScript/">"
// + "parent.location.href='" +
//ctxPath + logon_page + "'"
// + "</script>");
// return;
// }
}
}
public void init(FilterConfig config) throws ServletException {
// TODO Auto-generated method stub
// 从部署描述符中获取登录页面和首页的URI
logon_page = config.getInitParameter(LOGON_URI);
register_page = config.getInitParameter(REGISTER_URI);
// System.out.println(logon_page);
if (null == logon_page || null == register_page) {
throw new ServletException("没有找到登录页面或主页");
}
}
}
修改Web核心配置文件:
注:保存后需要重启tomcat服务器
<filter>
<filter-name>loginFilter</filter-name>
<filter-class>servlet.LoginFilter</filter-class>
<init-param>
<param-name>LOGON_URI</param-name>
<param-value>/login.jsp</param-value>
</init-param>
<init-param>
<param-name>REGISTER_URI</param-name>
<param-value>/register.jsp</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>charseterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>loginFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
Errorer错误信息处理类:
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Error
*/
public class Errorer extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String url="login.jsp";
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Error</title>");
out.println("</head>");
out.println("<body>");
out.println("<h2 align=center>" + request.getAttribute("err") + "</h2>");
response.setHeader("refresh", "2;url='login.jsp'");
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write("请登录!页面将在3秒后自动跳转,如果没有,请点击<a href='login.jsp'>回到登录页面</a>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
RequestDispatcher dis = request.getRequestDispatcher(url);
dis.forward(request, response);
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
效果展示: