Introduction of JSP Technology II

Source: Internet
Author: User
Tags object contains html form variables sessions string variable valid


Session state maintenance is a problem that WEB application developers must face. There are several ways to solve this problem, such as using Cookies, hidden form input fields, or attaching state information directly to a URL. The Java Servlet provides a session object that continues to be valid between multiple requests, allowing the user to store and extract session state information. JSP also supports this concept in the Servlet.



In Sun's JSP guide you can see a number of descriptions of suppressed objects (implicitly, these objects can be referenced directly, not explicitly declared, and do not require specialized code to create their instances). For example, the request object, which is a subclass of HttpServletRequest. This object contains all the information about the current browser request, including Cookies, HTML form variables, and so on. The session object is also such an implied object. This object is automatically created when the first JSP page is loaded and is associated with the request object. Similar to the Session object in ASP, the sessions object in the JSP is useful for applications that want to accomplish a transaction across multiple pages.



To illustrate the specific application of the Session object, we then use three pages to simulate a multiple-page Web application. The first page (q1.html) contains only one HTML form that requires the user's name to be entered, and the code is as follows:


  < HTML>
  < BODY>
  < FORM METHOD=POST ACTION="q2.jsp">
  please input your name:
  < INPUT TYPE=TEXT NAME="thename">
  < INPUT TYPE=SUBMIT VALUE="SUBMIT">
  < /FORM>
  < /BODY>
  < /HTML>


The second page is a JSP page (q2.jsp) that extracts the thename value from the Q1.html form from the request object, stores it as a name variable, and then saves the name value to the Session object. The Session object is a collection of name/value pairs, where the name is "Thename" in the name/value pair, and the value is the value of the name variable. Because the session object is always valid during sessions, the variables saved here are also valid for subsequent pages. Another task for q2.jsp is to ask the second question. The following is its code:


  < HTML>
  < BODY>
  < %@ page language="java" %>
  < %! String name=""; %>
  < %
  name = request.getParameter("thename");
  session.putValue("thename", name);
  %>
  your name is: &< %= name %>
  < p>
  < FORM METHOD=POST ACTION="q3.jsp">
  what would you like to eat ?
  < INPUT TYPE=TEXT NAME="food">
  < P>
  < INPUT TYPE=SUBMIT VALUE="SUBMIT">
  < /FORM>
  < /BODY>
  < /HTML>


The third page is also a JSP page (q3.jsp), the main task is to display the question and answer results. It extracts the value of Thename from the Session object and displays it to prove that although the value is entered in the first page, it is retained through the session object. Another task for q3.jsp is to extract the user input from the second page and display it:


   < HTML>
    < BODY>
    < %@ page language="java" %>
< %! String food=""; %>
< %
  food = request.getParameter("food");
  String name = (String) session.getValue("thename");
%>
  your name is: &< %= name %>
< P>
  You like to eat: &< %= food %>
< /BODY>
< /HTML>

Related Article

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.