当前位置:网站首页>解决JSP无法使用session.getAttribute()
解决JSP无法使用session.getAttribute()
2022-07-24 22:22:00 【旋风1+1】
概述,IDEA项目JSP页面无法使用内置session对象的getAttribute()方法。
项目中已经导入了jsp-api.jar和servlet-api.jar,用户在登录页面当中登录成功之后将用户对象存入session中,方便后面展示用户的相关信息。但是遇到一个问题,无法直接调用JSP内置的session对象的getAttribute()方法获取用户。具体是什么原因也不知道,但是解决方法如下:
1. 登录页面,当用户登录成功之后会跳转到list.jsp页面中。
// 获取用户名
String username = request.getParameter("username");
String password = request.getParameter("password");
String sql = "select id, username, password from t_user where username = ? and password = ?";
// 执行sql并查询用户
User user = DBUtils.query(User.class, sql, username, password);
if (user != null){
// 获取或者创建用户session,这里要求一定要获取到用户的session,如果没有会自动创建一个session对象。
HttpSession session = request.getSession();
// 将用户信息存储到session当中
session.setAttribute("user", user);
// 成功查询到用户时跳转到用户列表页面
response.sendRedirect(request.getContextPath()+"/dept/list");
} else {
// 没有查询到用户,让用户重写输入用户名与密码
response.sendRedirect(request.getContextPath()+"/login_fail.jsp");
}
2. 在jsp中展示登录显示“欢迎:xxx”用户,代码如下:
<body>
<h3>欢迎:<%=session.getAttribute("user")%></h3>
</body>
这里并不能直接通过内置对象session.getAttribute()方法取出在登录页面中存入session域中的用户。
3. 解决以上问题
在list.jsp页面中使用request.getSession()方法获取一个session对象;然后在取出session域当中的数据。
<%--从session中取出user--%>
<%
User user = (User)request.getSession().getAttribute("user");
%>
<body>
<h3>欢迎:<%=user.getUsername()%></h3>
</body>
边栏推荐
- The rule created by outlook mail is invalid. Possible reasons
- Available parameters of ansible Playbook
- 并查集结构
- EL & JSTL: JSTL summary
- Local data enhancement method of icml2022 | graph neural network
- Trinitycore worldofwarcraft server - registration website
- [database learning] redis parser & single thread & Model
- Alibaba cloud SSL certificate
- Baidu online disk +chrome plug-in
- Apipost signs up with Chinatelecom! Work together to accelerate the digital transformation of enterprises
猜你喜欢

From Fibonacci sequence to matrix fast power technique

【数据库学习】Redis 解析器&&单线程&&模型

Uniform sampling and thinning of PCL point cloud processing (61)

Web3 security go + security

图结构的实现,从点到边再到图

有序表之AVL树

PCL点云处理之直线点集投影规则化(五十六)

阿里云SSL证书

Apipost signs up with Chinatelecom! Work together to accelerate the digital transformation of enterprises

Application programming of communication heartbeat signal for communication abnormality judgment
随机推荐
How to adjust the default output of vscode to the debugging console to the terminal and the problem of garbled code in both
Boundary extraction of PCL point cloud processing (58)
[cloud native kubernetes] kubernetes cluster advanced resource object staterulesets
单调栈结构练习——子数组最小值的累加和
力扣 1184. 公交站间的距离
窗口内最大值或最小值的更新结构——窗口内最大值
WPF opens external programs and activates them when needed
Ranking of engineering project management software
A compatible, smaller and easy-to-use web font API
VScode默认输出到调试控制台如何调整到终端以及两者中的乱码问题
"Fundamentals of program design" Chapter 10 function and program structure 7-3 recursive realization of reverse order output integer (15 points)
RichTextBox operation
The accuracy of float
Get the solution to the slow running speed of Mengxin Xiaobai computer! ٩ ( ‘ ω‘ )و get! ٩ ( ‘ ω‘ )و
Dialogue with celebrities: where are the opportunities and challenges in the second half when brands gather at the shuzang track?
Violent recursion - detailed explanation of Queen n & how to optimize with bit operation
《元宇宙2086》单日销量超《三体》 夺得京东图书单日科幻榜冠军
Connector in C
EL & JSTL: JSTL summary
From Fibonacci sequence to matrix fast power technique