Example explains JSP Model2 Architecture (_JSP) programming

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 decides the appropriate actions in turn, and then processes the request parameters for the items to be added. It then illustrates a new CD Bean (see Code Listing 4) that represents the selected item and updates the Cart object within the session.

Code Listings 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 joined CD is checked again, it simply adds a count to the CD bean in the cart. The control servlet can also handle actions that are triggered in cart.jsp, such as deleting items from a shopping cart or checking out. Note that the control servlet has always mastered the power of resources, and it determines which resources are invoked in response to a particular action. For example, changes to the status of a shopping cart, such as additions or deletions, enable the control servlet to send processed requests to the ESHOP.JSP page. This prompts the page to display the main view again, when the data displayed in the shopping cart has been updated. If the user decides to checkout, the request is sent to the checkout.jsp page after processing (see Listing 5) and is implemented by the scheduler as follows:

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

ServletContext sc = Getservletcontext ();

RequestDispatcher rd = sc.getrequestdispatcher (URL);

Rd.forward (Req,res);

Code Listings 5:checkout.jsp

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



<title>music without Borders checkout</title>


<body bgcolor= "#33CCFF" >

<font face= "Times New roman,times" size=+3>

Music without Borders Checkout

</font>


<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>


Checkout.jsp only the total number of shopping carts and all requests from the session, and then displays the selected item and the total price. Figure 5 shows the client's view when the account is closed. Once the user checkout, it is important to remove the session object in time. To take care of this, at the end of the page you need to have a session.invalidate () call. This process is necessary, there are two original: first, if the session is not terminated, the user's shopping cart will not be reinitialized, when he did not checkout and try to start a new round of shopping, his shopping cart will still retain the items he has purchased. Second, if the user leaves without checking out, the session object is not invalidated and will still consume valuable system resources until it expires. Because the default session expiration is 30 minutes, this situation can quickly deplete system resources on a high load system. Of course we know what it means to run out of system resources by an application!


Figure 5: Music Without Borders, checkout view

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

Note that in this example all resource allocations are based on the session, because the model is stored in the session. Therefore, you must make sure that the control servlet is not accessed by the user, even if unexpected access is not allowed. To resolve this problem, you can automatically redirect to the redirection error page when the control servlet checks to an illegal access. (See Code Listing 6)

Code Listings 6:error.html


<body>


Sorry, there was a unrecoverable error! <br>

Please try <a href= "/examples/jsp/shopping/eshop.jsp" &GT;AGAIN&LT;/A&GT;.


</body>


Configure "Music Without Borders"
I assume you are using the latest version of Sun's JavaServer Web Development kit (Java Server Web Development Kit-JSWDK) for illustrative purposes. Suppose this server is installed in the jswdk-1.0.1 directory-this is its default installation path in Windows, and the files for "music borderless" applications should be configured as follows:

Create a shopping directory under the 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 compile. java files

Copy Shoppingservlet.class to Jswdk-1.0.1webpagesweb-infservlets

Create a shopping directory under the 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 the application

Weighing JSP and Servlets
In this example, we examined the control level and flexibility provided by JSP Model 2. In particular, we have seen how to exploit the best features of Servlets and JSP, separating content and expression to the greatest extent possible. Using the Model2 architecture correctly, you can centralize all the processing logic in the control servlet so that the JSP page is only responsible for the expression or view. However, the disadvantage of using Model 2 is that it is complex. As a result, model 1 may be more appropriate in a simple application.

< finished full >

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.