2016-11-24 104 views
0

我正在学习JSP。我无法了解会话是如何在JSP中创建的。会话如何在JSP中创建?

到目前为止我所知道的是会话是隐式对象在_jspService方法下创建的。所以我手动创建会话。在下面的JSP代码中,我创建了与自动在index_jsp中创建的会话相同的会话,但我的值为空。所以任何机构都可以解释我为什么我变得空?

<%@ page import ="java.util.*" session="false" %> 
<% 
javax.servlet.http.HttpSession session = null; 
session = pageContext.getSession(); 
%> 
<html> 
<body> 
<%=session %> 
</body> 

+0

是的,你的答案是不是没有答案,我找 – Bat

+0

为什么你希望设置会话=假后的会话处理或会话对象? – Holger

+0

我有一些要求,其中默认页面会话是错误的,但在某些情况下,我不得不在JSP中手动创建会话,所以当我用上述方法尝试时,我没有得到 – Bat

回答

0

可以解释我为什么我的会议越来越空?如果你设置会话属性设置为false,即

在JSP文件中的会话将被禁用,session="false",你可以看看here了解更多详情。

我无法知道会话是如何在JSP中创建的。当从JSP调用request.getSession(true);(因为request也是JSP隐式对象)

httpsession对象将被创建(&维护)由servlet容器。

公共HttpSession中的getSession(布尔创建):返回当前 的HttpSession与此请求相关联,或者如果没有当前 会话,并创建为true,则返回一个新的会话。如果create为假 且请求没有有效的HttpSession,则此方法返回null。

你可以参考API here

因此,创建你的代码中session你改变它为:

<% 
javax.servlet.http.HttpSession session = request.getSession(true); 
// you can session object from now add attributes, 
// get attributes, remove attributes, etc.. 
%> 

而且,一旦会话创建(如上图所示,使用request.getSession(true) ),则需要使用request.getSession()来检索相同的会话对象。

换句话说,在整个应用程序,

(1)使用过程中LoginServletLoginController类用户登录时创建的会话ONLY ONCE request.getSession(true)

(2),然后在所有使用request.getSession()其他servlet /控制器方法。

作为一个附注,我强烈建议您使用Controller(如使用Spring)或Servlet类来编写Java代码,因为JSP仅用于表示层(用于显示html内容)。

+0

downvoters,请留下评论,以便我可以了解什么是缺失 – developer

+0

我也很惊讶。再来一次。有时它似乎是一个downvote机器人。 – Holger

+0

我不知道什么是从上面丢失,至少你可以upvote,如果你觉得它是有帮助的 – developer

1

至于你说:

会话隐对象下_jspService方法

的JSP文件由碧玉引擎编译成一个Java类创建。尽管您已经在JSP中编写了代码,但在创建的Java类中还是有一些准备这些隐式对象的。
因此你不需要再做一次。

你可以使用它们。您可以在JSP中编写代码:

From EL:<br> 
sessionScope.name: ${sessionScope.name}<br> 
<br> 
From Scriptlet. <br> 
<%=session.getAttribute("name")%> 

而且您获得相同的输出两次:会话属性“name”的值。

在例如JSP与内容:

<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Title</title> 
</head> 
<body> 
From EL:<br> 
sessionScope.name: ${sessionScope.name}<br> 
<br> 
From Scriptlet. <br> 
<%=session.getAttribute("name")%> 
</body> 
</html> 

将导致Java类文件:

package org.apache.jsp; 

import javax.servlet.*; 
import javax.servlet.http.*; 
import javax.servlet.jsp.*; 

public final class testcompile_jsp extends org.apache.jasper.runtime.HttpJspBase 
    implements org.apache.jasper.runtime.JspSourceDependent { 

    private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory(); 

    private static java.util.List<String> _jspx_dependants; 

    private org.glassfish.jsp.api.ResourceInjector _jspx_resourceInjector; 

    public java.util.List<String> getDependants() { 
    return _jspx_dependants; 
    } 

    public void _jspService(HttpServletRequest request, HttpServletResponse response) 
     throws java.io.IOException, ServletException { 

    PageContext pageContext = null; 
    HttpSession session = null; 
    ServletContext application = null; 
    ServletConfig config = null; 
    JspWriter out = null; 
    Object page = this; 
    JspWriter _jspx_out = null; 
    PageContext _jspx_page_context = null; 

    try { 
     response.setContentType("text/html; charset=UTF-8"); 
     response.setHeader("X-Powered-By", "JSP/2.3"); 
     pageContext = _jspxFactory.getPageContext(this, request, response, 
       null, true, 8192, true); 
     _jspx_page_context = pageContext; 
     application = pageContext.getServletContext(); 
     config = pageContext.getServletConfig(); 
     session = pageContext.getSession(); 
     out = pageContext.getOut(); 
     _jspx_out = out; 
     _jspx_resourceInjector = (org.glassfish.jsp.api.ResourceInjector) application.getAttribute("com.sun.appserv.jsp.resource.injector"); 

     out.write("\r\n"); 
     out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n"); 
     out.write("<html>\r\n"); 
     out.write("<head>\r\n"); 
     out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n"); 
     out.write("<title>Title</title>\r\n"); 
     out.write("</head>\r\n"); 
     out.write("<body>\r\n"); 
     out.write("From EL:<br>\r\n"); 
     out.write("sessionScope.name: "); 
     out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.evaluateExpression("${sessionScope.name}", java.lang.String.class, (PageContext)_jspx_page_context, null)); 
     out.write("<br>\r\n"); 
     out.write("<br>\r\n"); 
     out.write("From Scriptlet. <br>\r\n"); 
     out.print(session.getAttribute("name")); 
     out.write("\r\n"); 
     out.write("</body>\r\n"); 
     out.write("</html>"); 
    } catch (Throwable t) { 
     if (!(t instanceof SkipPageException)){ 
     out = _jspx_out; 
     if (out != null && out.getBufferSize() != 0) 
      out.clearBuffer(); 
     if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); 
     else throw new ServletException(t); 
     } 
    } finally { 
     _jspxFactory.releasePageContext(_jspx_page_context); 
    } 
    } 
} 

正如你可以在_jspService方法见有行:

HttpSession session = null; 
... 
session = pageContext.getSession(); 

基本上这是隐式会话对象。你的代码在那之后,可以使用它。

编辑:

随着<%@ pagesession="false" %>你说 “我不需要会话”。所以会话在pageContext中没有绑定。因此,如果您拨打pageContext.getSession(),您会收到null;

如果你需要它,你必须使用:

request.getSession(); 
+0

谢谢,但我的查询是,如果会话在页面指令为false然后session = pageContext.getSession();返回null。为什么?如果你使用简单的JSP,那么它返回会话对象 – Bat