Request Forwarding: MVC design Patterns, details, programming instances of request domain properties, request redirection, and request forwarding differences

Source: Internet
Author: User

 

Request Forwarding: MVC design Patterns, details, programming instances of request domain properties, request redirection, and request forwarding differences

The MVC design pattern divides the response process of a request into three functional modules (commonly referred to as layers) to work together, and these three modules are models (model layer), view (views layer), Controller (control layer).

Model is a business object that can be used as a javabean; The view is the JSP page responsible for creating the display interface; A controller is usually a servlet program that receives a user request, creates a corresponding model object based on the request, and invokes a business method of the Model object. Finally, select a view to create the Web document content and send it back to the client.

The controller calls the Requestdispatcher.forward method to forward the request to the JSP page as the view, passing the model object as a request domain property, and then retrieving the model object from the request domain as a JSP page for the view.

Application scenario for request forwarding: MVC design pattern

The Request object provides a Getrequestdispatcher method that returns a RequestDispatcher object that calls the forward method of the object to enable request forwarding.

The request object is also a domain object, and the developer uses the request object to bring the data through the request object to other Web resources as it is being forwarded.

SetAttribute method

GetAttribute method

RemoveAttribute method

Getattributenames method

Programming instances of request domain properties

Implement a simple example program using the MVC design pattern:

Userregister.html is an HTML page containing a form form that allows the user to fill in the registration information;

User.java is a common Java class that represents user registration information;

Actionservlet.java is a servlet program for processing form form information that creates an instance object of the user class based on the information submitted by the form, stores the user instance object in the request domain, and forwards the request to another Servlet program that displays the user's registration information;

Jspresultservlet.java is a servlet program that displays the user registration information, jspresultservlet out the user instance object from the request domain, and displays the information for this instance object.

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

<title>UserRegister.html</title>

<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >

<meta http-equiv= "description" content= "This is my page" >

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">

<!--<link rel= "stylesheet" type= "Text/css" href= "./styles.css" >-->

<body>

<form action= "Actionservlet" >

User name: <input type= "text" name= "username"/><br/>

Password: <input type= "password" name= "password"/><br/>

<input type= "Submit" value= "Submit"/>

</form>

</body>

public class User {

private String name;

Private String Pass;

Public String GetName () {

return name;

}

public void SetName (String name) {

THIS.name = name;

}

Public String Getpass () {

return pass;

}

public void SetPass (String pass) {

This.pass = pass;

}

}

Import java.io.IOException;

Import javax.servlet.ServletException;

Import Javax.servlet.http.HttpServlet;

Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse;

public class Actionservlet extends HttpServlet {

/**

*

*/

Private static final long serialversionuid = 1L;

public void doget (HttpServletRequest request, httpservletresponse response)

Throws Servletexception, IOException {

String name = Request.getparameter ("username");

String pass = request.getparameter ("password");

String name1 = new String (Name.getbytes ("iso-8859-1"), "UTf-8");

String pass1 = new String (Pass.getbytes ("iso-8859-1"), "UTf-8");

User user = new user ();

User.setname (NAME1);

User.setpass (PASS1);

This.getservletcontext (). SetAttribute ("user", user);

Request.getrequestdispatcher ("Jspresultservlet"). Forward (request, response);

}

public void DoPost (HttpServletRequest request, httpservletresponse response)

Throws Servletexception, IOException {

Doget (request, response);

}

}

Import java.io.IOException;

Import Java.io.OutputStream;

Import javax.servlet.ServletException;

Import Javax.servlet.http.HttpServlet;

Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse;

public class Jspresultservlet extends HttpServlet {

Private static final long serialversionuid = 1L;

public void doget (HttpServletRequest request, httpservletresponse response)

Throws Servletexception, IOException {

Response.setcontenttype ("text/html;charset=gb2312");

User user = (user) This.getservletcontext (). getattribute ("user");

OutputStream out = Response.getoutputstream ();

String name = User.getname ();

String pass = User.getpass ();

if (user! = null) {

Out.write (name+ "..." +pass). GetBytes ());

}

}

public void DoPost (HttpServletRequest request, httpservletresponse response)

Throws Servletexception, IOException {

}

}

Details of Request forwarding

The forward method is used to forward requests to resources encapsulated by the RequestDispatcher object.

If some of the content written in the servlet program has been actually delivered to the client before the forward method is called, the forward method throws a IllegalStateException exception.

If the content is written to the servlet engine's buffer (response) before calling the forward method, as long as the content written to the buffer is not actually output to the client, the forward method can be executed normally, and the contents of the original write to the output buffer will be emptied. However, the Response header field information that has been written to the HttpServletResponse object remains valid.

Package com.hbsi.request;

Import java.io.IOException;

Import Java.io.OutputStream;

Import javax.servlet.ServletException;

Import Javax.servlet.http.HttpServlet;

Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse;

public class Request7 extends HttpServlet {

public void doget (HttpServletRequest request, httpservletresponse response)

Throws Servletexception, IOException {

Response.getwriter (). Write ("AAA");

Out.close ();

Request.getrequestdispatcher ("/index.jsp"). Forward (request, response);

}

public void DoPost (HttpServletRequest request, httpservletresponse response)

Throws Servletexception, IOException {

Doget (request, response);

}

}

Differences in Request redirection and request forwarding

Once a Web resource receives a client request, it notifies the server to call another Web resource for processing, called request forwarding.

Once a Web resource receives a client request, it notifies the browser to access another Web resource, called a request redirection.

The Requestdispatcher.forward method can only forward requests to components in the same web app, while the Httpservletresponse.sendredirect method also redirects to resources in other applications at the same site. Even resources that are redirected to other sites using an absolute URL.

If the relative URL passed to the Httpservletresponse.sendredirect method begins with "/", it is relative to the root of the entire Web site, and if the relative URL specified when the RequestDispatcher object is created starts with "/". It is relative to the root directory of the current Web application.

After the access process that calls the Httpservletresponse.sendredirect method redirects, the URL displayed in the browser's address bar changes, and the initial URL address becomes the target URL of the redirect; Call Requestdispatcher.forward The browser address bar keeps the initial URL address intact after the request-forwarding process for the method is completed.

The Httpservletresponse.sendredirect method responds directly to the request of the browser, and the result of the response is to tell the browser to re-issue the access request to the other URL, and the Requestdispatcher.forward method forwards the request to another server-side A resource, the browser only knows that a request has been made and has received a response, and does not know that a forwarding behavior has occurred inside the server program.

The caller of the Requestdispatcher.forward method shares the same request object and the response object as the callee, which belong to the same access request and response process, while the Httpservletresponse.sendredirect method caller and the The caller uses the respective request object and the response object, which belong to two separate access request and response procedures.

Request Forwarding: MVC design Patterns, details, programming instances of request domain properties, request redirection, and request forwarding differences

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.