欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java Web基于Session的登录实现方法

程序员文章站 2024-03-07 12:21:03
本文实例讲述了java web基于session的登录实现方法。分享给大家供大家参考,具体如下: package cn.com.login; import ja...

本文实例讲述了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程序设计有所帮助。