<J2EE Learning notes > Continuation of the last servlet partial boost content and JSP content outline

Source: Internet
Author: User

All of the following courseware are from Tongji University Liu Yan teacher's <EnterpriseJavaProgramming> because of the teaching language problem, if the translation is not correct in the place to welcome correct

Section 1. Part of the content enhancement for Servlets

I. How to perform server-side redirection

There are two main ways to redirect servers:

The first is implemented through the RequestDispatcher (Request dispatch) class, Within the servlet, the Webcontext class is obtained by invoking the request class parameter of the Doget or Dopost function to use the Getrequestdispatcher class to redirect the URL request. One thing to note here is that the RequestDispatcher class has two ways to get it:

1. Call the Getrequestdispatcher method directly through the request "unknown object" class, which is a relative path, that is, if the page where the request was sent is Implict Directory and the page to be redirected in the same virtual directory, you need to do a shorthand.

2. Indirect methods (the teacher's slides are all anonymous classes used to simplify the code) call the Getservletcontext method by invoking the request object to get the anonymous class again Getrequestdispatcher class to achieve the RequestDispatcher class, and finally unify the use of a forward (request,response) to complete the redirect.

The second is to use the Sendredirect method of the response class, and I think this method is the simplest and most brutal.

Two. How to transfer data/classes (persisting application data across requests) via the request class

The above table first gives the source of the class to be used, the transmission of the data/class referred to in this chapter only refers to the transmission through the Setattri7bute and GetAttribute methods, there are three kinds of set/ The getattribute methods are derived from the request class, session class, and ServletContext class, respectively. Since these three species have attribute getter&setter we might as well take a look at the difference between them.

First, take a look at the server-the difference between the Request,session,context cookie in the client structure

In this image on the server side of the red ABC is the session, the client's blue ABC indicates that COOKIE,CONTEXT is D, and the request graph does not indicate that the life cycle of the request is described in the following link

Http://blog.csdn.net/seablue_xj/archive/2009/11/26/4879847.aspx

1. When using the request attribute getter&setter the life cycle of the data we transmit is the same as the life cycle of the request class, which is the maximum limit for transmitting data using request. When the request object is created, the data is still present if it is processed by the Requestdispatcher.forward object mentioned above, meaning that the longest route of the information that is stored in the request is "jsp-〉servlet-〉 RequestDispatcher redirect url ", of course, if it is redirected with Sendredirect, then the data stored in the end of the request life cycle will naturally be destroyed.

2. If you are using the session attribute Getter&setter, actually the data to be transferred is present in the server-side session class, Client requests can access the session multiple times through their own sessionid, which means that the data can be stored over and over again, so that the life cycle and redirection of the stored data is not related

3. The most "big" approach is to use the ServletContext attribute Getter&setter This method can be described as the data exists in the entire server side (regardless of the parallel host Shared service side) in the background, the resources stored directly on the server side.

Section 2. About the main content of JSP

The conceptual introduction to JSP is simple: 1. Dynamic JSP code + static HTML combination 2.JSP and Servlets are generally used as controllers in the MVC pattern

These classes are implicitly declared classes that can be used directly in the JSP's embedded dynamic code, and will produce a "nullpointerexception" if you use other Explict objects without declaring them.

The example here and the previous example is that the Servlet-class tag in the Web. xml file is replaced with the jsp-file tag, which is used as a JSP file instead of the Javaservlet class, using a JSP file can be better and HTML5 code fusion to write beautiful and user-friendly page, do not need You want the response.getwriter in the servlet to line println.

A conceptual description of JavaBeans

1.javaBean is a "perfect" encapsulated class that requires internal data to be accessed strictly by the OOP specification through Getter&setter, and is private qualified

2.javaBean must use interface serializable to allow the object to be parallelized to the disk file, the following sentence

<jsp:usebean id= "id" class= "classname" scope= "Page|request|session|application"/>

<jsp:setproperty name= "id" property= "name1" value= "xxx1" or param= "xxx1"/>

<jsp:getproperty name= "id" property= "name1"/>

There are a lot of things to know about this picture, because my html is very shallow, such as the single quote is what the story ... But in the jsp:setproperty tag, we can either pass a value statically or use the URL directory + "? Xxx1=param1&, ... " Query tokens to set their own values

The use of Jsp:include is very simple, but here is not much to repeat the:-)

<J2EE Learning notes > Continuation of the last servlet partial boost content and JSP content outline

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.