几个JSP应用模板
引论 JSP的强大优势在于把一种应用的商务逻辑和它的介绍分离开来。用 Smalltalk的面向对象的术语来说, JSP鼓励MVC (model-view-controller)的web应用。JSP的classes 或 beans 是模型, JSP 是这个视图, 而Servlet是控制器。 这个例子是一个简单的留言板。用户登录和留言。 It is also available in the Resin demos
样板的框架: Hello, World GuestJsp的框架把 "Hello, World" 这个字符串传给login.jsp页面。这个框架为留言板设立结构。具体细节将在下面补充。 这个例子被编译后可以浏览到:http://localhost:8080/servlet/jsp.GuestJsp 你可以看到这样的页面:
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.
The servlet and the jsp page communicate with attributes in the HttpRequest object. The skeleton stores "Hello, World" in the "message" attribute. When login.jsp starts, it will grab the string and print it. Since Resin's JavaScript understands extended Bean patterns, it translates the request.getAttribute("message") into the J avaScript equivalent request.attribute.message.
Servlet Review For those coming to JSP from an ASP or CGI background, Servlets replace CGI scripts taking advantage of Java's strength in dynamic class loading. A servlet is just a Java class which extends Servlet or HttpServlet and placed in the proper directory. Resin will automatically load the servlet and execute it. doc index.html login.jsp add.jsp WEB-INF classes jsp GuestJsp.class GuestBook.class Guest.class The url /servlet/classname forwards the request to the Servlet Invoker. The Invoker will dynamically load the Java class classname from doc/WEB-INF/classes and try to execute the Servlet's service method. Resin checks the class file periodically to see if the class has changed. If so, it will replace the old servlet with the new servlet. Displaying the Guest Book The next step, after getting the basic framework running, is to create the model. The GuestBook model The guest book is straightforward so I've just included the API here. It conforms to Bean patterns to simplify the JavaScript. The same API will work for HashMap, file-based, and database implementations. JSP files only have access to public methods. So a JSP file cannot create a new GuestBook and it can't add a new guest. 上一篇:用Jsp开发网上商城 下一篇:JSP下通过socket访问数据库 更多相关文章
|
推荐文章
精彩文章
|