java 登录session_Java Web基于Session的登录实现方法

该博客介绍了如何使用Java Servlet进行用户登录验证,包括获取请求参数、数据库操作及会话管理。通过实例展示了如何在HttpServletRequest和HttpServletResponse中处理POST和GET请求,以及如何使用session存储用户信息并实现登录后跳转。
摘要由CSDN通过智能技术生成

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 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 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 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);

}

}

Index.html

用户名:

密码:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值