Here is the JSP knowledge point that I sort down:

All of these points can be found in my other articles.

JSP often meet questions

The difference between JSP static and dynamic inclusion

The difference between JSP static and dynamic inclusion

  • In the interpretation of the request object, we have used the request. GetRequestDispatcher (String url). The include (request, response) to the header and footer contains

  • The inclue directive does the same thing, so let’s try it out!

  • This is the header

<%@ page contentType="text/html; Charset =UTF-8" language=" Java "%> < HTML > <head> <title> </title> </head> <body> <br> <br> </body> </ HTML >Copy the code
  • This is footer
<%@ page contentType="text/html; Charset =UTF-8" language=" Java "%> < HTML > <head> <title> </title> </body>Copy the code
  • Include headers and footers in 1.jsp
<%@ page contentType="text/html; Charset =UTF-8" language=" Java "%> < HTML > <head> <title> </head> <body> <%@include file="head.jsp" %> <%@include file="foot.jsp" %> </body> </html>Copy the code
  • Access to 1. The JSP

  • The include directive is a static include. Static inclusion means: include the code content of the file and compile it! , take a look at JSP source code to know!

  • As mentioned above, include directives are static and include actions are dynamic. Include behavior is actually encapsulates the request. GetRequestDispatcher (String url). The include (request, response)
  • The include behavior syntax looks like this

	<jsp:include page=""/>

Copy the code
  • Let’s start by including headers and footers in 1.jsp pages.
<%@ page contentType="text/html; Charset =UTF-8" language=" Java "%> < HTML > <head> <title> </head> <body> < JSP :include page="head. <jsp:include page="foot.jsp"/> </body> </html>Copy the code
  • Visit the 1.jsp page to see what it looks like:

  • Using JSP behavior to include files, JSP source files look like this:

  • JSP behavior containment files compile the included page and then write the results of the page to the included page (1.jsp)

  • Now, of course, there’s static inclusion and dynamic inclusion, so which one is better? The answer is: dynamic inclusion.

  • Dynamic include can pass parameters to the included page (not very useful) and handle the included page separately (write the compiled results of the included page into the included page). !

  • To simulate a scenario, my header page now has a string variable named S

<%@ page contentType="text/html; Charset =UTF-8" language=" Java "%> < HTML > <head> <title> </title> </head> <body> <% String s =" zhongfucheng"; <br> <br> </body> </ HTML >Copy the code
  • I also have a string variable named S at the bottom of the page
<%@ page contentType="text/html; Charset =UTF-8" language=" Java "%> < HTML > <head> <title> </title> </head> <body> <% String s =" zhongfucheng"; </body> </ HTML >Copy the code
  • Now I’m using static include to see what happens. I get an exception.

  • The cause of the exception is simple: there are two identical variables s in the same file

  • This can be avoided by using dynamic inclusion

conclusion

  1. <%@include file=”xxx. JSP “%> is a compilation instruction in JSP, whose file is included at the time of JSP to servlet conversion, and <%@include page=”xxx. JSP “> is an action instruction in JSP, whose file is included at the time of compilation. This is the time when Java files are compiled into class files

  2. Using static include produces only one class file, while using dynamic include produces multiple class files

  3. With static include, the containing page and the included page’s request object are the same object, because static include simply copies the contents of the included page into the included page. The dynamic include page and the included page are not the same page, the included page request object can get a relatively large range of parameters, not only can get the parameters passed to the contained page, but also can get the parameters passed down the contained page

What built-in objects does JSP have? What are the effects?

What built-in objects does JSP have? What are the effects?

Nine built-in objects:

  • pageContext
  • page
  • config
  • request
  • response
  • session
  • application
  • exception
  • out

Request, Response, Session, Application and Config have the same API as Servlet. I’m not going to explain these 5 objects.

Of particular importance in JSPS is the pageContext object.

PageContext is the most important of the built-in objects, and it represents the compiled content of the JSP page (that is, the runtime environment of the JSP page)!

The pageContext object

  • Since it represents the compiled content of the JSP page, it stands to reason that it encapsulates references to the other eight built-in objects! That is, the other 8 built-in objects are available via pageContext!
