JAVAWEB-JSP, JSP's three major instructions, nine implicit objects, action tags

1. The operating principle of jsp-the essence of jsp is servlet (interview)

Jsp will be translated into a servlet by the web container and then executed when it is accessed for the first time

Process:
First visit -> helloServlet.jsp (Translated into Servlet)——>helloServlet_jsp.java(Compiled Servlet)——>Compile and run

PS: The translated servlet can be found in the work directory of Tomcat

Two, jsp scripts and comments

jsp script :

  • <% java代码 %> —— The java code inside is translated into the service method
  • <%=java变量或表达式> ——Translated into out.print() inside the service method, directly output content
  • <% ! java代码%>-Translated into the content of the servlet members.

jsp note : The visible range of different notes is different

  • Html notes: <!--注释内容-->——Visible scope: jsp source code, translated servlet, browser page display html source code

  • Java notes: //单行注释 /*多行注释*/——Visible scope: servlet translated from jsp source code

  • jsp notes: <%--注释内容--%>——Visible range: JSP source code is visible

Case:

<%@ page contentType="text/html;charset=UTF-8"language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <!--html注释-->
  <h1>
    JSP页面
  </h1>
  <%
    int i=0;
    System.out.println(i);
  %>
  <%=i%>
  <%=1+1%>
   //java注释
    /*java多行注释*/
  <%--jsp注释--%>
  <%!
     String str = "hello china";
  %>
  <%=str %>
  </body>
</html>

3. JSP instruction-(three major instructions)

The instructions of jsp are commands that guide the translation and operation of jsp. Jsp includes three major instructions:

1.page instruction

  • page指令——The instruction with the most attributes (the page instruction is the default in actual development)
    The instruction with the most attributes guides the characteristics of the entire page according to different attributes
  • format:<%@ page 属性名1= "属性值1" 属性名2= "属性值2" ...%>

Common attributes are as follows

  • language: Types of languages ​​that can be embedded in jsp scripts
  • pageEncoding:The encoding of the current jsp file—contentType can be included inside
  • contentType:response.setContentType(text/html;charset=UTF-8)
  • session:Whether jsp automatically creates session during translation
  • import: Import the java package. For example, if you want to import a List, an error will be reported, and the package will not be imported at the beginning.
  • errorPage: Which page to jump to when the current page goes wrong.
  • isErrorPage: The current page is an error-handling page. For example: when error.jsp=”true”. Indicates that it is currently an error page.
  • extends: For example, write "java.util.ArrayList". At this time, the jsp file will report an error. It is not commonly used in development and generally does not inherit casually.

Case:

Note: pay attention to some common page instructions on the jsp, and at the same time we set the current page as a page that handles errors

<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" session="true" isErrorPage="true"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%List<String> list =new ArrayList<String>(); %>
<%session.setAttribute("name","zhangsan"); %>
<% out.print("this is errorPage"); %>
</body>
</html>

Note: At this time, when we access resources that do not exist under the project, we will jump to the current page.And we need web.xmlto write something in as follows:

  <error-page>
        <error-code>404</error-code>
        <location>/jspdemo.jsp</location> <!--表示jsp的文件名-->
    </error-page>

The effect is as follows:

Insert picture description here

2. include directive

Page includes (static include) instruction, you can include a jsp page into another jsp page

format:<%@ include file="被包含的文件地址"%>

For example: we create a demo.jsp, and we write in index.jsp

<body>
<%@include file="demo.jsp"%>
</body>

At this point we visit index.jsp, the content inside will contain the content of demo.jsp

3. taglib instructions

Introduce the tag library (jstl tag library, struts2 tag library) in the jsp page

format:<%@ taglib uri="标签库地址" prefix="前缀"%>

Similar to the introduction of css and JavaScript files in the front end

Four. jsp built-in/implicit objects (9)-(important)

After jsp is translated into servlet, there are 9 objects defined and initialized in the service method, we can use these 9 objects directly in the jsp script

name Types of description
out javax.servlet.jsp.JspWriter For page output
request javax.servlet.http.HttpServletRequest Get user request information
response javax.servlet.http.HttpServletResponse The server's response message to the client
config javax.servlet.ServletConfig Server configuration, initialization parameters can be obtained
session javax.servlet.http.HttpSession Used to save user information
application javax.servlet.ServletContext Shared information for all users
page java.lang.Object Refers to the instance of the Servlet class converted from the current page (note the beginning of the case below)
pageContext javax.servlet.jsp.PageContext JSP page container
exception java.lang.Throwable Indicates the exception that occurred on the JSP page, which only works in the error page

