2016-11-24 15 views
0

私はJSPを学んでいます。 JSPでセッションがどのように作成されたのかわかりません。セッションはどのようにJSPで作成されますか?

セッションは暗黙のうちに_jspServiceメソッドの下で作成されています。だから私は手動でセッションを作成しました。以下のJSPコードでは、index_jspに自動的に作成されるのと同じセッションを作成しましたが、nullとして値を取得しています。だから、どんな体でも私に説明することができますなぜ私はヌルになっていますか?

<%@ 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

私はセッションのためにnullを取得していますなぜ私を説明できますか?

JSPファイル内のセッションでは、session="false"、すなわちfalseにセッション属性を設定した場合、あなたはより多くの詳細についてはhereを探すことができます無効になります。

セッションがJSPでどのように作成されたのかわかりません。

httpsessionあなたがJSPからrequest.getSession(true);を呼び出すとき(requestもJSPに暗黙オブジェクトであるため)オブジェクトは、サーブレットコンテナによって(&を維持)が作成されます。

公共のHttpSessionのgetSessionは(ブール値が作成):そこには、現在の のセッションではなく、真実である作成し、新しいセッションを返す場合、この要求に関連付けられている現在の のHttpSessionを返しますか。 createがfalseの場合 要求に有効なHttpSessionがない場合、このメソッドはnullを返します。

あなたはあなたのようにそれを変更し、あなたのコードからsessionを作成するには、here

ので、APIを参照することができます:request.getSession(true)を使用して上記のように、セッションが(作成された後、

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

も同じセッションオブジェクトを取得するには、request.getSession()を使用する必要があります。言い換えれば

、アプリケーション全体で、

(1)(2)request.getSession(true)

を使用してLoginServletLoginControllerクラスのユーザのログイン時間中に1回だけセッションを作成し、すべてでrequest.getSession()を使用他のサーブレット/コントローラメソッド。

補足:JSPは、プレゼンテーション層(htmlコンテンツを表示するため)のためだけに使用されるため、Controller(Springを使用するようなもの)またはServletクラスを使用してJavaコードを書くことを強くお勧めします。

+0

downvoters、私は紛失しているものを理解できるようにコメントを残してください – developer

+0

私も驚いています。もう一度。ときどきダウンボットボットのように見えます。 – Holger

+0

私は上に何が欠けているかわからない、少なくとも助けていると思うなら、upvoteすることができます – developer

1

あなたが言ったように:

セッションは、JSPファイルをJavaクラスにジャスパーエンジンによってコンパイルされ

_jspServiceメソッドの下に作成された暗黙オブジェクトです。 JSPで記述されたコードにもかかわらず、作成されたJavaクラスには、これらの暗黙オブジェクトのいくつかの準備があります。
したがって、再度行う必要はありません。

これを使用できます。セッション属性「名前」の値を:あなたは、あなたのJSPのコード書くことができます。

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

をそして、あなたは二度同じ出力を得ます。

<%@ 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クラスファイルになります:コンテンツとJSPの例では

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

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方法で見ることができるように

これは暗黙セッションオブジェクトです。あなたのコードはそれに続いて使用することができます。

EDIT:

<%@ pagesession="false" %>であなたは "私はセッションを必要としない" と言います。したがって、セッションはpageContext内の結合ではありません。したがって、pageContext.getSession()に電話すると、nullが届きます。

あなたがそれを必要とする場合、使用する必要があります。

request.getSession(); 
+0

ありがとう、しかし、私の質問は、セッションがpageディレクティブでfalseならsession = pageContext.getSession();ヌルが返されます。どうして?単純なJSPを使用している場合、セッションオブジェクトを返す – Bat

関連する問題