JSP高級技術如何開發動態網站
2024-09-05 00:19:10
供稿:網友
近年來,jsp技術現在已經成為一種卓越的動態網站開發技術。java開發者出于各種理由喜愛使用jsp。有人喜愛其“一次開發,處處使用”的性能,另外的人覺得jsp使java成為一種易學的服務器端scripting語言。但是,jsp最大的長處在它將頁面的表現和頁面的商業邏輯分開了。本章中,我們將深入地討論如何使用jsp模式2體系結構來開發網站。這一模式可以被看作是通用模式瀏覽控制模式(popular model-view-controller,mvc)模式的服務器端實現。
servlets有何缺陷?
當jsp成為開發動態網站的主要技術時,可能有人會問為何在jsp技術中我們不強調servlets。servlets的應用是沒有問題的。它們非常適于服務器端的處理和編程,并且它們會長期駐留在他們現在的位置。但是,從結構上說,我們可以將jsp看作是servlet的一個高層的抽象實現,特別是在servlet 2.2 api下。但是,你仍然不能無拘束地使用servlet;它們并不適合每一個人。例如,頁面設計者可以方便地使用html或者xml工具開發jsp頁面,但servlet卻更適合于后端開發者使用,他們的工具是ide——一個需要更多編程訓練的開發領域。當發布servlet時,每個開發者必須小心地確定在頁面表現和頁面邏輯之間沒有緊密的關聯出現。你可以使用第三方html包裝工具,如htmlkona來混合html和servlet代碼。即使如此,這點靈活性還不足以讓你自由地改變風格本身。例如,你希望從html改變到dhtml,則包裝本身需要被小心地測試,以確保新的格式可以正確使用。在最壞的情況下,包裝不可用,你就需要應變馬來表現動態內容。所以,需要一種新的解決方案。你將會看到,一種方案就是混合jsp和servlet的使用。
不同的方式
早期的jsp標準給出了兩種使用jsp的方式。這些方式,都可以歸納為jsp模式1和jsp模式2,主要的差別在于處理大量請求的位置不同。在模式1中(圖1),jsp頁面獨自響應請求并將處理結果返回客戶。這里仍然有表現和內容的分離,因為所有的數據依靠bean來處理。盡管模式1 可以很好的滿足小型應用的需要,但卻不能滿足大型應用的要求。大量使用模式1,常常會導致頁面被嵌入大量的script或者java代碼。特別是當需要處理的商業邏輯很復雜時,情況會變得嚴重。也許對于java程序員來說,這不算大的問題。但如果開發者是前端界面設計人員——在大型項目中,這非常常見,——則代碼的開發和維護將出現困難。在任何項目中,這樣的模式多少總會導致定義不清的響應和項目管理的困難。
在圖2中顯示的模式2 結構,是一種面向動態內容的實現,結合了servlet 和jsp技術。它利用了兩種技術原有的優點,采用jsp來表現頁面,采用servlets來完成大量的處理。這里,servlet扮演一個控制者的角色,并負責響應客戶請求。接著,servlet創建jsp需要的bean和對象,再根據用戶的行為,決定將那個jsp頁面發送給用戶。特別要注意,jsp頁面中沒有任何商業處理邏輯;它只是簡單地檢索servlet先前創建的bean 或者對象,再將動態內容插入預定義的模版。從開發的觀點看,這一模式具有更清晰的頁面表現,清楚的開發者角色劃分,可以充分地利用開發小組中的界面設計人員。事實上,越是復雜的項目,采用模式2 的好處就越突出。
為了清楚地了解模式2 的開發過程,我們舉一個網上音樂商店的例子。
我們創建一個叫”音樂無國界”的銷售音樂制品的商店?!耙魳窡o國界”在線商店的主界面,是一個叫“音樂無國界”的頁面(代碼1)。你會看到,這個頁面完全著眼于用戶界面,與處理邏輯無關。另外,注意另外一個jsp頁面,cart.jsp(在代碼2中),用<jsp:include page="cart.jsp" flush="true" />.嵌入eshop.jsp中。
listing 1:
eshop.jsp
<%@ page session="true" %>
<html>
<head>
<title>music without borders</title>
</head>
<body bgcolor="#33ccff">
<font face="times new roman,times" size="+3">
music without borders
</font>
<hr><p>
<center>
<form name="shoppingform"
action="/examples/servlet/shoppingservlet"
method="post">
<b>cd:</b>
<select name=cd>
<option>yuan | the guo brothers | china | $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>
</html>
listing 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>
<% } %>
這里,cart.jsp按照mvc的模式1處理基于session的購物車的表現。請看cart.jsp開始處的代碼:
<%
vector buylist = (vector) session.getvalue("shopping.shoppingcart");
if (buylist != null && (buylist.size() > 0)) {
%>
本質上,這段代碼從session中取出“購物車”。如果“購物車”為空或者沒有被創建,它就什么也不顯示。所以,在用戶第一次訪問應用時,其界面如圖:
如果“購物車”不為空,用戶選擇的商品從車中取出,依次顯示在頁面上:
<%
for (int index=0; index < buylist.size(); index++) {
cd anorder = (cd) buylist.elementat(index);
%>
一旦生成一個物品的說明,就使用jsp按照事先設定的模板將其插入靜態html頁面。下圖顯示了用戶選購一些物品后的界面:
需要注意的一個重要的地方是所有關于eshop.jsp,cart.jsp的處理有一個控制servlet,shoppingservlet.java,代碼在源程序3中:
listing 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")) {
//any previous buys of same cd?
boolean match=false;
cd acd = getcd(req);
if (buylist==null) {
//add first cd to the cart
buylist = new vector(); //first order
buylist.addelement(acd);
} else { // not first 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;
} //end of if name matches
} // end of for
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 if all this was in a scriptlet...ugly, eh?
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;
}
}
每次用戶用eshop.jsp增加一個商品,頁面就請求控制servlet。控制servlet決定進一步的行動,并處理增加的商品。接著,控制servlet實例化一個新的bean cd代表選定的商品,并在返回session前更新購物車對象。
listing 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;
}
}
注意,我們的servlet中具有附加的智能,如果一個物品被重復選擇,不會增加新的記錄,而是在以前的記錄上更新計數??刂苨ervlet也響應cart.jsp中的行為,如修改數量,刪除商品,還有結帳。如果結帳,控制通過下述語句轉向checkout.jsp頁面(源程序5):
string url="/jsp/shopping/checkout.jsp";
servletcontext sc = getservletcontext();
requestdispatcher rd = sc.getrequestdispatcher(url);
rd.forward(req,res);
listing 5:
checkout.jsp
<%@ page session="true" import="java.util.*, shopping.cd" %>
<html>
<head>
<title>music without borders checkout</title>
</head>
<body bgcolor="#33ccff">
<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>
結帳頁面簡單地從session中取出購物車,然后顯示每個物品和總金額。這里的關鍵是要結束session,因此在頁面中有一個session.invalidate()調用。這一處理有兩個原因。首先,如果不結束session,用戶的購物車不會被初始化,如果用戶要繼續購買,車中會保留他已經支付過的商品。另外,如果用戶不結帳就離開了,則session會繼續占用有效的資源直到過期。過期時間一般是30分鐘,在一個大的站點上,這樣的情況會很快導致資源耗盡。當然,這是我們不愿看到的。
注意,所有的資源分配在這個例子中是基于session的。所以,你必須確??刂苨ervlet不被用戶訪問,即使是意外的訪問也不允許。這可以在控制檢查到一個非法訪問時用一個簡單的重定向錯誤頁面來處理。見源代碼6。
listing 6:
error.html
<html>
<body>
<h1>
sorry, there was an unrecoverable error!
please try <a href="/examples/jsp/shopping/eshop.jsp">again</a>.
</h1>
</body>
</html>
小結
本章的討論顯示,使用模式2,jsp和servlet可以在功能上最大限度的分開。正確地使用模式2,導致一個中心化的控制servlet,以及只完成顯示的jsp頁面。另一方面,模式2的實現很復雜。因此,在簡單的應用中,可以考慮使用模式1。