我正在使用jsp和servlet开发一个应用程序,在jsp中使用会话隐式对象面临一个问题。我正在做的是在servlet中的session对象中设置某些属性,然后将响应转发给jsp页面。现在,我想访问jsp页面中的那些会话属性,但是在这样做时,它将抛出一个空指针异常。这是我的密码:-
这是我的servlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw=response.getWriter();
con=Connector.myCon();
u=request.getParameter("userName");
p=request.getParameter("password");
HttpSession session=request.getSession();
session.setAttribute("userName",u);
session.setAttribute("userType",userType);
RequestDispatcher rd=request.getRequestDispatcher("Home.jsp");
rd.forward(request, response);
}这是我的jsp
<%
session=request.getSession();
String s = (String) application.getAttribute("name");
System.out.println(s);
String userName = (String)session.getAttribute("userName");
if (userName == null) {
//request.setAttribute("Error", "Session has ended. Please logenter code herein.");
RequestDispatcher rd = request
.getRequestDispatcher("index.hmtl");
rd.include(request, response);
out.println("Session has ended. Please login.");
}
else
{
String userType = (String)session.getAttribute("userType");
System.out.println(userType);
if(userType.equals("student"))
{}
}%> 在使用此选项时,我将得到一个空指针异常。帮帮我。
发布于 2015-06-24 13:43:07
像这样改变,
<%
String s = (String) application.getAttribute("name");
System.out.println(s);
String userName = (String)session.getAttribute("userName");
if (userName == null) {
//request.setAttribute("Error", "Session has ended. Please logenter code herein.");
RequestDispatcher rd = request
.getRequestDispatcher("index.hmtl");
rd.include(request, response);
out.println("Session has ended. Please login.");
}
else
{
String userType = (String)session.getAttribute("userType");
System.out.println(userType);
if(userType.equals("student"))
{}
}%> https://stackoverflow.com/questions/31028121
复制相似问题