时间:2021-05-20
本文实例讲述了Java Web基于Session的登录实现方法。分享给大家供大家参考,具体如下:
package cn.com.login;import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class Login extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); String userName=request.getParameter("userName"); String password=request.getParameter("password"); PrintWriter out=response.getWriter(); List<User> list=Db.getAll(); for(User user:list) { if(user.getUserName().equals(userName)&&user.getPassword().equals(password)) { request.getSession().setAttribute("user", user); response.sendRedirect("/Session/index.jsp"); return ; } } out.write("用户名或者密码错误!"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); }}class Db{ public static List<User> list=new ArrayList(); static { list.add(new User("aaa","123")); list.add(new User("bbb","123")); list.add(new User("ccc","123")); } public static List<User> getAll() { return list; }}package cn.com.login;public class User { private String userName; private String password; public User() { super(); // TODO Auto-generated constructor stub } public User(String userName, String password) { super(); this.userName = userName; this.password = password; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }}package cn.com.login;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;/** * Servlet implementation class LogOut */public class LogOut extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session=request.getSession(false); if(session==null) { response.sendRedirect("/Session/index.jsp"); return ; } session.removeAttribute("user"); response.sendRedirect("/Session/index.jsp"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); }}<!DOCTYPE html><html> <head> <title>Index.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> <form action="/Session/Login"> 用户名:<input type="text" name="userName"/><br/> 密码:<input type="password" name="password"/><br/> <input type="submit" value="登录" name="login"/> </form> </body></html>希望本文所述对大家Java web程序设计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了PHP实现分布式memcache设置web集群session同步的方法。php的session默认是文件存储:session.save_handl
在Web开发中我们经常需要实现定时刷新某个页面:1.来保持session的值或者检查session的值是否为空(比如说防止同一用户重复登录);2.实现实时站内短
本文实例讲述了java设置session过期时间的实现方法,分享给大家供大家参考。具体实现方法如下:1、Timeoutinthedeploymentdescri
本文实例讲述了ThinkPHP自定义Redis处理SESSION的实现方法。分享给大家供大家参考,具体如下:日常中我们都会使用到session来保存用户登录的信
文档概览本文基于express、express-session实现了简易的登录/登出功能,完整的代码示例可以在这里找到。环境初始化首先,初始化项目express