Servlet设置简单的登录系统
Servlet+Session 设置简单的登录系统
- 首先创建一个登录jsp页面
1 <body> 2 <form action="${pageContext.request.contextPath }/loginServlet" name="actionForm" id="actionForm" method="post" > 3 <dl> 4 <dt>用户名:</dt> 5 <dd><input type="text" id="userCode" name="userCode"/> <span id="userCodeSpan"></span> </dd> 6 <dt>密 码:</dt> 7 <dd><input type="password" id="userPassword" name="userPassword"/><span id="userPasswordSpan"></span></dd> 8 </dl> 9 <div class="buttons"> 10 ${error } 11 <input type="button" value="登录系统" onclick="validate();" /> 12 <input type="reset" value="重 填" class="input-button" /> 13 </div> 14 </form> 15 16 17 </body>
2.创建一个Servlet类
public class LoginServlet extends HttpServlet { /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //获取用户名和密码 String name = request.getParameter("userCode"); String password = request.getParameter("userPassword"); //拿着信息去数据库查询是否存在 UserService us = new UserServiceImpl(); User user = new User(); user = us.getUserInfo(name, password); if(user!=null) { //登陆后设置用户session request.getSession().setAttribute("userSession", user); //如果数据库存在该用户就重定向到首页 response.sendRedirect("/news/jsp/admin/admin.jsp"); }else { response.sendRedirect("/news/jsp/login.jsp"); } } }
- 先从跳转页面获取name和password,然后去数据库查询信息是否存在
- 如果存在就跳转到登录的首页, 跳转之前先设置把用户信息设置进session里, 登录以后访问网站的其他页面不用在登录
- 如果不存在重定向回登录页面
3.写好severlet类后要在web.xml里配置一下 具体从跳转到找到你写的servlet类的过程如下 1.2.3.4
<servlet> <description></description> <display-name>LoginServlet</display-name> <servlet-name>LoginServlet</servlet-name>//3.根据名字找到这个类 <servlet-class>com.kgc.web.servlet.LoginServlet</servlet-class>//4.最后找到这个类所在的具体位置 </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name>//2.根据相对路径找到这个servlet名字 <url-pattern>/loginServlet</url-pattern>//1.提交表单时会定向到这个相对路径 </servlet-mapping>
4.如果存在下一步会重定向到response.sendRedirect("/news/jsp/admin/admin.jsp");这个路径,但是进入这个页面前会先经过过滤器
1 public class LoginFilter implements Filter { 2 3 public void doFilter(ServletRequest request, ServletResponse response, 4 FilterChain chain) throws IOException, ServletException { 5 //对登录进行验证 6 HttpServletRequest rq = (HttpServletRequest)request; 7 HttpServletResponse rp = (HttpServletResponse)response; 8 User userSession = (User)rq.getSession().getAttribute("userSession"); 9 if(null == userSession){ 10 rp.sendRedirect("/news/jsp/error.jsp"); 11 }else{ 12 chain.doFilter(request, response); 13 } 14 } 15 16 public void destroy() { 17 18 } 19 20 public void init(FilterConfig arg0) throws ServletException { 21 22 } 23 24 25 }
过滤器会先检查session中user是否为空,如果为空将会重定向到登录页面,如果不为空将会chain.doFilter(request, response);继续传递下去
过滤器在web.xml中配置如下
1 <filter> 2 <filter-name>loginFilter</filter-name> 3 <filter-class>com.kgc.web.filter.LoginFilter</filter-class> 4 </filter> 5 <filter-mapping> 6 <filter-name>loginFilter</filter-name> 7 <url-pattern>/jsp/admin/*</url-pattern>//设置过滤的范围,admin下的所有jsp页面 8 </filter-mapping>