当前位置:网站首页>解决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>
边栏推荐
- 通讯异常判断之通讯心跳信号应用编程
- [which is better to use, apopost or apifox? Just read this!]
- AC automata
- Use kettle to read the data in Excel file and store it in MySQL
- Monotonic stack structure exercise -- cumulative sum of minimum values of subarrays
- General syntax and classification of SQL language (II)
- Flex layout
- About constant modifier const
- Things to study
- Alibaba cloud SSL certificate
猜你喜欢

"Yuan universe 2086" outsold "San ti" in one-day sales and won the champion of JD books' one-day science fiction list

Okaleido tiger NFT即将登录Binance NFT平台,后市持续看好

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

Error connecting MySQL database with kettle

Alibaba cloud SSL certificate

From violent recursion to dynamic programming, memory search

PCL点云处理之均匀采样抽稀(六十一)

线段树,,

【1184. 公交站间的距离】

The rule created by outlook mail is invalid. Possible reasons
随机推荐
聊聊 Redis 是如何进行请求处理
Website resources
CSF cloth simulation filtering for PCL point cloud processing (59)
单调栈结构
Boundary extraction of PCL point cloud processing (58)
基于深度学习的多任务人脸属性分析(基于飞桨PaddlePaddle)
[cloud native kubernetes] kubernetes cluster advanced resource object staterulesets
WPF opens external programs and activates them when needed
Pyqt5 uses qfile and qdatastream to read and write binary files
WPF uses pathgeometry to draw the hour hand and minute hand
EL & JSTL: JSTL summary
ODBC executes stored procedure to get return value
PCL点云处理之平面规则化(五十五)
The kettle job implementation runs a kettle conversion task every 6S
Talk about how redis handles requests
Gradle learning - gradle advanced instructions
"Fundamentals of program design" Chapter 10 function and program structure 7-2 Hanoi Tower problem (20 points)
Helm —— 强大的 Kubernetes 应用的包管理工具
One click compilation and installation of redis6.2.4
Poj2308 continuously look at dfs+bfs+ optimization