Java Web基于Session的登錄實(shí)現(xiàn)方法
更新時(shí)間:2015年10月19日 14:39:14 作者:煙大洋仔
這篇文章主要介紹了Java Web基于Session的登錄實(shí)現(xiàn)方法,涉及Java針對(duì)session的操作及表單提交與驗(yàn)證技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了Java Web基于Session的登錄實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
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("用戶名或者密碼錯(cuò)誤!"); } 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>
希望本文所述對(duì)大家Java web程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
- JavaWeb Session 會(huì)話管理實(shí)例詳解
- JavaWeb Session失效時(shí)間設(shè)置方法
- JAVAEE中用Session簡(jiǎn)單實(shí)現(xiàn)購(gòu)物車功能示例代碼
- java中使用session監(jiān)聽實(shí)現(xiàn)同帳號(hào)登錄限制、登錄人數(shù)限制
- Java中設(shè)置session超時(shí)(失效)的三種方法
- Java中Cookie和Session的那些事兒
- JavaWeb基于Session實(shí)現(xiàn)的用戶登陸注銷方法示例
- 詳解Java如何實(shí)現(xiàn)基于Redis的分布式鎖
- Java中使用Jedis操作Redis的示例代碼
- Java自定義注解實(shí)現(xiàn)Redis自動(dòng)緩存的方法
- Java簡(jiǎn)單實(shí)現(xiàn)session保存到redis的方法示例
相關(guān)文章
SpringBoot整合EasyExcel實(shí)現(xiàn)導(dǎo)入導(dǎo)出功能
EasyExcel是一個(gè)基于Java的、快速、簡(jiǎn)潔、解決大文件內(nèi)存溢出的Excel處理工具,他能讓你在不用考慮性能、內(nèi)存的等因素的情況下,快速完成Excel的讀、寫等功能,本文就給大家介紹一下SpringBoot整合EasyExcel實(shí)現(xiàn)導(dǎo)入導(dǎo)出功能的方法,需要的朋友可以參考下2023-09-09Java變態(tài)跳臺(tái)階實(shí)現(xiàn)思路和代碼
今天小編就為大家分享一篇關(guān)于Java變態(tài)跳臺(tái)階實(shí)現(xiàn)思路和代碼,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-01-01Java8新特性之接口中的默認(rèn)方法和靜態(tài)方法
這篇文章主要介紹了Java8新特性之接口中的默認(rèn)方法和靜態(tài)方法的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07基于JavaMail的Java實(shí)現(xiàn)簡(jiǎn)單郵件發(fā)送功能
這篇文章主要為大家詳細(xì)介紹了基于JavaMail的Java實(shí)現(xiàn)簡(jiǎn)單郵件發(fā)送功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09Java中快速排序優(yōu)化技巧之隨機(jī)取樣、三數(shù)取中和插入排序
快速排序是一種常用的基于比較的排序算法,下面這篇文章主要給大家介紹了關(guān)于Java中快速排序優(yōu)化技巧之隨機(jī)取樣、三數(shù)取中和插入排序的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-09-09