JSP 9 basic built-in components

zhaozj2021-02-16  53

Basic component

JSP has the following 9 basic built-in components (can correspond to the six internal components of the ASP):

Request user request, this request will contain parameters from Get / POST request

Response web page back to the user's response

The properties of the PageContext page are managed here

Session and request related session

Application Servlet is being executed

OUT is used to transfer the output of the response

Config servlet architecture components

Page JSP page itself

Exception For error web, unconcerned exceptions

You can use them to access servlets that perform JSP code. In order to avoid talking to the details of too many servlet APIs, let's see some things you can do with them:

You don't have to use an arithmetic, you can directly access the internal OUT object to RESPONSE:

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

You don't have to transfer parameters directly to JavaBean, you can get the value of the parameters as requested:

<% String name = request.getParameter ("name");

Out.println (Name);%>.

and many more.

The session object is focused below.

Session status 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 directly attach status information to the URL. Java servlets provide a session object that continuously and valid between multiple requests that allow users to store and extract session status information. JSP also supports this concept in the servlet.

Many of the instructions on implied objects (implicit meanings can be directly referenced in the JSP guide in Sun), which do not need to be explicitly declared, and do not require special code to create an example. For example, a Request object, it is a subclass of HttpServletRequest. This object contains all information about the current browser request, including cookies, HTML form variables, and more. 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 session objects in ASP, the session object in JSP is very useful for applications that do you want to complete a transaction through multiple pages.

To illustrate the specific application of the session object, let's simulate a multi-page web application with three pages. The first page (Q1.html) only contains an HTML form requesting the username, the code is as follows:

please enter your name:

The second page is a JSP page (Q2.JSP), which extracts the THENAME value in the Q1.html form via the Request object, stores it as Name variable, and saves this Name value into the session object. The session object is a collection of name / value, here, the name / value pair is "tell", the value is the value of the Name variable. Since the session object is always valid during the session, the variable saved here is also valid on the subsequent page. Another task of Q2.jsp is to ask the second question. Here is its code:

<% @ page language = "java"%>

<%! String name = "";%>

<%

Name = Request.getParameter ("thename");

Session.putValue ("Thename", Name);

%>

Your name is: <% = Name%>

What do you like to eat?

The third page is also a JSP page (Q3.JSP), the main task is to display the question and answer result. It extracts the value of the THENME from the Session object and displays it to prove although the value is entered in the first page, it is reserved by the Session object. Another task of Q3.jsp is to extract the user input in the second page and display it:

<% @ page language = "java"%>

<%! String food = "";%>

<%

Food = Request.GetParameter ("Food");

String name = (string) session.getValue ("thename");

%>

Your name is: <% = Name%>

You like to eat: <% = food%>

转载请注明原文地址:https://www.9cbs.com/read-21612.html

New Post(0)