Javaweb II)

Source: Internet
Author: User
Tags access properties

State Management Solutions:

1. Hidden fields
1.1, the user on the site access to the session tracking.
1.2. Provide predefined input to the server-side program.
1.3. Store dynamically generated page context information.
Insufficient: Dynamic requests are valid and static pages are not supported.
2. Cookies
Disadvantage: The data is saved on the client, the user may disable the cookie causing the read cookie to fail.
3. Session (Conversation)
Keep the customer's status information on the server side.
Built-in objects.
Cons: Dependency cookie, Solution URL rewrite.
Destruction of Session:
Session set maximum active time expires, shutdown server, session.invalidate () (forced destroy);
Note: Closing the browser does not destroy Session,session is a built-in object for the Web container. belongs to the server.

4. URL Rewriting

Listener:
Three main objects: ServletContext, session, request
Same: Listen for life cycle, attribute change
Different: Session migration, object binding monitoring (no need to write configuration file)


Design pattern: is a set of repeated use, many people know, after the classification of the Code design experience Summary. Using design patterns is for reusable code, so that code is easy to maintain and extensible, and that is, the code has high aggregation, low coupling characteristics.


In a nutshell, the so-called pattern is a good example of what is being studied. Design patterns are examples of design.


The pattern is not a frame, but a process. Patterns are also not simple "problem solutions," because patterns must be solutions to typical problems. It is an example that can make learners extrapolate, have research value, have Exchange value and have their own name. (Java design mode)

mode is not applied. Don't assume that you want to use some design patterns in any one system. The design of the system is not the same as the design pattern, and the more it does not contain the more design pattern is better.

The essence of design pattern: The practical application of object-oriented method. In particular, encapsulation, inheritance, and multi-state reuse.

To really understand design patterns requires a thorough and immediate object-oriented design principle and object-oriented three features.


Some common applications for filters are:
1. Authentication filtering: The user's request for access to a unified authentication.
2. Login and Audit filtering: records and audits the user's access requests.
3. Image Conversion Filter: Image Conversion format.
4. Data compression filtering: Compress the data that occurs to the user, thereby reducing the amount of transmission.
5. Encryption filtering: Encrypt and decrypt requests and responses.
6. Token processing.
7. Resource access trigger time filtering.
8.XSLT filtering.
9.MIME-TYEP filter.

Matching principle: Exact match > directory match > Extension match
Filter Chain:
The 1.web container first matches the filter for the request URL, and then matches the filter for the servlet name.
2. Filters of the same type are always called in the order of the Web. XML configuration file.

Object scope:
1. Apply context ServletContext Object scope
For the entire web app, there is only one ServletContext object, and it can be visited everywhere in the Web App. The Servlet object is created and instantiated by the Web container, and he can read/write properties at the same time as threads that are not thread-safe. The ServletContext object is destroyed when the Web container is closed. Thread is not secure.
Recommendation: ServletContext objects with as little as possible to save data that will not be modified, the common way is to use singleton mode to process shared data.
2. HttpSession Object Scope
The HttpSession object is used to maintain session state with a customer. Setting a property in a HttpSession object cannot be accessed from anywhere in the Web App. HttpSession objects exist during a user session and can only be accessed in requests that are part of the same httpsession, so property access to HttpSession objects is theoretically safe. However, the user opens multiple browser windows that belong to a process, where access to the same HttpSession object occurs, multiple requests are made, more than one worker thread is required to process the request, and the thread may be unsafe. This is a thread-synchronous process that requires read and write properties, and is typically handled using the synchronous block synchronized. When the session is destroyed, the properties set by the HttpSession are also destroyed.
3. Request Object Scope
The HttpRequest object is used for a client-side request process. For each request, executed by a single thread, a new request object is created. Therefore, the request object can only be accessed in one thread, and it is thread-safe.


The common denominator of all three objects is that they can be used to access properties, have the same property manipulation methods, and are built-in objects.

Scope size comparison of three objects (from large to small)
ServletContext Object >httpsession Object >httpservletrequest object.

Three objects comparison of resource consumption (from many to less)
ServletContext Object >httpsession Object >httpservletrequest object.

Three thread security comparisons
ServletContext object thread is not secure
HttpSession Object Threads theoretically safe
The HttpServletRequest object thread feels secure.


The defect of the servlet

1, deployment cumbersome and error-prone.
2, not conducive to the division of the project, the front end of the sub-paragraph, require developers to master more skills.

JSP: The full name of the Java servlet pages, the Chinese Name Java Server page, is essentially a simplified servlet design.
It is a traditional Web page HTML file inserted into the Java program segment and JSP tags, thus forming a JSP file, suffix named *.jsp

JSP, JavaBean, custom tag, JSP El (JSP essence is a simplified servlet design)

The first receipt of a Web container


The life cycle of a JSP
1. The JSP engine translates the JSP page into Java code (translation)
2. Compile Java code into a class bytecode file (compile)
3. Load the class bytecode into the Web container (class mount)
4. Instantiation of objects by Web container (class instantiation)
5. Instantiation of object Initialization (initialization)
6. Request to Reach Call service method (Submit service)
7. Destroy the object instance (

Javaweb II)

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.