Nine basic built-in components of JSP

Time:2022-5-10

Source: provided by netizens. If you have any copyright problems, please contact us

Basic components

JSP has the following 9 basic built-in components (corresponding to 6 internal components of ASP):
Request client request, which will contain the parameters from the get / post request
The response page returns the response from the client
The properties of pagecontext web pages are managed here
Session the session duration associated with the request
What the application servlet is executing
Out is used to transmit the output of the response
Architecture part of config Servlet
Page JSP page itself
Exception refers to the wrong web page, which is an uncapped exception

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

Instead of using expressions, you can directly access the internal out object to print something to response:
<% out.println(“Hello”); %>
Instead of directly transmitting parameters to JavaBeans, you can obtain the value of parameters according to the request part:
<% String name=request.getParameter(“name”);

out.println(name); %>。

wait.

The following focuses on the session object.

Session state maintenance is a problem that web application developers must face. There are many ways to solve this problem, such as using cookies, hidden form input fields, or directly attaching status information to URLs. Java servlet provides a session object that is continuously valid between multiple requests, which allows users to store and extract session state information. JSP also supports this concept in servlets.
You can see many descriptions of implicit objects in sun’s JSP Guide (the implicit meaning is that these objects can be referenced directly without explicit declaration or special 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 implicit object. This object is automatically created when the first JSP page is loaded and associated with the request object. Similar to the session object in ASP, the session object in JSP is very useful for applications that want to complete a transaction through multiple pages.
To illustrate the specific application of session object, next we use three pages to simulate a multi page web application. The first page (Q1. HTML) contains only an HTML form that requires the user’s name, 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), which extracts Q1. JSP from the request object The name value in the HTML form, store it as a name variable, and then save the name value to the session object. The session object is a collection of name / value pairs. Here, the name in the name / value pair is “thename”, and the value is the value of the name variable. Since the session object is always valid during the session, the variables saved here are also valid for subsequent pages. q2. Another task of JSP is to ask the second question. Here 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). Its 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 on the first page, it is retained through the session object. q3. Another task of JSP is to extract the user input in 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>
What do you like to eat: <% = food% >
  < /BODY>
  < /HTML>

Recommended Today

VBS tutorial: introduction to regular expressions – common characters

Ordinary character Normal characters consist of all printed and non printed characters that are not explicitly specified as metacharacters. This includes all uppercase and lowercase alphabetic characters, all numbers, all punctuation, and some symbols. The simplest regular expression is a single ordinary character that matches the character itself in the search string. For example, the […]