时间:2021-05-19
UserDao.java从数据库中查询用户名与密码
//登录public User login(User user) throws SQLException {QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());String sql = "select from user where username = ? and password = ?";return qr.query(sql, new BeanHandler<User>(User.class),user.getUsername(),user.getPassword());}UserService.javapublic User login(User user){try {return ud.login(user);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}UserServlet.java实现登录功能
//登录public void login(HttpServletRequest request,HttpServletResponse response) throws IOException, IllegalAccessException, InvocationTargetException, ServletException{Map<String,String[]> map = request.getParameterMap();User user = new User();BeanUtils.populate(user,map);if (map.get("autoLogin")!=null){Cookie username = new Cookie("username", map.get("username")[0]);username.setMaxAge(6060);Cookie password = new Cookie("password", map.get("password")[0]);password.setMaxAge(60*60);response.addCookie(username);response.addCookie(password);}user = us.login(user);if (user != null){request.getSession().setAttribute("user", user);response.sendRedirect("/ShopStore/default.jsp");}else{request.setAttribute("message", "用户或密码错误!");request.getRequestDispatcher("/login.jsp").forward(request, response);}}工具类:AutoLoginFilter.java用来实现自动登录
package com.yinhe.web.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.Cookie;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.yinhe.bean.User;import com.yinhe.service.UserService;public class AutoLoginFilter implements Filter{br/>@Overridepublic void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {HttpServletRequest req = (HttpServletRequest)request;HttpServletResponse resp = (HttpServletResponse)response;//是否已登录if (req.getSession().getAttribute("user") == null){//如果已登录,则啥都不干//判断cookie中有没有账户密码Cookie[] cookies = req.getCookies();if (cookies != null){String username = "";String userpass = "";for (Cookie cookie : cookies) {if (cookie.getName().equals("username")){//找到感兴趣的cookieusername = cookie.getValue();}if (cookie.getName().equals("password")){//找到感兴趣的cookieuserpass = cookie.getValue();}}UserService us = new UserService();User user = new User();user.setUsername(username);user.setPassword(userpass);if (us.login(user) != null){req.getSession().setAttribute("user", user);}}}chain.doFilter(request, response);br/>}@Overridepublic void init(FilterConfig filterConfig) throws ServletException {// TODO Auto-generated method stubbr/>}@Overridepublic void destroy() {// TODO Auto-generated method stub}}前台:login.jsp此单选框被选中下次访问即为自动登录
主页内容
<a href="info.jsp" rel="external nofollow" >Python——画一棵漂亮的樱花树(不同种樱花+玫瑰+圣诞树喔)</a></div></body></html>login.jsp<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head><title>登录</title><style>tr{height:50px;}td{text-align: center;}</style></head><body><%String msg = (String)request.getAttribute("msg");%><%if(msg != null){out.print(msg);}%><form action="myServlet02" method="get"><h1 align="center">登录</h1><table width="500" border="1" cellspacing="0" cellpadding="0" align="center"><tr><td>账号:</td><td><input type="text" name="username" /></td></tr><tr><td>密码:</td><td><input type="password" name="password" /></td></tr><tr><td colspan="2"><input type="submit" value="登录" /></td></tr></table></form></body></html>info.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head><title>Title</title><style>#nav{height: 88px;padding: 5px 200px;background-color: aquamarine;}#logo{float: left;}#userinfo{float: right;height: 50px;line-height: 80px;}#container{background-color: aqua;height: 800px;margin: 2px 200px;font-size: xx-large;text-align: center;}</style></head><body><div id="nav"><div id="logo"><img src="csdn-logo.png" width="180" height="88"></div><div id="userinfo"><%String loginUser = (String) session.getAttribute("loginUser");if (loginUser != null){out.println(loginUser);out.println("<a href='logoutServlet'>退出</a>");}else {%><a href="login.jsp" rel="external nofollow" rel="external nofollow" rel="external nofollow" >登录</a><a>/注册</a><%}%></div></div><div id="container">主页内容</div></body></html>Servlet02.java
import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import java.io.IOException;br/>@WebServlet("/myServlet02")public class Servlet02 extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//修改编码request.setCharacterEncoding("utf-8");//请求过来的编码是亨达返佣e-file-list></web-app>总结
以上所述是小编给大家介绍的使用Java servlet实现自动登录退出功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
java实现银行ATM自助取款机,实现功能:用户登录、余额查询、存钱、取钱、转账、修改密码、退出系统。用java实现一个ATM机系统(2.0版)设计思路设计五个
本文实例讲述了Yii2框架实现登录、退出及自动登录功能的方法。分享给大家供大家参考,具体如下:自动登录的原理很简单。主要就是利用cookie来实现的在第一次登录
Java模拟ATM机,供大家参考,具体内容如下实现登录,查询,转账,取款,修改密码,退出功能。源码packagebank;importjava.io.*;imp
Java实现随机验证码功能简单实例现在许多系统的注册、登录或者发布信息模块都添加的随机码功能,就是为了避免自动注册程序或者自动发布程序的使用。验证码实际上就是随
本文实例讲述了Java实现JSP使用Servelt连接Oracle数据库的方法。为了实现在Servlet中连接数据库,必须编写Servlet的类程序。将Serv