Java Web基于Session的登錄實現(xiàn)方法
更新時間:2015年10月19日 14:39:14 作者:煙大洋仔
這篇文章主要介紹了Java Web基于Session的登錄實現(xiàn)方法,涉及Java針對session的操作及表單提交與驗證技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了Java Web基于Session的登錄實現(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("用戶名或者密碼錯誤!");
}
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程序設計有所幫助。
您可能感興趣的文章:
- JavaWeb Session 會話管理實例詳解
- JavaWeb Session失效時間設置方法
- JAVAEE中用Session簡單實現(xiàn)購物車功能示例代碼
- java中使用session監(jiān)聽實現(xiàn)同帳號登錄限制、登錄人數(shù)限制
- Java中設置session超時(失效)的三種方法
- Java中Cookie和Session的那些事兒
- JavaWeb基于Session實現(xiàn)的用戶登陸注銷方法示例
- 詳解Java如何實現(xiàn)基于Redis的分布式鎖
- Java中使用Jedis操作Redis的示例代碼
- Java自定義注解實現(xiàn)Redis自動緩存的方法
- Java簡單實現(xiàn)session保存到redis的方法示例
相關文章
SpringBoot整合EasyExcel實現(xiàn)導入導出功能
EasyExcel是一個基于Java的、快速、簡潔、解決大文件內存溢出的Excel處理工具,他能讓你在不用考慮性能、內存的等因素的情況下,快速完成Excel的讀、寫等功能,本文就給大家介紹一下SpringBoot整合EasyExcel實現(xiàn)導入導出功能的方法,需要的朋友可以參考下2023-09-09
基于JavaMail的Java實現(xiàn)簡單郵件發(fā)送功能
這篇文章主要為大家詳細介紹了基于JavaMail的Java實現(xiàn)簡單郵件發(fā)送功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09
Java中快速排序優(yōu)化技巧之隨機取樣、三數(shù)取中和插入排序
快速排序是一種常用的基于比較的排序算法,下面這篇文章主要給大家介紹了關于Java中快速排序優(yōu)化技巧之隨機取樣、三數(shù)取中和插入排序的相關資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2023-09-09

