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

网友投稿 242 2023-07-27


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

本文实例讲述了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(HttpSehttp://rvletRequest 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 http://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(HttpSerkBvAmrvletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request,response);

}

}

用户名:

密码:

希望本文所述对大家Java web程序设计有所帮助。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:API检测:保障产品质量的关键步骤
下一篇:用Java集合中的Collections.sort方法如何对list排序(两种方法)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~