My Baidu know answer, feel this knowledge very meaningful, specifically posted out.
But what remains to be verified is
Actioncontext.getcontext (). GetSession ("name", "Tom"); // How do I display it through an EL or Struts2 tag in a JSP?
Here is the answer text.
Many novice Java Web students because of the use of the STRUT2 framework to distinguish between the two session.
1. Jsp/servlety has a native session object, which is one of the 9 big built-in objects, all written in the book, Remember. The type is HttpSession session; The function method is SetAttribute (), getattribute (), with the following usage
// Java saved to the native session object Servletactioncontext.getrequest (). GetSession (). SetAttribute ("MyStr", mystr); // JSP value with struts tag: <s:property value= "#session. MyStr"/> with El Expression: ${sessionscope.mystr} with JSP code: <%=session.getattribute ("MyStr")%>
2. If the project uses the STRUTS2 framework, STRUTS2 itself also encapsulates a session, which is a map type object. So, as with the MAP usage, use the put value, get value. Usage is:
// Java Save to struts2 session Actioncontext.getcontext (). GetSession (). Put ("User", user); // JSP page reads with struts tags: <s:property value= "#session. Hello"/> with El Expression: ${hello} with Java code:<%= Session.getattribute ("Hello")%>
Many people say why can't the session take out the value? Look at their code, do you think it's right?
//Error code example://ActionMap<string, object> session=Actioncontext.getcontext (). getsession (); Session.put ("Name", "Tom");//JSPHis name is called <%=session.getattribute ("name")%>/*This obviously made the mistake of confusing the 2 session. In the action you are in the STRUTS2 Framework package session (actually a map), and in the JSP, you read the native session. It's not a thing at all. *///correct wording//Action//must be a session of the native HttpSession type obtained from the request object//native Sessiond methods are setattribute and getattribute, not put,set.HttpSession session =servletactioncontext.getrequest (). getsession (); Session.setattribute ("Name", "Tom");//JSPHis name is called <%=session.getattribute ("name")%>
Using the code above, note the appropriate class library for import.
The difference between the two sessions in a Java Web project using the Struts framework and the common session value NULL error