What is model2 pattern?
Model2 pattern actually a pattern which use to Construct Web site, before MVC pattern, we used servlet + JSP + JavaBean to build Web site. Which we call this pattern
Is model2.
First of first, let start with a simple example of tutorial.
1. defined the Java Bean like below.
Package CN. javass. hello. servletimpl. VO; public class helloworldmodel {private string account; private string password; private string submitflag; Public String getaccount () {return account;} public void setaccount (string account) {This. account = Account;} Public String GetPassword () {return password;} public void setpassword (string password) {This. password = password;} Public String getsubmitflag () {return submitflag;} public void setsubmitflag (string submitflag) {This. submitflag = submitflag;} Public String tostring () {return "account =" + account + ", password =" + password + ", submitflag =" + submitflag ;} public void businessexecute () {system. out. println ("ongoing business processing ======> ");}}
2. Implement the servlet.
Package CN. javass. Hello. servletimpl. servlet; Import Java. Io. ioexception; Import Javax. servlet. servletexception; Import Javax. servlet. http. httpservlet; Import Javax. servlet. http. httpservletrequest; Import Javax. servlet. http. httpservletresponse; Import CN. javass. Hello. servletimpl. VO. helloworldmodel; Public Class Helloworldservlet Extends Httpservlet { Protected Void Doget (httpservletrequest request, httpservletresponse response) Throws Servletexception, ioexception {dopost (request, response );} Protected Void Dopost (httpservletrequest request, httpservletresponse response) Throws Servletexception, ioexception { // 1: Collect parameters. If you do not need to do so, Use Javabean to pass in. // 2: The organization parameters are no longer needed. The organization has been organized and the data is encapsulated into a JavaBean // You only need to obtain the encapsulated JavaBean. Helloworldmodel hwm = (helloworldmodel) request. getattribute ("hellomodel" ); // 3: process the Logic Functions of the Call Model Hwm. businessexecute (); // Here, the input parameters are output. System. Out. println ("user input parameter =" + Hwm ); // 4: select the next page based on the logic processing result. Select the welcome page directly. // 4.1: first, prepare the data displayed on the welcome page. Request. setattribute ("hwm" , Hwm ); // 4.2: Welcome Page Request. getrequestdispatcher ("/servletimpl/welcome. jsp" ). Forward (request, response );}}
3. Build JSP
<% @ Page Language = "Java" contenttype = "text/html; charset = gb2312" Pageencoding = "Gb2312" %> <HTML> Class = "CN. javass. hello. servletimpl. VO. helloworldmodel "Scope =" request "> </jsp: usebean> <JSP: setproperty name =" hellomodel "property =" * "/> <% If ("Login" . Equals (hellomodel. getsubmitflag ())){ %> <JSP: Forward page = "/Hello"> </jsp: Forward> <% } %> <Form action = "/helloworld/servletimpl/login. JSP "method =" Post "> <input type =" hidden "name =" submitflag "value =" login "/> Account: <Input type = "text" name = "Account"> <br> Password: <Input type = "password" name = "password"> <br> <input type = "Submit" value = "Submit"> </form> </body>
From this example. We can see servlet retrieve the JavaBean from the request.
We notice the code
Helloworldmodel hwm = (helloworldmodel) request. getattribute ("hellomodel ");
Is use to get JavaBean. We also can see the JavaBean is specified in the JSP.
<JSP: usebean id = "hellomodel"Class= "CN. javass. hello. servletimpl. VO. helloworldmodel "Scope =" request "> </jsp: usebean> <JSP: setproperty name =" hellomodel "property =" * "/>
Let's get into the detail of these code.
The JSP: setproperty is set the value to the property of JavaBean instance.
Property = "*"
Stores all of the values the user enters in the viewable JSP page (called request parameters) in matching bean properties. the names of the properties in the bean must match the names of the request parameters, which are usually the elements of an HTML form. A bean property is usually defined by a variable declaration with matching getter and setter methods (for more information, see http://java.sun.com/products/javabeans/docs ).
What is the difference of getattribute and getparameter?