I am a junior and start to work for next year. Here I will collect some interview and written examination questions. Below are some basic knowledge about JSP.
JSP (Java Server Pages) inserts JavaProgramSegment and JSP tag to form a JSP file, similar to ASP. Web applications developed using JSP have the same benefits as Java and are cross-platform. The characteristic of JSP is that all operations are performed on the server, and the client only gets the result, which can greatly reduce the requirements for the customer's browser, even if the customer's browser does not support Java, you can also access the JSP webpage.
After introducing JSP, we will discuss some common JSP interview and written test questions.
1. What are the two jump methods of JSP: Include, JSP: forward? What is the difference?
First, you must know that JSP: Include and JSP: forward are JSP actions, that is, a series of XML tags that can call functions built in the network server.
<JSP: Include>: Java Servlet temporarily takes over the request and response of the specified JSP page. After processing the JSP page, it immediately returns the control to the current JSP page. The advantage is that JSPCodeIt can be shared among multiple JSP pages without copying. This is why we advocate JS files in JavaScript.
<JSP: Forward>: used to process requests and responses to another JSP or servlet. The control is never returned to the current JSP page, so the subsequent part will never be executed.
There are two common ways to jump to a page:
(1) Forward jump:
Like this:
<JSP: Forward page = "Jump page"/>
In this way, you only need to jump to the server without changing the address bar. After the jump statement is executed, the system immediately jumps unconditionally, and the subsequent code is no longer executed. Therefore, we must remember to release all resources before the jump. The property set for the request can still be used on the page after the jump. The pass parameter is passed through <JSP: Param name = "parameter name" value = "parameter value"/>.
(2) response jump:
Like this:
Response. sendredirect ("Jump page address ");
In this way, the client jumps and the address bar changes. After all the code is executed, it is redirected. After the jump, the page cannot use the request attribute of the previous page, and the parameters are overwritten by the address. Like this:
Response. sendredirect ("url? Parameter Name = parameter value ")
2. Briefly describe the built-in objects and functions of JSP
JSP has nine built-in objects, which are member variables that can be used in JSP page scripts (Java program slices and Java expressions) without declaration. Built-in objects have the following features:
(1) provided by JSP specifications, without the need to be instantiated by writers;
(2) Implementation and management through web containers;
(3) All JSP pages can be used;
(4) It can only be used in expressions or code segments of script elements, such as: <% = Built-in object %> or <% built-in object %>
Next we will briefly introduce each built-in object:
(1) Input/Output objects
A. Request object (javax. servlet. http. httpservletrequest)
Contains information about browser requests. This object can be used to obtain header information, cookies, and request parameters in the request, that is, the object that encapsulates the client request information.
B. Response object (javax. servlet. http. httpservletresponse)
The response returned to the user as the JSP page processing result is stored in this object, and provides methods for setting the response content, response headers, and redirection, such as cookies and header information.
C. Out object (javax. servlet. jsp. jspwriter)
It is used to write content to the output stream of the JSP page instance and provides a method for sending the output result to the browser.
(2) Communication Control Object
A. pagecontext object (javax. servlet. jsp. pagecontext)
Describes the running environment of the current JSP page, which can return access to other implicit objects and their attributes on the JSP page, and also implement the method of transmitting control from the current page to other pages. With this object, we can access all the objects and namespaces on the JSP page, such as all sessions on this page, or a property value of the application on this page.
B. Session Object (javax. servlet. http. httpsession)
A session object refers to a session between the client and the server and stores information about the session. attributes can be assigned to another session. Each attribute has a name and a value. Session objects are mainly used to store and retrieve attribute values.
C. Application Object (javax. servlet. servletcontext)
It stores the servlet running the JSP page and the context information of any Web component in the same application. It enables data sharing between users and stores global variables. It exists between the startup and shutdown of the server. The advantage is that the same attribute of the object can be operated on in the user's frontend and backend connections or connections between different users. However, as with global variables, operations on this object attribute anywhere will affect access by other users.
(3) servlet object
A. Page Object (Java. Lang. Object)
The servlet instance of the current JSP page, which is equivalent to the this pointer in the class.
B. config object (javax. servlet. servletconfig)
This object is used to access the initialization parameters of the servlet instance (consisting of attribute names and attribute values) and server information (by passing a servletcontext object ).
(4) error handling object --- exception object (Java. Lang. throwable)
When an exception is thrown on a page, it is forwarded to the JSP error page. This object is provided to handle errors in JSP and can only be used on the error page, such:
<% @ Page iserrorpage ="True"%>
This is required; otherwise, compilation fails.
3. Basic servlet Architecture
Let's briefly introduce servlet. Servlet is a Java application on the server. It originated from Java applet. It is independent of platform and protocol and can generate dynamic web pages. It serves as the intermediate layer between customer requests (Web browsers or other HTTP client programs) and server responses (databases or applications on the HTTP server.
The basic architecture of servlet can be summarized as follows:
Import Java. Io .* ; Import Javax. servlet .* ; Import Javax. servlet. http .*; Public Class Someservlet Extends Httpservlet { Public Void Doget (httpservletrequest request, httpservletresponse response) Throws Servletexception, ioexception { // Use "request" To Read Request-related information (such as cookies) // And form data // Use "response" to specify the HTTP response status code and Response Header // (For example, specify the content type and set the cookie) Printwriter out = Response. getwriter (); // Use "out" to send the response content to the browser }}
If we want to make a class servlet, we must make it inherit from httpservlet, and choose to overwrite doget, one or all of dopost methods based on whether the data is sent through get or post. In addition to the methods used to specify the HTTP response status and Response Header, httpservletresponse provides a printwriter for sending data to the client. For a simple servlet, most of its work is to generate a page sent to the client through the println statement.
The general process of using Servlet is as follows:
Server creates a servlet instance --- server calls servlet Init () method --- a client request arrives at server --- server creates a request object --- server creates a response object --- server activates Servlet's Service () method, pass the request and response object as the parameter --- Service () method to obtain information about the request object, process the request, access other resources, and obtain the required information.
The Service () method uses the response object method to send the response back to the server and finally reach the client. The Service () method may activate other methods to process requests, such as doget () or dopost () or a new method developed by the programmer.
4. getservletcontext () and getservletconfig ()
(1) getservletconfig ()
During servlet initialization, the container passes in a servletconfig object and stores it in the servlet instance. This object allows access to two items: the initialization parameter and the servletcontext object, the former is usually specified by the container in the file. It allows the servlet to transmit scheduling information to the servlet at runtime. More importantly, the servlet can obtain the object and configuration information at any time, the parameter configured in <servlet> <init-param> is obtained.
(2) getservletcontext ()
A servlet can use the getservletcontext () method to obtain the web application's servletcontext. This method can also obtain the relevant configuration information, but the configuration information is a parameter configured by <context-param>.
Getservletcontext () applies to the entire web app, while getservletconfig () applies only to the current servlet. However, the servletconfig object has reference to servletcontext, so you can get the web app through getservletconfig.
5. What is the role of filter? What is the main implementation method? What is the full name of request () and response?
Filter allows you to change a request and modify a response. Its essence is "servlet chainning" (servlet chain ).
The functions of a filter include:
(1) intercepted before the servlet is called;
(2) Check the Servlet request before the servlet is called;
(3) modify the request header and request data as needed;
(4) modify the response header and response data as needed;
(5) Intercepted after the servlet is called.
There are three main implementation methods: Init (filterconfig config), dofilter (servletrequest request, servletresponse response, filterchain chain), and destroy ().
The full name of request is httpservletrequest, and the full name of response is httpservletresponse.