Java EE third Week

Source: Internet
Author: User
Tags php script sessions server memory

1.p132 Analysis:

Long before = System.currenttimemillis ();

Returns the current computer time, in which the expression format is the number of milliseconds that the current computer time and GMT time (GMT) January 1, 1970 0:0 0 seconds. You can also use this value to construct a Date object.

HttpServletRequest hrequest = (httpservletrequest) request;

The HttpServletRequest interface is a servletrequest subinterface, and the HttpServletRequest interface follows the HTTP protocol. Converts a servletrequest request to a httpservletrequest request.

SYSTEM.OUT.PRINTLN ("Filter has intercepted the user's requested address:" +hrequest.getservletpath ());

Output a hint, get the requested path through the Getservletpath () method, and record the requested URL in the log. (incomplete URL form output)

long after = System.currenttimemillis ();

Returns the current computer time, the mark end time, and the previous before composition range, and the time required to calculate the filter.

2.session Introduction:

Session: In a computer, especially in a network application, it is called "conversation control." The Session object stores the properties and configuration information required for a specific user session. This way, when a user jumps between the application's Web pages, the variables stored in the session object are not lost, but persist throughout the user's session. When a user requests a Web page from an application, if the user does not yet have a session, the Web server automatically creates a Session object. When the session expires or is discarded, the server terminates the session. One of the most common uses of Session objects is to store the user's preferences. For example, if a user indicates that they do not like to view a graphic, they can store the information in the Session object. For more information about using session objects, see "Managing Sessions" in the ASP Application section. Note Session state is reserved only in browsers that support cookies.

How the session works(1) When a session is first enabled, a unique identifier is stored in a local cookie. (2) First, using the Session_Start () function, PHP loads the stored session variables from the session repository. (3) When executing a PHP script, register the session variable by using the Session_register () function. (4) When the PHP script executes, the non-destroyed session variable is automatically saved in the session library under the local path, which can be specified by the Session.save_path in the php.ini file and can be loaded the next time the page is browsed.  How to useEditThe Session is a Web server-based approach to maintaining state. Session allows you to persist any object during the entire user session by storing the object in the memory of the Web server. Session is typically used to perform the following actionsstores information that needs to maintain its state throughout a user's session, such as logon information or other information that a user needs to browse the Web application. stores objects that only need to maintain their state between page reloads or a set of pages grouped by functionality. the role of the Session is that it keeps the user's state information on the Web server for access at any time from a page on any device. Because the browser does not need to store any such information, it can use any browser, even if it is a browser device such as a Pad or a mobile phone. limitations of persistence methodsas more and more users log in, the amount of server memory required for the Session is increasing. Each user who accesses the Web application generates a separate Session object. The duration of each Session object is the time of the user's visit plus the time of inactivity. If you keep many objects in each session, and many users use the Web application at the same time (creating many sessions), the amount of server memory that is used for session persistence can be large, affecting scalability. 3.filterin Javaweb Development, request and response are two essential objects that are generated by the Web server after each client request is received. The filter can be processed separately before the request arrives at the servlet and response leaves the servlet. Filter is often used for web site filtering of sensitive words, setting character sets, logging, and more "public" event handling. in our normal programming, each line is called from top to bottom, and filter is "cut a knife" across every complete call. By configuration, it can filter arbitrary code paths.

Filter Development Two-step walk

Write the Java class to implement the filter interface and implement its Dofilter method.
Registers the Write filter class in the Web. xml file and sets the resources it can intercept.

The Web. XML Configuration node describes:

<filter>Specifies a filter. <filter-name>Used to specify a name for the filter, and the content of the element cannot be empty. <filter-Class>element is used to specify the full qualified class name of the filter. The <init-param> element is used to specify the initialization parameters for the filter, its child elements <param-name> the name of the specified parameter,<param-value>Specifies the value of the parameter. In a filter, you can use the Filterconfig interface object to access the initialization parameters. <filter-mapping>element is used to set a resource that the Filter is responsible for intercepting. A filter interception resource can be specified in two ways: the request path to the Servlet name and the resource access <filter-name> child elements used to set the registration name of the filter. The value must be the name of the filter declared in the <filter> element <url-pattern> Set the request path blocked by filter (the URL style associated with the filter) <servlet-name> Specifies the name of the servlet that the filter intercepts. <dispatcher> specifies how the resource that the filter intercepts is called by the Servlet container, which can be one of Request,include,forward and error, the default request. Users can set multiple <dispatcher> child elements to specify the Filter to intercept multiple calls to the resource. <dispatcher> child elements can set values and their meanings request: When a user accesses a page directly, the Web container invokes the filter. If the target resource is accessed through the include () or forward () method of RequestDispatcher, then the filter is not called. INCLUDE: The filter will be called if the target resource is accessed through the RequestDispatcher include () method. In addition, the filter is not called. FORWARD: If the target resource is accessed through the RequestDispatcher FORWARD () method, then the filter will be called and the filter will not be called. Error: If the target resource is called through a declarative exception handling mechanism, then the filter will be called. In addition, the filter is not called. 

4.async Introduction

In Java applications, the majority of cases are synchronized in the way of interactive processing, but when dealing with third-party systems to interact with the situation is prone to slow response, most of the previous use of multithreading to accomplish such tasks, in fact, after the spring 3.x, Has built-in @async to solve this problem perfectly, this article will complete the introduction of @async usage.

1. What is an asynchronous call?

Before interpreting the asynchronous invocation, let's take a look at the definition of the synchronous invocation, which is the entire processing sequence, when each process is executed and returns the result. An asynchronous invocation is simply a command that sends a call, and the caller does not have to wait for the called method to complete, but to proceed with the following process.

For example, in a call, you need to call the sequence A, B, C three procedure methods, such as they are synchronous calls, they need to be executed in sequence after the execution of the process, such as B is an asynchronous call method, after the completion of a, call B, do not wait for B to complete, but to execute the start call C, After C has been executed, it means that the process is complete.

2. General asynchronous invocation processing mode

In Java, when dealing with similar scenarios, it is based on the creation of independent threads to complete the corresponding asynchronous invocation logic, through the main thread and the execution flow between different threads, so that after starting a separate thread, the main thread continues execution without creating a stall wait.

3. @Async Introduction

In spring, the method based on the @async notation, called the Async method, will be executed in a separate thread, and the caller can continue other operations without waiting for it to complete.

How to enable @async in spring

Based on how Java configuration is enabled:

1 2 3 @Configuration@EnableAsyncpublicclassSpringAsyncConfig { ... }

Based on how the XML configuration file is enabled, configure the following:

1 2 <task:executor id="myexecutor" pool-size="5" /> <task:annotation-driven executor="myexecutor"/>

These are the two defining ways.

Java EE third Week

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.