【www.gdgbn.com--留言】

作者:jspfuns
By Scott Ferguson
引论
样板的框架: Hello, World
Servlet 评论
展示留言板
留言板的模式
作为应用属性的留言板
留言板的逻辑
结论
引论
JSP的强大优势在于把一种应用的商务逻辑和它的介绍分离开来。用 Smalltalk的面向对象的术语来说, JSP鼓励MVC(model-view-controller)的web应用。JSP的classes 或 beans 是模型, JSP 是这个视图, 而Servlet是控制器。
这个例子是一个简单的留言板。用户登录和留言。
 It is also available in the Resin demos
Role Implementation
Model A GuestBook of Guests.
View login.jsp for new users
add.jsp for logged-in users.
Controller GuestJsp, a servlet to manage the state.
样板的框架: Hello, World
GuestJsp的框架把 "Hello, World" 这个字符串传给login.jsp页面。这个框架为留言板设立结构。具体细节将在下面补充。
这个例子被编译后可以浏览到:
http://localhost:8080/servlet/jsp.GuestJsp
你可以看到这样的页面:
Hello, world
JSP模板是以Servlet的处理开始然后把处理结果传给JSP页进行格式化。
Forwarding uses a Servlet 2.1 feature of the ServletContext, getRequestDispatcher(). The request dispatcher lets servlets forward and include any subrequests on the server. It´s a more flexible replacements for SSI includes. The RequestDispatcher can include the results of any page, servlet, or JSP page in a servlet´s page. GuestJsp will use dispatcher.forward() to pass control to the JSP page for formatting.
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

本文来源:http://www.gdgbn.com/zhufuduanxin/6066/