Nine basic built-in components of JSP

Source: Internet
Author: User
Tags html form

Source: If you have any copyright issues, please contact us

Basic Components

JSP has the following nine basic built-in components (corresponding to the six internal components of ASP ):
Request client request, which contains parameters from the get/POST request
Response returned from the response webpage to the client
The pagecontext page is managed here.
Session-related Session Period
Content being executed by application Servlet
Out is used to send response output.
Configuration servlet framework
Page JSP page itself
Exception for the error webpage, exceptions not captured

You can use them to access and execute JSPCodeServlet. To avoid talking about too many details about servlet APIs, let's examine what you can do with them:

Without using the formula, you can directly access the internal out object to print something to response:
<% Out. println ("hello"); %>
You do not need to directly transmit parameters to JavaBean. You can obtain the parameter values according to the request parts:
<% String name = request. getparameter ("name ");

Out. println (name); %>.

And so on.

The following describes session objects.

Session status maintenance is a must for Web application developers. There are multiple ways to solve this problem, such as using cookies, hiding form input fields, or directly attaching status information to a URL. Java Servlet provides a continuously Valid Session object between multiple requests, which allows users to store and extract session status information. JSP also supports this concept in servlet.
Many descriptions of implicit objects can be seen in Sun's JSP Guide (the implicit meaning is that these objects can be directly referenced and do not need to be explicitly declared, no special code is required to create its instance ). For example, the request object is a subclass of httpservletrequest. This object contains all information about the current browser request, including cookies, HTML form variables, and so on. The Session object is also such an implicit object. This object is automatically created when the first JSP page is loaded and associated with the request object. Similar to session objects in ASP, session objects in JSP are very useful for applications that want to complete a transaction through multiple pages.
To illustrate the specific application of the session object, we use three pages to simulate a multi-page web application. The first page (q1.html) only contains an HTML form that requires the user name to be entered. The Code is as follows:
<HTML>
<Body>
<Form method = post action = "q2.jsp">
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). It extracts the thename value from the q1.html form through the request object, stores it as the name variable, and then saves the name value to the session object. The Session object is a set of name/value pairs. Here, the name in the name/value pair is "thename", and the value is the value of the name variable. Because the session object remains valid during the session, the variables saved here are also valid for subsequent pages. Another task of 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?
<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 Q & A result. It extracts the value of thename from the session object and displays it to prove that although the value is input on the first page, it can be retained through the session object. Another task of q3.jsp is to extract 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.