<%@ page contentType="text/html; Charset =UTF-8" language=" Java "%> < HTML > <head> <title> Get eight built-in objects </title> </head> <body> <% System.out.println(pageContext.getSession()); System.out.println(pageContext.getRequest()); System.out.println(pageContext.getResponse()); System.out.println(pageContext.getException()); System.out.println(pageContext.getPage()); System.out.println(pageContext.getServletConfig()); System.out.println(pageContext.getServletContext()); System.out.println(pageContext.getOut()); %> </body> </html>Copy the code
  • Take a look at the effect:

PageContext as the domain object

  • Methods like Request, Session, and ServletContext as domain objects have the following three methods:

    • setAttribute(String name,Objcet o)
    • getAttribute(String name)
    • removeAttribute(String name)
  • Of course, pageContext is no exception, pageContext also has these three methods!

  • PageContext essentially represents the compiled content of the current JSP page, and as a domain object, it represents the current JSP page (that is, page)! The pageContext field object is only valid within the scope of the page, not beyond the scope of the page!

  • First let’s see if it works in page scope

<%@ page contentType="text/html; Charset =UTF-8" language=" Java "%> < HTML > <head> <title> Use the Page domain object </title> </head> <body> <% pageContext.setAttribute("name", "zhongfucheng"); %> <% String value = (String) pageContext.getAttribute("name"); System.out.println(value); %> </body> </html>Copy the code
  • The effect is as follows:

  • Now let’s see if it doesn’t work outside the page scope!

  • Set the properties of the Request field object in 2.jsp

<%@ page contentType="text/html; Charset =UTF-8" language=" Java "%> < HTML > <head> <title> Request domain object Settings attribute </title> </head> <body> <% // This is what the Request domain object holds request.setAttribute("name","zhongfucheng"); % > < % - jump to 1. The JSP - % > < JSP: forward page = "1. JSP" / > < / body > < / HTML >Copy the code
  • PageContext attempts to retrieve the properties stored in request in 1.jsp
<%@ page contentType="text/html; Charset =UTF-8" language=" Java "%> < HTML > <head> <title> </title> </head> <body> <% // Attempts to get attributes stored in the Request domain object String value = (String) pageContext.getAttribute("name"); System.out.println(value); %> </body> </html>Copy the code
  • The effect is as follows:


  • PageContext essentially represents the content of a compiled JSP, pageContext can also encapsulate access to other fields!

  • The pageContext above is page-scoped by default, but the pageContext object overloads the set, GET, and removeAttribute methods

    • getAttribute(String name,int scope)
    • setAttribute(String name,Object value,int scope)
    • removeAttribute(String name,int scope)
  • One more parameter to set the scope of the field, if not specified default page. PageContext, of course, wraps the request, Session, Application, and Page fields around static variables for us to use.

    • PageContext.APPLICATION_SCOPE
    • PageContext.SESSION_SCOPE
    • PageContext.REQUEST_SCOPE
    • PageContext.PAGE_SCOPE
  • We can’t get the properties of the Request field object using pageContext when we didn’t use overloaded methods. Now let’s use the overloaded method to see if we can get it!

<%@ page contentType="text/html; Charset =UTF-8" language=" Java "%> < HTML > <head> <title> Get the properties of the Request domain object in the Page domain object </title> </head> <body> <% String value = (String) pagecontext.getAttribute ("name", pagecontext.request_scope); System.out.println(value); %> </body> </html>Copy the code
  • Effect:


  • PageContexst also has this method:

    • findAttribute(String name)
  • This method looks for the properties of each domain, starting from the smallest to the largest! Page -> Request -> Session -> Application.

  • We can use this method to find the properties of the request domain object.

<%@ page contentType="text/html; Charset =UTF-8" language=" Java "%> < HTML > <head> <title> Use findAttribute</title> </head> <body> <% / / use findAttribute find (2) in the JSP request a domain object attribute String value = (String) pageContext. FindAttribute (" name "); System.out.println(value); %> </body> </html>Copy the code
  • The effect is as follows:

