Learn jsp, this one is enough

1. Origin

jsp (Java Server Pages), Java server page, a technology defined by SUN for developing dynamic web resources.

In many dynamic web pages, most of the content is fixed and only part of the content needs to be dynamically generated and changed.

If a Servlet program is used to output a web page whose only partial content needs to be dynamically changed, and all the static content needs to be generated by the programmer with Java program code, the code of the entire Servlet program will be very bloated, and it will be very difficult to write and maintain.

The art design of a large amount of static content and the writing of related HTML statements are not the work of the programmer, and the programmer is not necessarily good at it. Web page art designers and production staff do not understand Java programming, and they cannot do this kind of work.

In order to make up for the shortcomings of Servlet, Sun introduced JSP (Java Server Pages) technology as a solution on the basis of Servlet.

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 controller component in web applications, and use JSP technology as a data display template.

The reason is that the program data is usually output after beautification:

Let jsp use java code to generate dynamic data, and beautify it will make the page difficult to maintain.

Let the servlet not only generate data, but also embed html code in it to beautify the data, it will also lead to poor program readability and difficult to maintain.

Therefore, the best way is to let them be responsible for each according to the characteristics of these two technologies. The servlet is only responsible for generating data in response to requests and bringing the data to jsp through the forwarding technology.Data display jsp to do

2. Grammar

The essence of jsp is servlet

It is different from HTML pages. In addition to writing tags, jsp pages can also write Java code between <% %>. Use <%= %> to output specified content to the page.

such as:

After the backend obtains the data, it passes
request.setAttribute("list",list);
request.getRequestDispatcher("/index.jsp").forward(request,responce);

Get data on the index.jsp page:
<%=request.getAttribute("list")%>

Three, Page command

page (used to configure page information)
Insert picture description here
(1) contentType: set the type and character set of the response body, which is equivalent to the parameter value of respond.setContentType()
(2) language: there is only one value Java, which is more tasteless
(3) import: Like the Java source code, it is used to guide the package and class, such as the following example

Insert picture description here
(4) errorPage: the page that will jump to when an exception occurs on the current page
(5) isErrorPage: identify whether the current page is an error page, when the value is true, the built-in object exception can be used;
(6) pageEncoding: set the current jsp The character set of the file

(7) autoFlush: Set whether to automatically refresh the buffer when the out output stream buffer is full, that is, after the buffer is full, the buffer data will be written out and then put in the data. The default is true.
(8) buffer: set the size of the out buffer, the default is 8kb
(9) session: set whether to create an HttpSession object when accessing the current jsp page, the default is true
(10) set who the Java class translated by jsp inherits by default

Four, static inclusion and dynamic inclusion

1. Static inclusion
Insert picture description here
Features of static inclusion:

  • The included jsp page will not be translated
  • Copy the included page code to the corresponding location

2. Dynamic inclusion
Insert picture description here

  • The page attribute specifies the path of the included jsp page
  • Dynamic inclusion will translate the included jsp page into a Java file
  • The bottom layer will call the code to output the included pageJspRuntimeLibrary.include(request,response,"/include/footer.jsp", out,false);
  • Dynamic inclusion can pass parameters to the included page
    Insert picture description here

Five, request forwarding

Insert picture description here

Six, nine built-in objects

The built-in objects in jsp refer to the nine objects provided internally by Tomcat after translating jsp into servlet source code;

Insert picture description here
The Exception object is only generated when an exception occurs on the JSP page, or the isErrorPage attribute is set to true in the page instruction;

Seven, four domain objects

Insert picture description here

Eight, the difference between out and respond.getWriter().write()

The content output by out will be saved in the out buffer first;
the content output by responce will be saved in the responce buffer;
when all the contents of the jsp page are executed, out.flush() will be executed; the data in the out buffer will be appended to the responce The end of the buffer;
perform the refresh operation of responce, and send all data to the client;
Insert picture description here
in the JSP is translated into Java source files, all output is used, so we generally best use the out object for output; avoid data sequence An error occurred.

out.write(): Only suitable for outputting strings
out.print(): After observing the source code, it is found that this method is to convert other types of data into string types, and then call the write() method to output.

When out.write() outputs a number, the bottom layer will convert the number to the corresponding char type, so what is displayed on the page is no longer a number; it is only suitable for outputting strings;
therefore, we can use out.print( ) Method, never go wrong;

Guess you like

Origin blog.csdn.net/qq_41504815/article/details/114986164