By Scott Ferguson Blueski編譯
目錄如下: 1 引論 2 范例的框架: Hello, World 3 Servlet 評論 4 展示留言本 5 留言本的模式 6 作為應用屬性的留言本 7 留言本的邏輯 8 結論
1 引論
JSP的強大優勢在于把一種應用的商務邏輯和它的介紹分離開來。用 Smalltalk的面向對象的術語來說, JSP鼓勵MVC(model-view-controller)的web應用。JSP的classes 或 beans 是模型, JSP 是這個視圖, 而Servlet是控制器。
這個例子是一個簡單的留言本,包括用戶登錄和留言。它被作為Resin平臺的示范: --執行角色 --模型 A 留言本 --用于新用戶的login.jsp --用于已注冊用戶的add.jsp --控制器 GuestJsp, 一個用來管理狀態的servlet
2 樣板的框架: Hello, World
GuestJsp servlet的框架把 "Hello, World" 這個字符串傳給login.jsp頁面。這個框架為留言本設立結構。具體細節將在下面補充。
這個例子被編譯后可以瀏覽到:
http://localhost:8080/servlet/jsp.GuestJsp
你可以看到頁面上有這樣的顯示: Hello, world
JSP模板是以Servlet的處理開始然后把處理結果傳給JSP頁進行格式化。
以下使用了一個Servlet2.1 ServletContext的特性 getRequestDispatcher()。 請求的調度器在服務器上讓servlets直接向前傳送并包括了任何可能的子請求。對SSI包含來說這是一個更靈活的取代做法。 在servlet文件中請求的調度器可以包含任何頁面,servlet,或JSP的結果。 GuestJsp將使用dispatcher.forward()來將控制傳給JSP頁進行格式化。
GuestJsp.java: Skeleton package jsp.GuestJsp;
import java.io.*; import java.util.*;
import javax.servlet.*; import javax.servlet.http.*;
/** * GuestJsp is a servlet controlling user * interaction with the guest book. */ public class GuestJsp extends HttpServlet { /** * doGet handles GET requests */ public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { // Save the message in the request for login.jsp req.setAttribute("message", "Hello, world");
// get the application object ServletContext app = getServletContext();
// select login.jsp as the template RequestDispatcher disp; disp = app.getRequestDispatcher("login.jsp");
// forward the request to the template disp.forward(req, res); } }
servlet和jsp頁使用HttpRequest對象中的屬性進行通信。skeleton在"message"屬性中保存了"Hello, World"。 當login.jsp啟動時,它將捕捉到該字符串并將其打印出來。
由于Resin的JavaScript能夠讀取擴充的Bean模型,它可以將request.getAttribute("message")轉換成為 JavaScript的對應物 request.attribute.message。
login.jsp: Skeleton <%@ page language=javascript %>
<head> <title><%= request.attribute.message %></title> </head>
<body bgcolor='white'> <h1><%= request.attribute.message %></h1> </body>
3 Servlet的復習
對于來自于ASP或CGI背景并轉向jsp的人來說, Servlets代替CGI腳本體現了Java在動態類加載方面的優勢。servlet就是一個Java類, 它對Servlet或HttpServlet進行了擴展并放置到適當的路徑中。Resin將自動加載servlet并執行它。
url /servlet/classname將request提交給Servlet請求器。請求器會從doc/WEB-INF/classes自動加載Java類的類名 并試圖執行Servlet的service方法。
Resin將定期檢查類文件以判斷是否被修改過。如果被修改過,則將用新的servlet取代舊的。
4 顯示留言本
在基本框架已經運行后, 下一步是創建model。
5 留言本模型
留言本是很直接的,這里知識包含了一下API。它遵從Bean模型以簡化JavaScript。 同樣的API可以工作于HashMap, 基于文件,以及數據庫應用。
JSP文件只能存取public方法。所以JSP文件無法創建一個新的留言本或者增加一個新用戶。 這是GuestJsp servlet的責任。
jsp.Guest.java API package jsp;
public class Guest { Guest(); public String getName(); public String getComment(); }
Resin的JavaScript能讀取Bean模型。所以使用JavaScript的JSP頁面可以存取getName()和getComment() 作為屬性。例如,你可以簡化使用guest.name和guest.comment。
jsp.GuestBook.java API package jsp;
public class GuestBook { GuestBook(); void addGuest(String name, String comment); public Iterator iterator(); }
Resin的JavaScript同樣可以讀取iterator()調用,所以你可以使用JavaScript用于 ... 任何一個來取得用戶:
for (var guest in guestBook) { ... }
GuestBook作為application屬性 為了使得例子保持簡單,GuestJsp在application (ServletContext)中存取GuestBook。作為例子, 在application中保存數據是可以接受的,但對于完全成熟的應用,最好僅使用application將數據放到其它地方。
jsp.GuestJsp.java // get the application object ServletContext app = getServletContext();
GuestBook guestBook;
// The guestBook is stored in the application synchronized (app) { guestBook = (GuestBook) app.getAttribute("guest_book");
// If it doesn't exist, create it. if (guestBook == null) { guestBook = new GuestBook(); guestBook.addGuest("Harry Potter", "Griffindor rules"); guestBook.addGuest("Draco Malfoy", "Slytherin rules"); app.setAttribute("guest_book", guestBook); } }
RequestDispatcher disp; disp = app.getRequestDispatcher("login.jsp");
// synchronize the Application so the JSP file // doesn't need to worry about threading synchronized (app) { disp.forward(req, res); }
JSP文件本身是簡單的。它從application獲取留言本并在表中顯示內容。通常, application對象需要同步,因為一些客戶端可能同時瀏同一頁面。 GuestJsp在jsp文件被調用之前小心處理了同步情況。
login.jsp: Display Guest Book <%@ page language=javascript %>
<head> <title>Hogwarts Guest Book</title> </head>
<body bgcolor='white'>
<h1>Hogwarts Guest Book</h1> <table> <tr><td width='25%'><em>Name</em><td><em>Comment</em> <% var guestBook = application.attribute.guest_book
for (var guest in guestBook) { out.writeln("<tr><td>" + guest.name + "<td>" + guest.comment); } %>
</body>
Hogwarts Guest Book Name Comment Harry Potter Griffindor Rules Draco Malfoy Slytherin Rules
6 留言本的規則(logic)--作為應用屬性的留言本
留言本的規則是很簡單的。如果用戶沒有登錄,他會看到一個提示和登錄表。 登錄后他會看到提示并在一個表中加入留言。 login.jsp給出了登錄的頁面,add.jsp給出了 增加流言的頁面。
GuestJsp在session變量中保存了規則信息。
執行'login'來登錄或 'add'來增加留言。 其中 name: 用戶名 password: 口令 comment:留言
7 留言本規則 ...
// name from the session String sessionName = session.getValue("name");
// action from the forms String action = request.getParameter("action");
// name from the login.jsp form String userName = request.getParameter("name");
// password from the login.jsp form String password = request.getParameter("password");
// comment from the add.jsp form String comment = request.getParameter("comment");
// login stores the user in the session if (action != null && action.equals("login") && userName != null && password != null && password.equals("quidditch")) { session.putValue("name", userName); }
// adds a new guest if (action != null && action.equals("add") && sessionName != null && comment != null) { guestBook.addGuest(sessionName, comment); }
String template; // if not logged in, use login.jsp if (session.getValue("name") == null) template = "login.jsp"; // if logged in, use add.jsp else template = "add.jsp";
RequestDispatcher disp; disp = app.getRequestDispatcher(template);
...
login.jsp和add.jsp僅加上了不同forms在前一個section中顯示代碼。
login.jsp <%@ page language=javascript %> <head> <title>Hogwarts Guest Book: Login</title> </head> <body bgcolor='white'>
<h1>Hogwarts Guest Book</h1> <table> <tr><td width='25%'><em>Name</em><td><em>Comment</em> <% var guestBook = application.attribute.guest_book
for (var guest in guestBook) { out.writeln("<tr><td>" + guest.name + "<td>" + guest.comment); } %>
<hr>
<form action='GuestJsp' method='post'> <input type=hidden name='action' value='login'> <table> <tr><td>Name:<td><input name='Name'> <tr><td>Password:<td><input name='Password' type='password'> <tr><td><input type=submit value='Login'>
</form> </body>
8 結論
Resin示例演示了擴充留言本的一些方法,包括加入一些智能的東西用于form處理。然而,由于forms取得更多的只能,即使是JSP模板也變得復雜化了。 有一個結論:XTP模板。
|