session 实现保存用户信息

时间:2023-03-09 18:00:47
session 实现保存用户信息

index.jsp

 <body>
<div style="margin: 0 auto; width: 500px; text-align: center;">
<img alt="图片" src="logo.jpg" width="200px">
<h1>绿叶网上书店系统</h1>
<form method="post" action="doLogin.jsp">
用户名:<input type="text" name="userName"><br>
查 询:<input type="password" name="pwd"><br>
<input type="submit" value="提交">
<input type="reset" value="取消">
</form>
</div>
</body>

doLogin.jsp

 <body>
<%
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("userName");
String pwd = request.getParameter("pwd");
if("admini".equals(name.trim()) && "admini".equals(pwd.trim())){
session.setAttribute("name", name);
session.setAttribute("pwd", pwd);
response.sendRedirect("showLoginInfo.jsp");
}
%>
</body>

show.jsp

 <body>
<%
String name = (String)session.getAttribute("name");
String pwd = (String)session.getAttribute("pwd");
out.print("欢迎 "+name+" 登录成功!!!! 用户密码为:"+pwd);
%>
</body>