The out object:

  • The out object is used to output data to the browser, and its counterpart is the PrintWriter object for the Servlet. However, the type of the out object is not PrintWriter, but JspWriter

  • A JspWriter is a PrintWrieter with a cache.

  • The out object works as follows:

  • Only to write the content, the out object and meet the following any one condition, the out object to invoke the ServletResponse. GetWriter method, The PrintWriter object returned by this method actually writes the contents of the out object’s buffer to the buffer provided by the Servlet engine:

    • Setting the buffer property of the Page directive turns off the caching of the OUT object
    • The buffer of the OUT object is full
    • The entire JSP page ends
  • We usually use expressions (<%=%>) for output in JSP pages, so we don’t use the out object very much!

Page object

The built-in page object is the HttpJasPage object, which represents the current JSP page, which is the object of the Servlet class compiled by the current JSP. In other words: the Page object is equivalent to the ordinary Java class this

The exception object

  • The built-in exception object is an object of the Java.lang. exception class, which encapsulates the exception information thrown by a JSP page. ** Exception is often used to handle error pages

  • Now that we’ve talked about how to set up the error page, let’s briefly use the Exception object

  • 1. The JSP page

<%@ page contentType="text/html; Charset =UTF-8" language=" Java "errorPage="error.jsp" %> < HTML >< head> <title></title> </head> <body> <% <% String sss = null; sss.length(); %> </body> </html>Copy the code
  • Error. The JSP page
<%@ page contentType="text/html; Charset =UTF-8" language=" Java "isErrorPage="true" %> < HTML > <head> <title> Error page </title> </head> <body> <% Out.println (" the program threw an exception: "+ exception); %> </body> </html>Copy the code
  • Effect:

conclusion

  1. Request A client request that contains parameters from a GET/POST request
  2. Response A response that the web page returns to the client
  3. The pageContext page property is managed here, representing the compiled JSP content
  4. Session Duration of the session related to the request
  5. What the Application Servlet is executing
  6. Out is used to transmit the output of the response
  7. Config framework for the servlet
  8. Page THE JSP page itself
  9. Exception An uncaught exception to an error page

JSP and servlet difference, common ground, respective application scope?

JSP and servlet difference, common ground, respective application scope?

  1. JSP is an extension of Servlet technology, essentially a simple way of Servlet. JSP is compiled to be servlet-like.
  2. The main difference between servlets and JSPS is that the application logic of servlets is in Java files and completely separated from the HTML in the presentation layer. In the case of JSP, Java and HTML can be combined into a file with a.jsp extension.
  3. JSPS focus on views, while servlets focus on controlling logic.

Property scope scope

Property scope scope

  1. Page [Save attributes in one page only, skipping pages does not work]
  2. Requet [Save attributes in one request only, server redirect is valid, browser redirect is invalid]
  3. Session [Save attributes in a session scope, regardless of the jump, not after closing the browser]
  4. Application [Save in the entire server, all users can use]

Application Scenarios:

  1. Request: If the client sends a request to the server, the data generated will be useless after the user finishes reading it. ** Data like this will be stored in the Request domain, such as news data, which will be useless after the user finishes reading it
  2. Session: If the client sends a request to the server, the generated data will be used by the user later, and such data will be stored in the session domain, such as the shopping data. The user needs to see his shopping information and wait a little while and use this shopping data to checkout
  3. ServletContext: If a client makes a request to the server, the resulting data is used up by the user and then used by another user, such data is stored in the servletContext domain, such as chat data

Write out five common JSTL tags

Write out five common JSTL tags


<c:if>, <c:item>, <c:foreach>, <c:out>, <c:set>Copy the code

What class does a custom tag inherit from

What class does a custom tag inherit from

We can implement custom tags in two ways:

  • Traditional way to implement Tag interface (old way)
  • The simple way is to inherit the SimpleTagSupport class

SimpleTagSupport class execution order (principle) :

  • ① The WEB container calls the setJspContext method of the tag handler object, passing the pageContext object representing the JSP page to the tag handler object
  • ② The WEB container calls the setParent method of the tag handler object, passing the parent tag handler object to the tag handler object. Note that the WEB container only calls this method if the tag has a parent tag.
  • ③ If the tag is called with a property set, the container calls the setter method for each property to pass the property value to the tag handler object. If the attribute value of the tag is an EL or script expression, the WEB container first evaluates the value of the expression and then passes the value to the tag handler object.
  • (4) If the simple tag has a body, the container calls the setJspBody method to pass in the JspFragment object representing the body
  • ⑤ Tag execution: the container calls the doTag() method of the tag processor, and the developer can execute, iterate, and modify the tag body by manipulating the JspFragment object inside the method body.

