我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

Java中怎么利用servlet实现自动登录退出功能

本篇文章给大家分享的是有关Java中怎么利用servlet实现自动登录退出功能,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

成都创新互联于2013年开始,是专业互联网技术服务公司,拥有项目成都网站设计、成都做网站网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元蛟河做网站,已为上家服务,为蛟河各地企业和个人服务,联系电话:18982081108

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.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 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此单选框被选中下次访问即为自动登录

 

<%@ page contentType="text/html;charset=UTF-8" language="java" %>系统首页

<%-- 不推荐使用<%String loginUser = (String) request.getAttribute("loginUser");if (loginUser != null){out.println(loginUser);}else {%>登录/注册<%}%>--%><%String loginUser = (String) session.getAttribute("loginUser");if (loginUser != null){out.println(loginUser);out.println("退出");}else {%>登录/注册<%}%>

主页内容

Python——画一棵漂亮的樱花树(不同种樱花+玫瑰+圣诞树喔)

login.jsp<%@ page contentType="text/html;charset=UTF-8" language="java" %>登录<%String msg = (String)request.getAttribute("msg");%><%if(msg != null){out.print(msg);}%>

登录

账号:
密码:

info.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>Title

<%String loginUser = (String) session.getAttribute("loginUser");if (loginUser != null){out.println(loginUser);out.println("退出");}else {%>登录/注册<%}%>

主页内容

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");//请求过来的编码是亨达返佣www.kaifx.cn/broker/hantecglobal.htmlutf-8response.setContentType("text/html;charset=utf-8");//响应出去的内容,为网页编码utf-8//获取表单数据String username = request.getParameter("username");String password = request.getParameter("password");//验证if("aaa".equals(username) && "123".equals(password)){//跳转(请求转发 请求重定向)//重定向:两次请求,不能在request作用域中共享数据。//如果要在两次请求或多次请求之间,进行数据共享,需要用session//使用session步骤//获取sessionHttpSession session = request.getSession();//如果存在已有的session,则直接返回,否则会创建一个新的,返回。//HttpSession session = request.getSession(true);//同上//HttpSession session = request.getSession(false);//如果存在已有的session,则直接返回,否则返回null。//在session的作用域保存数据,供后续请求使用session.setAttribute("loginUser",username);response.sendRedirect("index.jsp");/ 多个页面不推荐使用请求转发request.setAttribute("loginUser",username);request.getRequestDispatcher("index.jsp").forward(request,response);/}else {//转发:一次请求,可以在request作用域中,共享数据request.setAttribute("msg","");// response.sendRedirect("login.jsp");request.getRequestDispatcher("login.jsp").forward(request,response);}}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request, response);}}

LogoutServlet.java

import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import java.io.IOException;br/>@WebServlet("/logoutServlet")public class LogoutServlet extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//退出系统//获取sessionHttpSession session = request.getSession();//删除sessionsession.removeAttribute("loginUser");//跳转到登录页面/首页response.sendRedirect("index.jsp");}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request, response);}}

web.xml

index.jsp

以上就是Java中怎么利用servlet实现自动登录退出功能,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。


网站名称:Java中怎么利用servlet实现自动登录退出功能
URL链接:http://mswzjz.cn/article/jcgcsc.html

其他资讯