2016-11-24 103 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