LoginFilter:
- Java code
package com.zoe.filter;import java.io.IOException;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;public class LoginFilter implements Filter{ public void destroy() { // TODO Auto-generated method stub } public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; request.setCharacterEncoding("utf-8"); HttpSession session = request.getSession(); //session.setAttribute("name", "s"); System.out.println(session.getAttribute("name")); if(session.getAttribute("name")!=null){ chain.doFilter(req, res); }else{ request.getRequestDispatcher("../login.jsp").forward(req, res); } } public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub }}
LoginServlet:
- Java code
package com.zoe.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import com.zoe.dao.UserDao;import com.zoe.entity.User;public class LoginServlet extends HttpServlet { /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); String name = request.getParameter("name"); String pwd = request.getParameter("pwd"); User u = new User(); u.setName(name); u.setPwd(pwd); HttpSession session = request.getSession(); boolean flag = new UserDao().login(u); if(flag){ session.setAttribute("name", name); request.getRequestDispatcher("admin/admin.jsp").forward(request, response); }else{ response.sendRedirect("error.jsp"); } out.close(); }}