Java programmers written interview Collection Chapter 5 Java Web: 5.1 Servlet and JSP (13-17 questions)

5.1.13 JSP include directive and the include action in what is the difference

The main role is to include the introduction of another file in the current file, for use in the current file, for example, are exactly the same when some part of all pages of the application (such as a title, footer, navigation, etc.) , can be considered part of the same extract write a separate file, and include by way of introduction.

There are two methods used include: include instructions and include action. Wherein the instructions include use of: <% @ include file = "test.jsp"%>, using the method of action include: <jsp: include page = "test.jsp" flush = "true"> <jsp: param name = "name" value = "value" /> </ jsp: include>.

The difference between the fundamental and include instructions include both the operation time that is called. include instruction is an instruction compile phase, i.e., at compile time, the compiler will copy the contents of the instruction pointed by the position command to the target file is located, alternative instruction into a final file, there is only one file at run time. In other words, the contents include instructions contained in the file is inserted into a JSP file at compile time, when the file content changes would need to be rewritten to compile therefore suitable for situations contain static pages, for example, can contain a Servlet.

The action is the syntax include run-time, when the main page is requested, it will be used pages include it involves two files, similar to the method call, and therefore more suitable for situations including dynamic pages. In addition, there are two differences in the following three points:

1) When the operation include, variable declaration in a page is available for another file, unless the variable is placed in the request, session, application scope; in use include instruction, the current page and the pages can be shared comprising variable.

2) When using the include directive, the new generation of JSP page JSP syntax to meet the requirements, should avoid conflicts of variable names; and in the use include action, variable name conflict does not exist.

3) include instructions will be included to modify the file, but does not take effect immediately, unless the main page modify or delete the main page of the class; and include actions to modify the file to be included, it will take effect immediately.

Considering the advantages include action on maintenance when both methods are applicable, include the use of priority action. When the main page is defined only in the include file to use a method or a field or set contains a file header page response surface, it should be used include instructions, for example, copyright information and the navigation portion are many sites partially the same, each will appear in the file, it is possible to consider the content in a separate file, then <% @ in-clude%> to reference instructions.


5.1.14 What conversation tracking technology

In the development of Web applications, often you need to be able to do data sharing or parameters can be passed between different pages, and data in a session may be used in different places, so you need to have a dedicated mechanism to transfer and save the data.

The so-called session, refers to the connection opening and from the server and the client makes a request to the server response to client requests the entire process.

Since HTTP is a communication between the client and by the server, the HTTP is a stateless protocol itself, it can not save the customer information, i.e., once the connection is disconnected after the completion of the response, the next request, it is necessary to re-establish the connection , until the establishment of the connections also need to determine whether the same user, therefore, in order to monitor the course of the session, the best way is to use session tracking technology.

Specifically, session tracking technologies include the following four:

1) page. On behalf of a page of related objects and attributes. A page of a compiled Java Servlet class (it can include with any instructions, but did not include action) representation. This includes both Servlet and is compiled into Servlet JSP page.

2) request. A related objects and attributes requests on behalf of Web clients send. A request may span multiple pages, involves a number of Web components.

3) session. On behalf of a user and for a Web client experience related objects and attributes, a Web session can and often span multiple client requests.

4) application. Representatives of the objects and attributes associated with the entire Web application, which is essentially across multiple Web applications, including multiple pages, requests, and a global session scope.

Common pen questions:

If you only want to share data across multiple pages, you can use () scope.

A.request,session B.application session

C.request,application D.pageContext request

The answer: A. See the above explanation.


5.1.15 Web developers how to specify the encoding string

ISO-8859-1 Latin for encoding, which consists of a single byte (0 to 255). GB 2312, GBK coding for Simplified Chinese, single-byte and double-byte consists of mixing, the highest bit is 1 byte and the next byte constituting a character, the highest bit is a 0 byte ASCII code. UTF-8 / UTF-16 / UTF-32 is the international standard UNI-CODE encoding, UTF is called the Unicode Translation Format, i.e. in a sense transferred to the Unicode format. Wherein the encoding scheme is the most used UTF-8, because the space-saving manner at the time of encoding the Latin, which is characterized by different length encoding ranges for different character.

String or byte array into the sequence required to select the correct deserialization encoding, encoding if used incorrectly, will get distortion. Therefore, Web application development, often encounter situations need to specify the encoding format string, in order to prevent garbled, the best way is to specify the encoding format.

The following examples string output in ISO-8859-1 encoding format:

Note that it is also possible to specify the encoding format JSP pages displayed through response.setContentType () method in the Web development.


5.1.16 What is Ajax

Ajax (Asynchronous JavaScript and XML, Asynchronous JavaScript and XML) is a combination of Java technology, XML, and JavaScript programming technology, its main purpose is without refreshing the page to increase page by exchanging small amount of data interaction with the server sex , reduce response time, thereby improving the user experience. After the use of Ajax technology, the page each time the user does not need to commit changes reloaded.

In applications that use traditional software architecture development, when a client needs to interact frequently with the server side, the user only wait for the entire page to reload to see get from the server to the resource information, the page will be reloaded many times. Most of the current HTML code two pages are the same, this approach can be very wasteful of network bandwidth, after all, get a lot of resources are repeated useless information.

In this case, if the use of Ajax technology will bring many benefits: First, since Ajax technology can send and retrieve only the necessary data to the content server, so that significantly reduce the amount of data exchange, thereby reducing network load server; secondly Since it by using the SOAP (protocol simple Object access protocol, simple Object access protocol, for exchanging data specification), or some other XML Web Service interface, the client uses JavaScript response from the server process, but also reduces the Web server processing time; finally, there is no need to reload the entire page, so the system has a shorter response time, which will help improve the stability and availability of the system, thereby enhancing customer satisfaction.

Note that, Ajax technology is a client technology, the core JavaScript object XmlHttpRequest, the object is an asynchronous request technical support, which enables developers to use JavaScript to make a request to the server and handle the response, without blocking the user.


5.1.17 cookie and session What is the difference

A cookie is in the HTTP, a server or workstation on a script can be a way to maintain customer information. It is maintained by the Web server on the user's browser a small file that can contain information (such as identification numbers, passwords and other information) about the user. means for holding the session solutions and storage structural state between the client and the server.

Although both capable of information storage, but there are also differences. Specifically, the two differ in the following aspects:

1) cookie mechanism used to hold the client program, i.e., the data stored on the client's browser; ses-sion and the mechanism used to hold programs on the server side, i.e., the data on the server.

2) cookie security is not enough. Since the cookie information stored in the client, other people can easily get stored in the local cookie, cookie and deceit; and session information is stored on the server side, and therefore safer.

3) the performance of some higher cookie. Since the session will be saved in a certain time on the server, so when the access amount increases, it will reduce server performance.

4) single cookie stored data can not exceed 4KB, many browsers are limited to a maximum of 20 sites saved cookie; and session this problem does not exist.

In view of the above points difference, under normal circumstances, the user login information and other important information is stored into the session, while other information needs to be retained can be placed in a cookie.

=======================================

end

Published 101 original articles · won praise 20 · views 60000 +

Guess you like

Origin blog.csdn.net/qq_40993412/article/details/104069140