Today is the 92nd day for Liu Xiaoai to learn Java by herself.

Thank you for watching. Thank you.

Without further ado, let’s begin today’s lesson:


First of all: JSP technology is basically obsolete, not very important, the possibility of using it is very low, but some enterprises will still use it.

Take a day to understand it briefly.

First, JSP overview

The reason for the emergence of 1JSP

JSP the emergence of this technology is to solve a problem?

Let’s look at the following:


In Java code, the server responds to an HTML page to the browser by concatenating tags into the code.

Such a meal operation down will appear very cumbersome, operation trouble not to say, reading is poor.

Is there any way to solve this problem?

JSP technology came into being, in JSP files, can write Java code, and can write HTML code, especially powerful.

2 the JSP defined

JSP, full name Java Server Pages, translates to Java Server Pages and is essentially a simplified Servlet.

It is a dynamic web technology standard initiated by Sun and established with the participation of many companies.

Plain English is a file that can write both Java code and HTML code.


Why did 3JSP become obsolete?

JSP was not phased out per se because of technological backwardness; it was phased out because of industry trends.

Now emphasize the separation of the front and back end, front-end to write front-end code, back-end to write back-end code, there is no need to front-end and back-end code fusion together, so JSP use is limited.

It’s not that no one’s using it at all, it’s just that they’re using it less.

JSP syntax

Write code in JSP


① Comment format

The format of a comment in JSP is <%– comment –%>

②Java code writing

The format is <%Java code %>, in which Java code can be written.

Once you’ve done this, type the path in the browser and you’ll see that you can use Java syntax to type in the browser.

That completes writing Java code in a JSP file.

2JSP execution process

JSP file why can write Java code, its underlying is what kind of an execution process? Drawing and explaining:


① The browser is demo01.jsp

Access the JSP file based on the corresponding path, and the JSP file is converted into two files:

Demo01_jsp. Java and demo01_jsp. Class.

These two files are hardly familiar to Java developers: one is a Java source file and the other is its bytecode file.

That is, JSP files are actually converted to Java files at the bottom, and then executed Java code.

② On the converted Java source code

Open the corresponding Java source file, of course, I only screenshot a part of the illustration above, in fact, the source is much more than that.

Part of the code in the screenshot can be seen:

  • The code in <%%> is parsed directly into Java code.

  • The HTML portion is concatenated as a string by the out.write(“”) method, which responds to the browser.

All the way around is actually concatenation, the same way we started, except that the JSP concatenation is encapsulated and we don’t have to write it.

Three ways to write Java code


① Script declaration

Format: < %! Write Java code %>

There’s an exclamation mark in the middle, and you can write Java code in there.

If you look at the corresponding Java source file, you’ll see that this piece of code corresponds to member variables and member methods in the source code

② Script snippets

Format: <% Write Java code %>

It has one less exclamation point than ① and corresponds to the Java code for the _jspService method in the source code.

In Java, there is no way to define a new method inside a method, so the method definition has to be written in the ① format.

③ Script expressions

Format: <%=” expression “%>

The corresponding Java code is out.print().

3. EL expression

EL expressions are specifically designed to replace ③ script expressions in the above three formats.

The format is ${STR}. This is equivalent to <%= STR %>, where STR is a variable.

1 Four domain objects

In order of size:

  • Page field: valid only on the current page.
  • Request domain: Valid only in a single request or request domain.
  • Session domain: Valid during a session (one or more requests and responses).
  • Application domain: valid throughout the project.

2 The value can be one of the four domain objects


① Set the value of the domain object

The methods used are setAttribute(), where parameters are stored as key-value pairs,

(2) Common mode

Take the page field object as an example. The format is:

${pageScope.pageKey}

PageKey is the key corresponding to the page field object. This method can be used to fetch the value of the field object.

③ The value of shorthand

For example, the page field object is ${pageKey}.

I’ve simplified the pageScope, which is just a key.

But then there’s the problem that the key might be duplicated.

After all, keys are artificially named, and keys in a Page domain object can be the same as keys in a Request domain object.

If the key is repeated, the search is performed in ascending order.

3 The value is obtained from the Cookie


① Save the Cookie to the browser

Save two cookies to the browser in LoginServlet:

  • UsernameCookie: the name is username and the value is the data in the request, that is, the username entered on the login page.
  • PasswordCookie: the name is password and the value is the data in the request, that is, the password entered in the login interface.

② Fetch the corresponding value of Cookie

In the example of usernameCookie, the key in the Cookie is usename and the value is based on the key.

Format: ${cookie.username. Value}

What’s the good of that? Take a test:


After the user name and password are entered for the first login, the data will be stored in cookies, which will be read by the page.

When you refresh again, the username and password will appear automatically, so you do not need to enter the username and password again.

The last

Thanks for watching.

If you can, please give it a thumbs up. Thank you.

This article is formatted using MDNICE