当前位置:网站首页>Solve the problem that JSP cannot use session.getattribute()
Solve the problem that JSP cannot use session.getattribute()
2022-07-24 22:40:00 【Cyclone 1+1】
summary ,IDEA project JSP Pages cannot use built-in session Object's getAttribute() Method .
The project has already imported jsp-api.jar and servlet-api.jar, After the user logs in successfully on the login page, he saves the user object session in , It is convenient to show the relevant information of users later . But there was a problem , Cannot call directly JSP Built in session Object's getAttribute() Method get user . I don't know the specific reason , But the solution is as follows :
1. The login page , When the user logs in successfully, he will jump to list.jsp On the page .
// Get username
String username = request.getParameter("username");
String password = request.getParameter("password");
String sql = "select id, username, password from t_user where username = ? and password = ?";
// perform sql And query users
User user = DBUtils.query(User.class, sql, username, password);
if (user != null){
// Get or create users session, Here, it is required to obtain the user's session, If not, one will be created automatically session object .
HttpSession session = request.getSession();
// Store user information in session among
session.setAttribute("user", user);
// When the user is successfully queried, jump to the user list page
response.sendRedirect(request.getContextPath()+"/dept/list");
} else {
// No user found , Let the user rewrite the user name and password
response.sendRedirect(request.getContextPath()+"/login_fail.jsp");
}
2. stay jsp Login display in “ welcome :xxx” user , The code is as follows :
<body>
<h3> welcome :<%=session.getAttribute("user")%></h3>
</body>
You can't directly use built-in objects session.getAttribute() Method to take it out and save it in the login page session Users in the domain .
3. Solve the above problems
stay list.jsp Page usage request.getSession() Method to get a session object ; And then take it out session Data in the domain .
<%-- from session Remove from user--%>
<%
User user = (User)request.getSession().getAttribute("user");
%>
<body>
<h3> welcome :<%=user.getUsername()%></h3>
</body>
边栏推荐
- Multi task face attribute analysis based on deep learning (based on paddlepaddle)
- 由斐波那契数列引述到矩阵快速幂技巧
- [1184. Distance between bus stops]
- [database learning] redis parser & single thread & Model
- Flex layout
- EL & JSTL: JSTL summary
- Archsummit: evolution of the underlying framework of cherished microservices
- The kettle job implementation runs a kettle conversion task every 6S
- Gradle learning set integration
- 洛谷 P2024 [NOI2001] 食物链
猜你喜欢

老杜Servlet-JSP

Monotonic stack structure exercise -- cumulative sum of minimum values of subarrays

Talk about how redis handles requests

QT6 with vs Code: compiling source code and basic configuration

Morris遍历

Archsummit: evolution of the underlying framework of cherished microservices

《元宇宙2086》单日销量超《三体》 夺得京东图书单日科幻榜冠军

One click compilation and installation of redis6.2.4

Database - Metadata databasemetadata beginner

Homework of the 20th week
随机推荐
QT6 with vs Code: compiling source code and basic configuration
ansible-playbook 可用参数
Kubernetes scheduling concept and workflow
RichTextBox save as picture
Let‘s Encrypt
"Yuan universe 2086" outsold "San ti" in one-day sales and won the champion of JD books' one-day science fiction list
On the problem that the on-board relay cannot be switched on due to insufficient power supply
10 key points and 5 measures for good project management
A compatible, smaller and easy-to-use web font API
WPF opens external programs and activates them when needed
《JUC并发编程 - 高级篇》05 -共享模型之无锁 (CAS | 原子整数 | 原子引用 | 原子数组 | 字段更新器 | 原子累加器 | Unsafe类 )
窗口内最大值或最小值的更新结构——窗口内最大值
Joint search set structure
Nvida tensorrt deployment (I)
并查集结构
Enterprise operation we media can't "self Hi": the content should be grounded, not advertising
PCL point cloud processing: creating a two-dimensional grid to organize point cloud data (64)
Uniform sampling and thinning of PCL point cloud processing (61)
Gee - dataset introduction mcd12q1
Gradle learning - gradle advanced instructions