The difference between the two sessions in a Java Web project using the Struts framework and the common session value NULL error

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.