web【比较request与session作用域区别】

UserDao类

public class UserDao {
	public boolean Check(User u)
	{
		if(u.getUser().equals("admin") && u.getPwd().equals("123"))
		{
			return true;
		}
		return false;
	}
}

User类

public class User {
	
	private String user;
	private String pwd;
	public String getUser() {
		return user;
	}
	public void setUser(String user) {
		this.user = user;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public User(String user, String pwd) {
		super();
		this.user = user;
		this.pwd = pwd;
	}
	

}

LoginServlet处理

@WebServlet({ "/LoginServlet", "/login" })
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
		//response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//doGet(request, response);
		String action = request.getParameter("action");
		if(action.equals("login"))
		{
		String user = request.getParameter("user");
		String pwd = request.getParameter("pwd");
		User u = new User(user,pwd);
		UserDao dao = new UserDao();
		if(dao.Check(u))
		{
			request.setAttribute("user",u.getUser());
			request.getSession().setAttribute("user",u.getUser());
			//验证request、session作用域内置对象
			response.sendRedirect("main.jsp");
		}
		else
		{
			response.sendRedirect("fail.jsp");
		}
	}
		else if(action.equals("logout")){
			//清楚session中属性user
			request.getSession().removeAttribute("user");
			request.getSession().invalidate();
			//跳转至登录界面
			response.sendRedirect("login.jsp");
		}
	}

}

main.jsp

<html>
<head>
<meta charset="UTF-8">
<title>系统页面</title>
</head>
<body>
<% 
	if(session.getAttribute("user")==null)
	{
		response.sendRedirect("login.jsp");
	}
%>
<p>①使用内置对象request,因为重定向所以request作用域原因导致丢失</p>
<h1>欢迎<%=request.getAttribute("user") %>登录</h1>
<p>①使用内置对象session</p>
<h1>欢迎<%=session.getAttribute("user") %>登录</h1>
<br>
<p></p>
<a href="login?action=logout">登出(用的是get方法)
</a>
</body>
</html>

fail.jsp

<html>
<head>
<meta charset="UTF-8">
<title>出错</title>
</head>
<body>
<h1>登录失败,2秒后跳转至登录界面</h1>
<%
	response.setHeader("Refresh", "2;url=login.jsp");
%>
</body>
</html>

login登录页面

<html>
<head>
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<body>
<form action="login?action=login" method="post">
	账号:<input type="text" name="user">
<br>
	密码:<input type="password" name="pwd">
<br>
	<input type="submit" value="登录">
	<input type="reset" value="重置">
</form>
</body>
</html>

示例。admin,123.
request丢失
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

咸鱼xxx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值