Today is the ninth day of my study in Lebyte education, the main content of today’s study is JSP.

JSP: Java Server Page Dynamic Web programming technology provided by SUN is a dynamic resource on Java Web Server.

Compared with HTML, which can only provide static data to users, Jsp technology allows you to nest Java code in a page to provide dynamic data to users.

Compared to servlets, which are more difficult to format data, JSPS make it easy to format data in addition to generating dynamic data in Java code.

Both JSPS and servlets can be used to develop dynamic Web resources, though. However, due to the respective characteristics of these two technologies, in the long-term software practice, people gradually use servlet as a controller component in Web applications, and JSP technology as a data display template to use.

A Jsp is actually a Servlet, and when we first access a Jsp, the Jsp engine will translate the Jsp into a Servlet, which is stored in the Work directory of Tomcat (source directory).

The IDEA configuration step is not necessary, although some of the default configuration items in the editor are not perfect, such as “encoding format”, page templates, etc. We can modify the JSP page to what we need before we create it.

1. Choose “File” – > Settings…

2. Set the encoding format. Search for “encode”, select “File Encoding”

,

3. Set the page template. Search for “Template”, select “File and Code Templates”, select “Other” on the right, select “Jsp File” on the bottom

New JSP page annotations Support syntax operations for two types of annotations in JSP:

One is to display comments that are allowed to be seen by the client. The other is implicit comments, which are invisible to the client

Display comment syntax: inherited from the HTML style

Implicit comment syntax: Inherit from JAVA style; JSP’s own annotations

JSP annotation in three ways:

1) // comment, single line comment /* multi-line comment */

2)

The most important part of a JSP is the Scriptlet, any Java program embedded in HTML code.

There are three types of Scriptlet code in a JSP: all must be marked with a Scriptlet

First: <% %> : Java script segment, can define local variables, write statements

Second: <%! %> : declaration that defines global (member) variables, methods, and classes

The third type: <%= %> : expression, data a variable or concrete content to understand three small scripts by observing JSP code that is parsed into Java files

<%@ page language=”java” contentType=”text/html; charset=UTF-8″ pageEncoding=”UTF-8″%>

This is a JSP page!!

Method 1: In each JSP page (HTML) include toolbar, header information, tail information, specific content

Method 2: Separate the toolbar, header information, and tail information into independent files and import them directly

Obviously, the second method is better than the first one, the first one will have a lot of repeated code, and modification is not convenient, in JSP if you want to achieve the operation of including, there are two ways: static include, dynamic include, static include using the include directive, dynamic include is the need to use the include action tag.

Static include <% @include file=” file path to include “%> For example:

<%@include file=”include.jsp” %> or <%@include file=”include.html” %> Static inclusion is a direct substitution of content, just like variables defined in the program, when translated by the servlet engine, The contents of this file are included (the source code for both files is combined into the _jspService method), so only one servlet is generated, so the two pages cannot have variables with the same name. Run a little bit more efficiently. High coupling, not flexible enough.

Include dynamic includes are two separate parts of code at compile time. The included and included parts are included dynamically only at run time, such as method calls.

< JSP :include page=”include. JSP “></ JSP :include> </ JSP :include>

<jsp:include page=”include.html”></jsp:include> <% String a = “hello.jsp”; %> < JSP :include page=”<%=a %>”></ JSP :include> Use dynamic include also by passing parameters between pages.

Receive parameters via request.getParameter(name);

<jsp:include page=”hello.jsp” flush=”true”> <jsp:param name=”uname” value=”zhangsan”/> </jsp:include> ​ hello.jsp

<%=request.getParameter(“uname”)%> JSP provides four kinds of properties to save scope, the so-called property save scope, refers to a set of objects, how many pages can be saved and can continue to use

Page range

PageContext: Saves attributes only on one page and is invalid after a jump

The request scope

Request: Saved in a single request and still valid after a switch to the server

The session scope

Session: Any jump can be used within a session scope

Scope of application

Application: Saves on the entire server

Method Type Description public void setAttribute(String name, Object O) Common Attribute name and content Public Object getAttribute(String Name) Common Attributes are obtained based on the attribute name public void removeAttribute(String name) Common Delete the specified property verify the characteristics of the property range page This page is obtained, server side jump () is invalid

request

The server redirect is valid, but the client redirect is invalid

If the client jumps, it makes two requests, and the first request does not exist. If you want to save both client and server jumps, you need to expand the scope further.

session

