Study notes 5: JSP and JSTL (on)

JSP 与 JSTL

Introduction to jsp

jsp: The full name java Server Page is a dynamic web page programming technology provided by Sun.

Compared with html, html can only provide static page data, while JSP allows java code to be embedded in the page to provide users with dynamic data.

Compared with Servlet, Servlet is difficult to typeset data, and JSP can use java code to generate dynamic data, but it is also easy to typeset data.

Whether it is jsp or servlet, although it can be used to develop dynamic web resources. However, due to the respective characteristics of these two technologies, in long-term software practice, people gradually use Servlet as a control component in web applications, and use jsp technology as a data display template.

In fact, jsp is a servlet. When we visit jsp for the first time, the jsp engine will translate this jsp into a servlet. This file is stored in the work directory of tomcat.

It is recommended to configure the encoding format in idea before use to prevent garbled code:
Insert picture description here
JSP is exactly the same as HTML, except that JSP can write embedded java code!

Annotation

There are two kinds of comment syntax supported in JSP: one is to display the comment, which can be seen on the client side (that is, it can be seen in the browser developer tool), and the other is an implicit comment. The client cannot see it.

  1. Display comment syntax: inherited from HTML style
  2. Implicit comment syntax: inherited from JAVA style; JSP's own comment
// 单行注释

/*
	多行注释
*/

以上两种只能在java程序段中编写

<!--  html风格的注释 -->

<%--
	jsp的注释
--%>

Basic Scriplet Grammar

The most important part in jsp is Scriplet ( script applet ), that is, all JAVA programs embedded in HTML code .

There are three kinds of Scriplet codes in JSP: all must be marked with Scriplet

第一种:<%     %> :java 脚本段,可以定义局部变量,编写语句。
生成的代码在servlet中的service方法体中

第二种:<%!    %> :声明。可以定义全局(成员)变量、方法、类  //一般用来定义全局变量
生成的代码在类中

第三种:<%=    %> :输出表达式,数据,一个变量或具体内容
生成的代码在servlet中的service方法体中,相当于out.print();语句

jsp command label

Using the include operation, you can include the repeated code for continued use, because some pages only need a part of the page to be changed, and we can use the include operation to join the pages without changing the pages. There are two ways to achieve the above functions.

Method 1: In each jsp page (html) include the toolbar, header information, tail information, specific content, that is, do not use include

Method 2: Divide the toolbar, header information, and tail information into separate files, and import them directly when using them

Obviously, the second method is better. The first method will have a lot of redundant duplicate code, and it is not convenient to modify. If jsp wants to implement the included operation, there are two methods: static inclusion , dynamic inclusion , and static inclusion use The include directive is sufficient, and the dynamic inclusion requires the use of the include action tag.

1. static includes : static include is the main file and the file contained by the servlet engine when translated directly to these files source code integrated together, all on jspService method , it only generated a Servlet source File , so there can be no variables with the same name in these files .

Small link: In the web project, the compiled class and java files of the jsp in the idea will be generated under the tomcat path associated in the idea. This path can be obtained by viewing the tomcat startup information through the console, such as:
Insert picture description here
continue to say:

Advantages: slightly higher operating efficiency
Disadvantages: high coupling, not flexible enough

The format is as follows:

<%--静态包含--%>
<%@include file="header.jsp"%>

<h2>主题内容</h2>

<%@include file="footer.jsp"%>

2. Dynamic inclusion :

The format is as follows:

<%-- 动态包含--%>
<jsp:include page="header.jsp"></jsp:include>

<h2>主题内容</h2>

<jsp:include page="footer.jsp"></jsp:include>

Note: When the dynamic include does not need to pass parameters, there should be no content between the include double tags, including line breaks and spaces .

If you need to pass parameters, you can use the method of dynamically including passing parameters:

<%-- 动态包含传递参数: value值支持表达式,而name值不支持表达式--%>
<jsp:include page="footer.jsp">
    <jsp:param name="namem" value="admain"/>
    <jsp:param name="msg" value="<%= str%>"/>
</jsp:include>

Get the passed parameter:
use request.getParameter(String name)
to get the passed parameter by specifying the parameter name

Analysis of dynamic containment technology : This containment technology uses the invocation method equivalent to the method to load these contained pages, which will generate multiple source code files, so several pages do not interfere with each other, so variable names can be repeated. as follows:
Insert picture description here

Four domain objects of JSP

There are four types of attribute storage scopes provided in jsp. The so-called attribute storage scope refers to a set domain object, how many pages can be saved and continue to be used, and human words are the scope of data sharing.

  1. Page scope
    pageContext: Only save attributes in the current page, and can’t be shared after the jump (jsp:forward)
  2. The request range is
    only saved in one request. It is still valid after the server jumps. The client jump is invalid.
    If it is a client jump, it is equivalent to two requests. Then the first request does not exist. If you want If both types of jumps can get the data, the scope needs to be expanded.
  3. The scope of
    the session is valid in a session, and it can be obtained regardless of any jump.
    Because when the browser is closed, the session is over, so if you want to set the attribute once, you can get it regardless of whether it is a new browser or a new session. To the data, then application should be used.
  4. The scope of application
    is stored on the entire server.
    All users (every session) can get data, but if the server restarts, the data stored on the server disappears.

Guidelines for using domain objects: try to use the smallest range within a reasonable range

Guess you like

Origin blog.csdn.net/qq_40492885/article/details/115279070