conclusion

SimpleTagSupport, usually calls the doTag method or implements the SimpleTag interface

How is a JSP executed? Is it less efficient than servlets?

How is a JSP executed? Is it less efficient than servlets?

  • When a client sends a request to a JSP page, the Web Container converts the JSP into the source code for the servlet (only on the first request), then compiles the transformed servlet and loads it into memory for execution, which responds to the client
  • JSPS are converted to servlets only on their first execution. Each subsequent execution, the Web container executes the compiled servlet directly, so JSPS and servlets are just different on their first execution. JSPS are slower, and subsequent executions are the same

How do I avoid JSP pages automatically generating session objects? Why would you do that?

How do I avoid JSP pages automatically generating session objects? Why would you do that?

You can explicitly turn it off using a page directive as follows:

<%@ page session="false" %>

Disadvantages of JSP?

Disadvantages of JSP?

  • 1) Bad debugging
  • 2) Interaction with other scripting languages (poor readability)

Tell the difference between servlets and CGI?

Tell the difference between servlets and CGI?

  • Servlets live in a server process, there is only one instance of a Servlet, a new thread is created for each request, and Servlet instances are generally not destroyed
  • CGI: A process is created on a request and destroyed on completion, which is less efficient than a servlet

Briefly describe the design pattern of JSP.

Briefly describe the design pattern of JSP.

In the Web development pattern, there are two main development structures, called Mode I and Mode II.

First, let’s get some ideas straight:

  • Data Access Object (DAO) : Atomic operations such as adding, modifying, and deleting Data.
  • Web layer: interface + controller, that is JSP [interface] +Servlet [controller]
  • Service business layer: Combine atomic DAO operations into a complete business logic
  • Control layer: Mainly uses servlets for control
  • Data access layer: DAO, Hibernate, JDBC technology is used to add, delete, change and check data
  • JavaBean for encapsulating data, processing part of the core logic, used in each layer!

Mode one refers to the development of the display layer, control layer, data layer operation unified to JSP or JavaBean to deal with!

Pattern ONE has two cases:

Fully use JSP for development:

  • Advantages:

    • Development speed thief fast, as long as write JSP on the line, JavaBean and Servlet are not designed!
    • Small changes to the code is convenient, directly modify the JSP page to the WEB container, unlike servlets that need to be compiled into a. Class file and then sent to the server! Of course, developing this in an IDE is not a big deal.
  • Disadvantages:

    • The readability of the program is poor, low reuse, complex code! What JSP code, HTML code to write above, this must be difficult to read, difficult to reuse!

Use JSP+JavaBean to do development:

  • Advantages:

    • The program is highly readable, most of the code is written in JavaBean, not mixed with HTML code, readable line.
    • Reusable high, the core of the code is developed by JavaBean, JavaBean design is used to reuse, encapsulation, greatly reduce the work of writing repeated code!
  • Disadvantages:

    • There is no process control, JSP pages in the program need to check whether the parameters of the request is correct, when the exception occurs processing. Display operations and business logic code work closely together! Maintenance will be difficult

Mode II in all the development of the Servlet as the main body of the expansion, by the Servlet to receive all client requests, and then according to the request to call the corresponding JavaBean, and all the display results to JSP to complete! , also known as MVC design pattern!

MVC design pattern:

  • Display layer (View) : Mainly responsible for accepting the content delivered by servlets, calling Javabeans, and displaying the content to the user
  • Control layer (Controller) : mainly responsible for all user request parameters, judge whether the request parameters are legitimate, according to the type of the request to call JavaBean, the final processing results to the display layer display!
  • Model layer (Mode) : The model layer includes the business layer and the DAO layer.

conclusion

  • (1) ModelI, JSP+JavaBean design mode.
  • (2) ModelII, MVC design pattern.

Open source project (6 K STAR) :Github.com/ZhongFuChen…

If you want to follow my updated articles and shared dry goods in real time, you can search Java3y on wechat.

The content of the PDF document is typed by hand. If you don’t understand anything, you can ask me directly (the official account has my contact information).