It can be accessed from both the client and server, but if you restart a new browser, you will not be able to retrieve the session set before, because each session is only saved in the current browser and retrieved from the relevant page.

For a server, each client that connects to it is a session

If you want the property to be set once and available regardless of whether a new browser is opened, you can use Application

application

All application properties are stored directly on the server and can be accessed by all users (each session)

As long as the properties are set by application, all sessions can be obtained, representing the common content. However, if the server is restarted, it cannot be obtained, because all the properties disappear after the server is shut down, so it needs to be set again.

Q: Which range to use?

A: As small as is reasonably possible

The syntax EL (Expression Language) uses EL expressions to make JSP writing easier. The expression language, inspired by the ECMAScript and XPath expression languages, provides a way to simplify expressions in JSPS, making the code of JSPS even simpler.

The syntax is very simple: ${expression} EL normally operates on data in a domain object, not local variables.

The concept of domain object in JSP has four: pageContext, Request, Session, Application; The range is, in order, this page, one request, one session, the entire application.

When you need to specify that you want to find data from a specific domain object, you can use the space objects corresponding to the four domain objects: pageScope, requestScope, sessionScope, applicationScope.

The default search mode of EL is from small to large. Returns an empty string “” when all the field objects have been found.

The use of the EL expression gets data to set the data in the domain object

<% pageContext.setAttribute(“uname”,”zhangsan”); // Page scope request.setAttribute(“uname”,”lisi”); // Request scope session.setAttribute(“uname”,”wangwu”); // session scope application.setAttribute(“uname”,”zaholiu”); // application %> get the value of the domain object

<%– Get the data in the domain object: the default lookup is from small to large, as long as you find it. If none of the four ranges is found, an empty string is returned. –%> ${uname} gets the value of the specified domain object

pageScope.uname
{pagescope.uname}
pageScope. Uname
{requestscope. uname} sessionscope. uname
{sessionscope. uname}
sessionScope. Uname
{applicationScope.uname} gets the List

<% List list = new ArrayList(); list.add(“aaa”); list.add(“bbb”); list.add(“ccc”); request.setAttribute(“list”, list); List [subscript] {List [subscript]} List [subscript] {list.size()} List represents the names of variables in the domain object –%> ${list[1]} get Map

<% Map map = new HashMap(); map.put(“aaa”, “111”); map.put(“bbb”, 2222); map.put(“ccc-a”, 333); request.setAttribute(“map”, map); % > < % – get a Map is specified in the values Map [” key “] {Map [” key “]} or Map [” key “] or {Map. Key} note: Map.aaa {map.aaa}map.aaa{map[” BBB “]} get JavaBean objects

User.java

public class User {

private Integer userId;
private String uname;
private String upwd;

public Integer getUserId() {
    return userId;
}

public void setUserId(Integer userId) {
    this.userId = userId;
}

public String getUname() {
    return uname;
}

public void setUname(String uname) {
    this.uname = uname;
}

public String getUpwd() {
    return upwd;
}

public void setUpwd(String upwd) {
    this.upwd = upwd;
}
Copy the code

} <% User user = new User(); user.setUserId(1); user.setUname(“zhangsan”); user.setUpwd(“123456”); request.setAttribute(“user”,user); % > < % — the property fields need to provide the get method in JavBean — % > user < {user} < % % – access object – > user < {user. The uname} < % % – object attributes – > the empty < % – the empty Determines whether the domain object is empty. Null, returns true; Return false if not null; Empty Determines whether the object is not empty. Empty determines whether the object is not empty. Empty Determines whether the object is not empty. {! Empty variable name} –%> emptyuname{empty uname}emptyuname{empty list} emptyMap {empty map} EMPtymap {empty user} EL operation <% request.setAttribute(“a”, 10); request.setAttribute(“b”, 2); request.setAttribute(“c”, “aa”); request.setAttribute(“d”, “bb”); %> equivalent judgment

< % – compare two values are equal, return true or false = = or eq — % > a = b = {} a = = b = = b} {c = = d ceqd eq d {c} ceqd {a = = 5} ${c = = ‘aa’} arithmetic operations

<%– addition: + subtraction: – Multiplication: * Division: / or div –%> a+b{a +b}a+b{a/b} or ${a div b} size comparison

<%– Greater than: > Less than: < greater than or equal to: >= Less than or equal to: <= –%> a>b{a > b}a>b{a + 1 > 10 } a+b>=10{a + b >= 10 }a+b>=10{a > b && b > 5 } ${a + b > 10 || a – b > 5 }