Example: JSP Model2 architecture (below)

Source: Internet
Author: User

Each time a user adds an item to the Eshop. jsp page, the page sends a request to the control servlet. The servlet determines the appropriate actions in sequence, and then processes the Request Parameters of the items to be added. It then shows a new CD Bean (see code list 4) indicating the selected item and updating the shopping cart object in the session.

Code list 4: CD. java

Package shopping;

Public class CD {

String album;

String artist;

String country;

Float price;

Int quantity;

Public CD (){

Album = "";

Artist = "";

Country = "";

Price = 0;

Quantity = 0;

}

Public void setAlbum (String title ){

Album = title;

}

Public String getAlbum (){

Return album;

}

Public void setArtist (String group ){

Artist = group;

}

Public String getArtist (){

Return artist;

}

Public void setCountry (String cty ){

Country = cty;

}

Public String getCountry (){

Return country;

}

Public void setPrice (float p ){

Price = p;

}

Public float getPrice (){

Return price;

}

Public void setQuantity (int q ){

Quantity = q;

}

Public int getQuantity (){

Return quantity;

}

}

Note: We include additional intelligence in the servlet so that it can understand that if a previously added CD is selected again, it only needs to increase the count for the CD Bean in the shopping cart. This control servlet can also handle the actions triggered in Cart. jsp, such as deleting an item from the shopping Cart or checking out the account. Note that the control servlet has always been fully aware of resource allocation and determines which resources are called in the response to specific actions. For example, if the shopping cart status changes, such as adding or deleting, the control servlet will send the processed requests to the Eshop. jsp page. This prompted the page to re-display the main view, and the data displayed in the shopping cart has been updated. If the user decides to settle the bill, the request will be sent to the Checkout. jsp page after processing (see Code List 5) and implemented through the following scheduling program:

String url = "/jsp/shopping/Checkout. jsp ";

ServletContext SC = getServletContext ();

RequestDispatcher rd = SC. getRequestDispatcher (url );

Rd. forward (req, res );

Code List 5: Checkout. jsp

<% @ Page session = "true" import = "java. util. *, shopping. CD" %>

<Html>

<Head>

<Title> Music Without Borders Checkout </title>

</Head>

<Body bgcolor = "#33 CCFF">

<Font face = "Times New Roman, Times" size = + 3>

Music Without Borders Checkout

</Font>

<Hr> <p>

<Center>

<Table border = "0" cellpadding = "0" width = "100%" bgcolor = "# FFFFFF">

<Tr>

<Td> <B> ALBUM </B> </td>

<Td> <B> ARTIST </B> </td>

<Td> <B> COUNTRY </B> </td>

<Td> <B> PRICE </B> </td>

<Td> <B> QUANTITY </B> </td>

<Td> </td>

</Tr>

<%

Vector buylist = (Vector) session. getValue ("shopping. shoppingcart ");

String amount = (String) request. getAttribute ("amount ");

For (int I = 0; I <buylist. size (); I ++ ){

CD anOrder = (CD) buylist. elementAt (I );

%>

<Tr>

<Td> <B> <% = anOrder. getAlbum () %> </B> </td>

<Td> <B> <% = anOrder. getArtist () %> </B> </td>

<Td> <B> <% = anOrder. getCountry () %> </B> </td>

<Td> <B> <% = anOrder. getPrice () %> </B> </td>

<Td> <B> <% = anOrder. getQuantity () %> </B> </td>

</Tr>

<%

}

Session. invalidate ();

%>

<Tr>

<Td> </td>

<Td> </td>

<Td> <B> TOTAL </B> </td>

<Td> <B >$ <% = amount %> </B> </td>

<Td> </td>

</Tr>

</Table>

<P>

<A href = "/examples/jsp/shopping/EShop. jsp"> Shop some more! </A>

</Center>

</Body>

</Html>

Checkout. jsp only retrieves the total number of shopping cart and all requests from the session, and then displays the selected item and total price. Figure 5 shows the client view at checkout. Once the user settles the bill, it is important to remove the session object in time. Taking care of this, a session. invalidate () call is required at the end of the page. This process is necessary. There are two original items: first, if the session is not terminated, the user's shopping cart will not be reinitialized. When he tries to start a new round of shopping without checking out the account, his shopping cart will still keep the items he has purchased. Second, if the user leaves without checkout, the session object will not be voided and will still occupy valuable system resources until it expires. Because the default session is valid for 30 minutes, in high-load systems, this situation will quickly exhaust system resources. Of course, we know what an Application means to exhaust system resources!

Figure 5: music without borders, checkout View

The text in the figure is the same as that in Figure 4.

Note that all resource allocation in this example is based on sessions, because this model is stored in sessions. Therefore, you must make sure that the control SERVLET is not accessed by users, even unexpected access is not allowed. To solve this problem, you can automatically redirect the error page when the control servlet detects an illegal access. (See code listing 6)

Code List 6: error.html

<Html>

<Body>

<H1>

Sorry, there was an unrecoverable error! <Br>

Please try <a href = "/examples/jsp/shopping/EShop. jsp"> again </a>.

</H1>

</Body>

</Html>

Configure "music without borders"
I suppose you are using the latest version of JavaServer Web Development Kit (Java Server Web Development Kit-JSWDK) of Sun for example. Assuming that this server is installed under the jswdk-1.0.1 directory-this is the default installation path for it in Windows, the file for the music without Borders application should be configured as follows:

● Create shopping directory under jswdk-1.0.1examplesjsp directory

Copy Eshop. jsp to jswdk-1.0.1examplesjspshopping

● Copy Cart. jsp to jswdk-1.0.1examplesjspshopping

● Copy Check. jsp to jswdk-1.0.1examplesjspshopping

● Type javac *. java to compile the. java file.

● Copy ShoppingServlet. class to jswdk-1.0.1webpagesWeb-Infservlets

● Create shopping directory under jswdk-1.0.1examplesWeb-Infjspbeans directory

● Copy CD. class to jswdk-1.0.1examplesWeb-Infjspbeansshopping

●Copy error.html to jswdk-1.0.1webpages

● Once the server is started, you can use http: // localhost: 8080/examples/jsp/shopping/EShop. jsp to access this application.

Weigh JSP and servlets
In this example, we carefully examine the control level and flexibility provided by JSP Model 2. In particular, we have seen how to mine the best features of servlets and JSP, to the greatest extent possible to separate content and expressions. Correct use of the Model2 architecture allows you to centralize all processing logic in the control servlet so that JSP pages are only responsible for expressing or viewing. However, the disadvantage of using Model 2 is that it is complicated. Therefore, Model 1 may be more suitable for simple applications.

<Full text>

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.