JSP implicit object and MVC design pattern

Source: Internet
Author: User
Tags config relative stack trace java web root directory tomcat tomcat server

Today, let's take a look at the implicit image and MVC design patterns for JSP applications.

First, let's start with the implicit object of JSP: The JSP implicit object is an instance of a set of classes loaded by the Web container. It is an object that can be used directly on a JSP page. Divided into 4 main categories: 1, Input and Output objects: Control page input and output (request, response, out).

2. Scope Communication object: Retrieves the information related to the servlet of the JSP page (session, application, PageContext).

3. Servlet object: Provides information about the page environment (page, config).

4. Error object: Process the error in the page (exception).

Let's take a look at the input and output objects: Requests object: Request Implicit object represents a client's demand, contains all request information, common method: Voidsetcontenttype (String name) : Sets the type and character encoding of the content generated as a response.

void Sendredirect (Stringname): Sends a response to the browser indicating that it should request another URL (loss of data, no longer executing the code following the Sendredirect method after the jump.

Out object: An out implicit object should be used in a JSP page to send the entity content in text form to the client.

The Out object is returned by calling the Getout method of the PageContext object, and its function and usage are very similar to those PrintWriter objects returned by the Servletresponse.getwriter method.

The type of out implicit object in a JSP page is jspwriter,jspwriter equivalent to a printwriter with caching, and the buffer property of the page instruction for the JSP page can adjust its cache size or even its cache.

An out implicit object in a JSP page is equivalent to a buffered wrapper class object that is inserted before the PrintWriter object returned by the Servletresponse.getwriter method.

Only if the content is written to an out object, and if any of the following conditions are true, Out object to invoke the Servletresponse.getwriter method, and the PrintWriter object returned by this method is actually written to the contents of the out object's buffer into the buffer provided by the servlet engine: the buffer property that sets the page directive closes the The cache feature is written to an out object that fills the buffer of the Out object. Entire JSP page End scope Communication object: The PageContext object is an instance object of the Javax.servlet.jsp.PageContext class, Javax.servlet.jsp.PageCont The Ext class is a subclass of Javax.servlet.jsp.JspContext.

The PageContext object is returned by calling the Jspfactory.getpagecontext method.

The PageContext object encapsulates the running information of the current JSP page, providing a way to return to other implicit objects of the JSP page.

In a JSP page, you can simply pass the PageContext object to an instance object of a Java class, in which the functionality of other implicit objects is accessible and invoked.

The PageContext object gives the user access to all implicit objects defined in the current page scope.

The GetException () method returns the exception implicit object GetPage () method returns the page implicit object Getrequest () method to return the request implicit object GetResponse () method returns the response implicit object Getservletconfig () method returns the config-implicit object Getservletcontext () method to return application implicit Object GetSession () method returns the session implicit object Getout () method to return an out implicit object PageContext class defines a setattribute method to store the object into a HashMap object inside the PageContext object. A getattribute method is also defined to retrieve the objects stored in the HashMap object.

void setattribute (string name, ob ject value) ob ject getattribute (string name)

(Note: The PageContext class, in addition to storing and retrieving property objects within itself, defines methods that can store and retrieve property objects in other domain scopes.) )

SetAttribute methods and GetAttribute methods can be invoked in application, session, request, and PageContext objects to set and retrieve properties within their domain.

Properties stored in the Application object can be accessed by all servlet and JSP pages in the same Web application.

Properties stored in the Session object can be accessed by all servlet and JSP pages that belong to the same conversation.

Properties stored in the Request object can be accessed by all servlet and JSP pages that belong to the same request, such as multiple servlet and JSP pages that are connected by using the Pagecontext.forward and Pagecontext.include methods.

Properties stored in the PageContext object can only be accessed by the components that are invoked during the current response of the current JSP page, such as the JSP page that is responding to the current request and the individual custom label classes it calls.

The PageContext class also provides a way to unify the various domain-wide properties to simplify access to the various domain-wide properties.

public void setattribute (java.lang.String name,java.lang.ob ject value,int scope) public Java.lang.ob ject ( String name,int Scope)

The value of the parameter SCOPE is: Pagecontext.application_scope pagecontext.session_scope pagecontext.request_scope PageContext.PAGE_ SCOPE publicvoid removeattribute (String name)

Publicvoid removeattribute (String name,int scope)

Getattributenamesinscope method Findattribute Method PageContext class defines a forward side Method and two include methods to simplify and replace the invocation of the Requestdispatcher.forward method and the Requestdispatcher.include method respectively: public void Forward ( Java.lang.String relativeurlpath) throwsjavax.servlet.servletexception,java.io.ioexception public Voidinclude ( Java.lang.String Relativeurlpath)

