Session保存用户名到Session域对象中
2024-08-29 05:57:49
Session保存用户名
1.构造登录界面
用户名:
密 码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<! DOCTYPE html> < html > < head > < meta charset="UTF-8"> < title >Session保存用户名</ title > </ head > < body > < form action="doS3" method= "post"> 用户名:< input type="text" name="name"/>< br /> 密 码:< input type="password" name = "pwd">< br /> < input type="submit"> </ form > </ body > </ html > |
2.获取Session并将用户名保存到Session域对象中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
package com.oaec.session; import java.io.IOException; import java.util.Date; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class servletDemo3 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding( "UTF-8" ); resp.setContentType( "text/html;charset=UTF-8" ); // req.getAttribute(arg0) String name = req.getParameter( "name" ); String pwd = req.getParameter( "pwd" ); if ( "高圆圆" .equals(name) && "123" .equals(pwd)) { // 将用户名保存在session中 // 1.获得session HttpSession session = req.getSession(); // 2.将用户名保存在session中 session.setAttribute( "uname" , name); resp.sendRedirect( "doS4" ); } else { resp.sendRedirect( "index.html" ); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } } |
3.从Session中取出数据 并对页面进行保护 没有登录通过URL访问 直接重定向到登录界面 即主页
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
package com.oaec.session; import java.io.IOException; import java.io.PrintWriter; import java.util.Date; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class servletDemo4 extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding( "UTF-8" ); resp.setContentType( "text/html;charset=UTF-8" ); //从session中取出数据 HttpSession session = req.getSession( false ); Object object = null ; if (session != null && (object = session.getAttribute( "uname" ))!= null ) { PrintWriter writer = resp.getWriter(); writer.write( "登录成功<br>" ); writer.write( "欢迎你" +object); } else { //没有登录过 直接重定向到主页 resp.sendRedirect( "index.html" ); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } } |
最新文章
- [WPF系列]-Data Validation
- BZOJ4614 [Wf2016]Oil
- angularJS实现二级联动查询以及自定义过滤器的使用
- 用Ant来做一键部署
- I535卡刷土豆修改4.1.2版本ROMV4過程
- session实现防止重复提交,以及验证
- Codeforces Round #312 (Div. 2)B. Amr and The Large Array 暴力
- 很棒的jQuery代码片段分享
- 网络编程TCP协议-聊天室
- 蓝牙连接音响问题(android电视)
- 用 node.js 创建第一个Hello World
- Sass之Compass学习笔记
- mysql数据库连接池使用(二)实现自己的数据库连接池
- js判断页面在pc端打开还是移动端打开
- #218 Iterate with JavaScript For Loops
- Servlet-获取页面的元素的值的方式以及区别
- Oracle的decode、sign、trunc函数
- Pandas初学者代码优化指南
- 并发工具类(四)线程间的交换数据 Exchanger
- linux 和windows系统下同时可用的UML建模工具(umbrello),超强