Example explains JSP Model2 Architecture (medium) _JSP programming

Source: Internet
Author: User
Tags flush
Understanding "Music Without Borders"
The main interface for "Music Without Borders" is the JSP page eshop.jsp (see Code Listing 1). You will notice that this page is almost exclusively a dedicated user interface and does not assume any processing tasks--is an ideal JSP solution. Also, note that another JSP page cart.jsp (see Code Listing 2) is eshop.jsp by the instruction <jsp:include page= "cart.jsp" flush= "true"/> included.

Code Listings 1:eshop.jsp

<%@ page session= "true"%>



<title>music without borders</title>


<body bgcolor= "#33CCFF" >

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

Music without Borders

</font>


<center>

<form name= "Shoppingform"

action= "/examples/servlet/shoppingservlet"

method= "POST" >

<b>CD:</b>

<select name=cd>

<option>yuan | The Guo Brothers | Our | $14.95</option>

<option>drums of Passion | Babatunde Olatunji | Nigeria | $16.95</option>

<option>kaira | Tounami diabate| Mali | $16.95</option>

<option>the Lion is Loose | Eliades Ochoa | Cuba | $13.95</option>

<option>dance the Devil Away | Outback | Australia | $14.95</option>

<option>record of Changes | Samulnori | Korea | $12.95</option>

<option>djelika | Tounami Diabate | Mali | $14.95</option>

<option>rapture | Nusrat Fateh Ali Khan | Pakistan | $12.95</option>

<option>cesaria Evora | Cesaria Evora | Cape Verde | $16.95</option>

<option>ibuki | Kodo | Japan | $13.95</option>

</select>

<b>quantity: </b><input type= "text" Name= "qty" size= "3" value=1>

<input type= "hidden" name= "action" value= "ADD" >

<input type= "Submit" name= "Submit" value= "Add to Cart" >

</form>

</center>

<p>

<jsp:include page= "cart.jsp" flush= "true"/>

</body>


Code Listings 2:cart.jsp

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

<%

Vector buylist = (vector) session.getvalue ("Shopping.shoppingcart");

if (buylist!= null && (buylist.size () > 0)) {

%>

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

<%

for (int index=0; index < buylist.size (); index++) {

cd Anorder = (CD) buylist.elementat (index);

%>

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

<td>

<form name= "Deleteform"

action= "/examples/servlet/shoppingservlet"

method= "POST" >

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

<input type= "hidden" name= "Delindex" value= ' <%= index%> ' >

<input type= "hidden" name= "action" value= "DELETE" >

</form>

</td>

</tr>

<%}%>

</table>

<p>

<form name= "Checkoutform"

action= "/examples/servlet/shoppingservlet"

method= "POST" >

<input type= "hidden" name= "action" value= "CHECKOUT" >

<input type= "Submit" Name= "Checkout" value= "Checkout" >

</form>

</center>

<%}%>

Here, cart.jsp manipulates the expression of a web-based shopping cart, and in the MVC system, the shopping cart acts as model.

Observe the script fragment at the beginning of the cart.jsp:

<%

Vector buylist = (vector) session.getvalue ("Shopping.shoppingcart");

if (buylist!= null && (buylist.size () > 0)) {

%>

This script mainly takes out the shopping cart from the session. If the cart is empty or has not been created, it is not displayed; Therefore, when the user accesses the application for the first time, the view presented to him is shown in Figure 3:


Figure 3: Music Without Borders, main view

Picture button text: Put in shopping cart

If the shopping cart is not empty, the selected items are taken out of the cart in turn, as shown in the following script fragment:

<%

for (int index=0; index < buylist.size (); index++) {

cd Anorder = (CD) buylist.elementat (index);

%>

Once the variable describing the item is created, it is embedded in the static HTML template using the JSP expression directly. Figure 4 shows the view after the user puts some items into the cart.


Figure 4: Music without Borders, shopping cart view

Text: Music without Borders: Musical no borders; Quantity: quantity; ALBUM: Record; ARTIST: singer; COUNTRY: country; Price: prices; Delete: remove; Checkout: Checkout.

The important point to note here is that the processing of all the actions implemented in eshop.jsp and cart.jsp is controlled by a single servlet――shoppingservlet.java, as shown in Listing 3:

Code Listings 3:shoppingservlet.java

Import java.util.*;

Import java.io.*;

Import javax.servlet.*;

Import javax.servlet.http.*;

Import shopping. CD;

public class Shoppingservlet extends HttpServlet {

public void init (ServletConfig conf) throws Servletexception {

Super.init (conf);

}

public void DoPost (HttpServletRequest req, httpservletresponse Res)

Throws Servletexception, IOException {

HttpSession session = Req.getsession (false);

if (session = null) {

Res.sendredirect ("http://localhost:8080/error.html");

}

Vector buylist=

(Vector) Session.getvalue ("Shopping.shoppingcart");

String action = req.getparameter ("action");

if (!action.equals ("CHECKOUT")) {

if (Action.equals ("DELETE")) {

String del = req.getparameter ("Delindex");

int d = (new Integer (Del)). Intvalue ();

Buylist.removeelementat (d);

else if (action.equals ("ADD")) {

Have you purchased the same CD before?

Boolean match=false;

CD ACD = GETCD (req);

if (buylist==null) {

Put the first CD in the shopping Cart

Buylist = new Vector (); First Order

Buylist.addelement (ACD);

else {//not the first time to buy

for (int i=0; i< buylist.size (); i++) {

cd cd = (CD) buylist.elementat (i);

if (Cd.getalbum (). Equals (Acd.getalbum ())) {

Cd.setquantity (Cd.getquantity () +acd.getquantity ());

Buylist.setelementat (Cd,i);

Match = true;

}//if name matches end

}//For Loop end

if (!match)

Buylist.addelement (ACD);

}

}

Session.putvalue ("Shopping.shoppingcart", buylist);

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

ServletContext sc = Getservletcontext ();

RequestDispatcher rd = sc.getrequestdispatcher (URL);

Rd.forward (req, res);

else if (action.equals ("CHECKOUT")) {

float total = 0;

for (int i=0; i< buylist.size (); i++) {

cd Anorder = (CD) buylist.elementat (i);

Float price= anorder.getprice ();

int qty = anorder.getquantity ();

Total + = (Price * qty);

}

Total = 0.005;

String amount = new Float (total). toString ();

int n = amount.indexof ('. ');

Amount = amount.substring (0,n+3);

Req.setattribute ("Amount", amount);

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

ServletContext sc = Getservletcontext ();

RequestDispatcher rd = sc.getrequestdispatcher (URL);

Rd.forward (Req,res);

}

}

Private CD Getcd (HttpServletRequest req) {

Imagine how ugly it would be if these were all in a script fragment.

String MYCD = Req.getparameter ("CD");

String qty = req.getparameter ("qty");

StringTokenizer t = new StringTokenizer (MYCD, "|");

String album= T.nexttoken ();

String artist = T.nexttoken ();

String country = T.nexttoken ();

String Price = T.nexttoken ();

Price = Price.replace (' $ ', '). Trim ();

cd cd = new CD ();

Cd.setalbum (album);

Cd.setartist (artist);

Cd.setcountry (country);

Cd.setprice (New Float (Price). Floatvalue ());

Cd.setquantity (New Integer (qty)). Intvalue ());

return CD;

}

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