Throwsjavax.servlet.servletexception,java.io.ioexception public void include (Java.lang.String Relativeurlpath, Boolean flush) throws javax.servlet.servletexception,java.io.ioexception the resource path passed to these methods can only be a relative path, if the path begins with "/", Represents the root directory relative to the current Web application, otherwise, represents the access path relative to the current JSP being mapped to.

The Session object Sessions object represents the user's conversational status, which makes it easy to identify each user and save and track the user's session state. The most common method for session objects: void setattribute (String name, ob ject value)

void GetAttribute (String name)

The Application object application objects to the entire application, and all client windows can share the object, starting from the server until the server shuts down. The most common method for application objects: void setattribute (String name, ob ject value): The value of an object is stored in the application (the type of the stored value is ob ject) by name/value.

void GetAttribute (String name): Gets the value of the object stored in the application (the type of the obtained value is OB ject), based on the name.

Sets the context initial parameter. Add content to the appropriate location in the WEB.XM L file:

<context-param> <param-name>website</param-name> <param-value>www.sohu.com</ Param-value> </context-param>

In the JSP page, the Getinitparameter () method of the Applicatin object is used to obtain the parameter values corresponding to the website parameter. Gets the parameter value in the servlet using the same method as the ServletContext object.

Logs are logged using the log () method.

You can use the log () method provided by the application object to implement logging, and in the Tomcat server, the logged logs are saved in the logs directory of the Tomcat root directory. Log contents are recorded in different files.

Servlet object: The Page Object Page object provides access to all objects defined on a Web page. The Page object represents the pages themselves, which is an instance of the Java.lang.ob Ject class.

The Config object Config object stores some of the initial information for the servlet. The Config object is an instance of the Javax.servlet.ServletConfig interface, and the ServletConfig interface provides methods to retrieve the servlet initialization parameters. The Config object represents the configuration of the servlet initialization data that compiles the JSP page.

Error object exception:exception the error in the JSP page: The Printstacktrace () method is used to display the stack trace of the exception above we have introduced the implicit object of JSP, Let's talk about the design pattern of MVC: Before you develop a software, you have to design its architecture, a basic architectural idea is to divide the software into different modules, and the key to the problem is how to divide the modules.

MVC (Model-View-Controller) is a software design pattern invented in the the 1980s for SMALLTALK-80 programming language, which is a design method of separating business logic and display interface.

The model part of MVC is responsible for managing the business data of the program, and the View section is responsible for displaying the interface, while the controller (Controller) section is responsible for interacting with the user (accepting the request and selecting the response view).

JSP design pattern: JSP specification gives two scenarios for building Web applications using JSP pages The difference between-jsp mode 1 and Mode 2. Two patterns is the location of the processing.

JSP Mode 1 (JSP + JavaBean): In the architecture of Mode 1, the JSP page is responsible for processing the request and sending the response to the client.

Model1 Typical Exchange process: First, the user through the browser to invoke the JSP page in the Web application, send request requests, JSP page received the browser request, by calling the JavaBean object to read the data from the database, and then the JSP page to return the data to the browser , and finally displays the appropriate information in the browser.

Benefits: Very well suited for rapid development of small web projects and low technical requirements for Java Web developers.

Drawbacks: Java and HTML are softer together, creating a lot of difficulty in the late and maintenance phases of Web project development.

JSP Mode 2 (MVC): Mode 2 architecture integration uses servlet and JSP pages. In this mode, the JSP page is used for the presentation layer, and the servlet handles various tasks.

Model2 Interactive Process: First, the user through the browser to the Web application to send the servlet request, the servlet received the request after the instantiation JavaBean object, call the JavaBean object method, JavaBean object return data read from the database, The servlet selects the appropriate JSP and displays the data read from the database through this JSP, and the final JSP page returns the final result to the browser.

Advantages: Business logic and presentation content is well separated, this development approach is suitable for many people to cooperate in the development of large-scale projects.

Drawbacks: Web project development is becoming more difficult, and the technical requirements of the developers have been increased.

The servlet acts as a controller that handles requests and creates any beans that are required by the JSP page. The controller is also responsible for determining which JSP page to pass the request to. The JSP page retrieves the objects created by the servlet and extracts the dynamic content into a template.

Note: Because the model is on the Web server side, the final view is located in the user browser, only the browser issued a request, the server will respond, no request is not responding, so in the Web application is difficult to implement the model "State Change Notification" event, the view can not be automatically updated as the model changes. Due to the inability to fully implement the MVC model in Web applications, pattern 2 can only be considered a variant of the MVC design pattern, and someone simply calls him "Webmvc".

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.