[JavaWeb] JSP knowledge points and JSP essence analysis

Summarize the relevant knowledge points and use cases of JSP+EL expressions;

Table of contents

What are JSPs?

The relationship between JSP and Servlet

 The role of JSP

 JSP syntax format

 How to write Java program in JSP

JSP output statement

Summary of JSP Basic Grammar

Nine built-in objects of JSP


 What are JSPs?

What are JSPs?

  • JSP is a Java program. (JSP is essentially a Servlet)

  • JSP is: the abbreviation of JavaServer Pages. (A server-side page implemented based on the Java language.)

  • Servlet is one of the 13 sub-specs of JavaEE, so JSP is also one of the 13 sub-specs of JavaEE.

  • JSP is a set of specifications. All web containers/web servers follow this set of specifications and "translate" according to this set of specifications. Each web container/web server will have a built-in JSP translation engine.


The relationship between JSP and Servlet

We write an index.jsp in the web directory, and then when we access index.jsp in the browser, we will find that index.jsp will automatically translate and generate index_jsp.java, and will automatically compile and generate index_jsp.class, then we can get index_jsp It is a class.

Then we will find that the index_jsp class inherits from HttpJspBase, and the HttpJspBase class inherits from HttpServlet. So it can be concluded that the index_jsp class is a Servlet class

Then we find that JSP is essentially a Servlet, and we will have a question;

What is the difference between JSP and Servlet?

Different responsibilities:

  • What is the Servlet's responsibility: to collect data. (Servlet's strengths are logic processing, business processing, and then linking to the database to obtain/collect data.)
  • What is the responsibility of JSP: display data. (The strength of JSP is to display data)

 The role of JSP

        The role of JSP is to display the effect, how to display it? It is to return the data of the HTML page by replacing the Servlet program. What does it mean? That is, we have written some additions, deletions, modifications, and queries to the data table in the Servlet program we wrote, then we will get some data after executing the corresponding Servlet, so how to make the data we get can be obtained and displayed in HTML? ? At this time, JSP technology is needed;


 JSP syntax format

  • Directly write ordinary strings in JSP

    • Translate to out.write("here") of the service method

  • <%%>

    • Translated into the body of the service method, there are java statements one by one.

  • <%! %>

    • Translated outside the service method.

  • <%= %>

    • Translated into the body of the service method, the translation is: out.print();

  • <%@page contentType="text/html;charset=UTF-8"%>

    • The page directive is used to set the content type of the response through the contentType attribute.

If we directly write text in the jsp file, where will it be automatically translated?

  • It will be translated into the out.write("Translate to here") of the service method of the servlet class, directly translated into double quotation marks, and will be printed and output to the browser as a normal string by the java program.

What is HTML CSS JS code to JSP?

  • These codes are just an ordinary string to JSP. But once JSP outputs this ordinary string to the browser, the browser will interpret and execute HTML CSS JS. show an effect.

JSP directive

  • The role of the instruction: to guide the JSP translation engine how to work (to guide the current JSP translation engine how to translate JSP files.)

  • What are the instructions?

  • Include instruction: Include instruction, complete static inclusion in JSP, rarely used. (not here)
  • taglib instruction: Instructions for importing tag libraries. Learn this when you go to the JJSTL tag library. Never mind for now.
  • page command: currently focus on learning a page command.
  • What is the syntax for using the directive?

  • <%@command name attribute name=attribute value attribute name=attribute value attribute name=attribute value....%>

case:

<%@page contentType="text/html; charset=UTF-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

Common usage and common instructions of JSP page instructions

Common usage of JSP page instruction: Solve the problem of Chinese garbled characters in the response: set the content type of the response through the page instruction, and add at the end of the content type: charset=UTF-8

<%@page contentType="text/html;charset=UTF-8"%>
  • <%@page contentType="text/html;charset=UTF-8"%>, indicating that the content type of the response is text/html, and the character set used is UTF-8;

Common usage of JSP's page directive: import statement, guide package.

<%@page import="java.util.List, java.util.Date, java.util.ArrayList" %>
<%@page import="java.util.*" %>

 


 So how to write Java program in JSP

  • <% java statement; %>

    • What is written in this symbol is regarded as a java program and is translated into the service method of the Servlet class.

    • Here you have to be careful, you have to think, when writing java code in the <% %> symbol, you have to always remember that you are writing code in the "method body", what can be written in the method body, not What can you write, do you understand it in your heart?

    • The code written in the service method is sequential, and the code in the method body must be executed line by line in a top-down order.

    • In the service method, you cannot write static code blocks, methods, or define member variables. . . . . .

    • The <%%> symbol can appear multiple times in the same JSP.

  • <%! %>

    • Java programs written in this symbol will be automatically translated outside the service method.

    • This syntax is rarely used, why? It is not recommended to use it, because writing static variables and instance variables outside the service method will have thread safety issues, because JSP is a servlet, and a servlet is a singleton. In a multi-threaded concurrent environment, once this static variable and instance variable are modified , there will inevitably be thread safety issues.

How to output a java variable to the browser.

<% String name = “jack”; out.write("name = " + name); %>
  • Note: out in the above code is one of the nine built-in objects of JSP. It can be used directly. Of course, it must only be used inside the service method.

  • If there is no "java code" in the content output to the browser, for example, the output string is a fixed string, which can be directly written in jsp without writing to <%%> here.

  • If the output contains "java code", the following syntax format can be used at this time:

    • <%= %> Note: Write the content to be output after =.

    • Where will the <%= %> symbol be translated? What did it end up being translated into?

      • Translated into this java code: out.print();

      • Translated into the service method.

    • When to use <%=%> output? The output content contains java variables, and the output content is a dynamic content, not a dead string. If the output is a fixed string, it can be written directly in the JSP file.

How to Write Professional Comments for JSP in JSP

  • <%--JSP professional comments will not be translated into java source code. --%>

  • <!--This kind of comment belongs to HTML comment, and this comment information will still be translated into java source code, which is not recommended. -->

Nine built-in objects of JSP

  • jakarta.servlet.jsp.PageContext pageContext page scope
  • jakarta.servlet.http.HttpServletRequest request request scope
  • jakarta.servlet.http.HttpSession session session scope
  • jakarta.servlet.ServletContext application application scope

The above four are all domain objects, and the scope is from small to large: pageContext < request < session < application

The above four scopes have: setAttribute, getAttribute, removeAttribute methods.

The principle of using the above scope: use as small a scope as possible.

  • java.lang.Throwable exception
  • jakarta.servlet.ServletConfig config
  • java.lang.Object page (actually this, the current servlet object)
  • jakarta.servlet.jsp.JspWriter out (responsible for output)
  • jakarta.servlet.http.HttpServletResponse response (responsible for the response)

Guess you like

Origin blog.csdn.net/m0_64231944/article/details/129391049