Introduction to nine basic built-in components of JSP

Source: Internet
Author: User
Tags contains html form variables sessions valid
JS JSP has a total of the following 9 basic built-in components (can be compared with 6 internal components of ASP):

Request client, this request contains parameters from the Get/post request

Response Web page Returns the response of the client

The properties of the PageContext Web page are managed here

Session duration associated with the request

What the application servlet is doing

Out output used to transmit a response

Config servlet frame part

Page JSP pages themselves

Exception for error pages, not captured exceptions

You can use them to access the servlet that executes the JSP code. To avoid talking about the details of too many servlet APIs, let's examine some of the things you can do with them:

Instead of using a formula, you can directly access an internal out object to print something to response:

<% out.println ("Hello"); %>

You do not have to transfer parameters directly to JavaBean, you can get the value of the parameter according to the requested part:

<% String name=request.getparameter ("name"); OUT.PRINTLN (name); %>.

Wait a minute.

The session objects are highlighted below.

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 enter 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 do 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.