session().getAttribute返回null [英] session().getAttribute is returning null
本文介绍了session().getAttribute返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个servlet,它调用jsp页面. 在servlet中,我正在正确检索登录名提供的用户名. 但是在会话中设置了相同的值之后,当我访问被调用的jsp页面时,其返回null.
I have a servlet which calls a jsp page. In the servlet I am retrieving the username provided at the login correctly. But after setting the same in session, when i access the called jsp page, its returning null.
public class AdminServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -4244742541587179390L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userName = request.getParameter("name");
System.out.println("UserName: " + userName); // Here it prints the username properly
request.getSession(true).setAttribute(request.getParameter("name"), userName );
RequestDispatcher rd = request.getRequestDispatcher("upload.jsp");
rd.forward(request, response);
// response.sendRedirect("upload.jsp");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
我正在访问此代码的JSP代码段:
JSP Code snippet where I am accessing this:
<label class="message">Welcome <%= session.getAttribute("userName") %></label>
我在这里做错了什么?谁能帮忙
What am I doing wrong here? Can anyone help please
推荐答案
我认为您颠倒了两个参数.应该是这样的:
I think you inverted the two params. It should be like this:
request.getSession(true).setAttribute("userName", userName );
这篇关于session().getAttribute返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文