1. out object

Type of out: JspWriter

The role of out is to output content for the client-out.write()

The out buffer is 8kb by default and can be set to 0, which means that the contents of the out buffer are closed and written directly to the response buffer

Case:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
 aaaaaaaaaaaaaa
        <%   out.write("bbbbbbbbbbbbbbbbbb");
           	 response.getWriter().write("ccccccccc");
        %>
        <%="ddddddddddddd" %>
		<%  out.print("ffffffff");     %>
</body>
</html>

The effect is as follows: The
Insert picture description here
reason why ccccc is output first is:The Tomcat engine gets the content from the response buffer and then to the out buffer

2. pageContext object

Page object and pageContext object are two objects, pay attention to the difference
The context object (pageContext object) of the jsp page is as follows:

pageContext is a domain object

Like other scopes, pageContext also has the following three methods:

  • setAttribute(String name,Object obj)
  • getAttribute(String name)
  • removeAttrbute(String name)

pageContext can access data to other specified domains

  • setAttribute(String name,Object obj,int scope)
  • getAttribute(String name,int scope)
  • removeAttrbute(String name,int scope)

Case:

<body>
   <%
//使用pageContext向request域存数据
request.setAttribute("name","zhangsan");
//将name =zhangsan,设置到reques的域中,该行代码与上句代码作用一样
		pageContext.setAttribute("name","lisi",PageContext.REQUEST_SCOPE);
		//使用pageContext向session域存数据
        pageContext.setAttribute("name","wangwu",PageContext.SESSION_SCOPE);
        //使用pageContext向application域存数据
        pageContext.setAttribute("name","tianqi",PageContext.APPLICATION_SCOPE);
    %>
</body>

The pageContext object has a more powerful function that can automatically find Attribute

  • findAttribute(String name)

Get the attributes from the pageContext domain, request domain, session domain, and application domain in turn. After getting it in a certain domain, it will not look back.

Case:

body> 
<%
 //使用pageContext向request域存数据
request.setAttribute("name","zhangsan");
//将name =zhangsan  设置到reques的域中,该行代码与上句代码作用一样 pageContext.setAttribute("name","lisi",PageContext.REQUEST_SCOPE);
        pageContext.setAttribute("name","wangwu",PageContext.SESSION_SCOPE);
        pageContext.setAttribute("name","tianqi",PageContext.APPLICATION_SCOPE);
 %>
<%=request.getAttribute("name")%>
 //获得域中的属性
<%=request.getAttribute("name")%>>
//该方法域上面的request方法一样
<%=pageContext.getAttribute("name",PageContext.REQUEST_SCOPE)%>
//从所有域中找数据,小到大中搜索域中的范围中的name
<%--从小到大  pageContext域,request域,session域,application域--%>
<%=pageContext.findAttribute("name")%>>
</body>

Here is a comparison of the size of a domain

Summary of the four scopes:

  • page域:The current JSP page range
  • request域: One request, multiple forwarding is also counted as one request.
  • session域: One session, the page opens to the page closes.
  • application域: The entire web application

The pageContext object can get 8 other implicit objects

Case:

<%
    pageContext.getOut();
    pageContext.getRequest();
    pageContext.getResponse()
%>

Five. jsp label (action)

  • Page contains (dynamic inclusion):<jsp:include page="被包含的页面"/>
  • Request forwarding: the <jsp:forward page="要转发的资源" />address remains unchanged.

What is the difference between static and dynamic inclusion?

  • Static inclusion:<%@include file=”/”%>
  • Dynamic inclusion: <jsp.include page =”被包含的页面”/>
    Static inclusion is to piece together two jsp pages into one page. However, the dynamic page is two separate pages but not pieced together.

Case:
Make a dynamic inclusion

Step 1: Write the first page

body>
<%--include1中包含include2--%>
 <jsp:include page="include2.jsp"></jsp:include>
 <h1>This is include1 page</h1>
</body>

Step 2: Write the second page

<body>
    <h1>This is  include2 page</h1>
</body>

At this time, when we visit the first page, the content of the second page is included. There are actually two pages.

Guess you like

Origin blog.csdn.net/Mr_GYF/article/details/109176196