国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Java > 正文

Java Web基于Session的登錄實(shí)現(xiàn)方法

2019-11-26 14:55:12
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(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ì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 新干县| 逊克县| 商南县| 南部县| 山阳县| 宜昌市| 西乌珠穆沁旗| 阿拉善左旗| 孟州市| 乌兰县| 千阳县| 南丰县| 宁陵县| 平阳县| 黎川县| 平山县| 扶沟县| 右玉县| 井研县| 定襄县| 东阿县| 白银市| 如皋市| 永州市| 万源市| 蒙自县| 祁连县| 花莲市| 庆城县| 宕昌县| 尉氏县| 舒兰市| 大庆市| 托克逊县| 彭水| 佛山市| 师宗县| 苗栗县| 衡阳县| 荥阳市| 定远县|