⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 0061.htm

📁 JspServlet教程专栏 对javaservlet讲述的非常详细
💻 HTM
字号:
<html>

<head>
<title>新时代软件教程:操作系统 主页制作 服务器 设计软件 网络技术 编程语言 文字编辑</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
<!--
body, table {font-size: 9pt; font-family: 宋体}
a {text-decoration:none}
a:hover {color: red;text-decoration:underline}
.1  {background-color: rgb(245,245,245)}
-->
</style>
</head>
<p align="center"><script src="../../1.js"></script></a>
    <p align="center"><big><strong>JSP 的模板</strong></big></p>

<div align="right">(文/Scott Ferguson)</div>

<p>
<br>
引论 <br>
样板的框架: Hello, World <br>
Servlet 评论 <br>
展示留言板 <br>
留言板的模式 <br>
作为应用属性的留言板 <br>
留言板的逻辑 <br>
结论 <br>
<br>
<br>
<br>
引论 <br>
<br>
JSP的强大优势在于把一种应用的商务逻辑和它的介绍分离开来。用 Smalltalk的面向对象的术语来说, JSP鼓励MVC(model-view-controller)的web应用。JSP的classes 或 beans 是模型, JSP 是这个视图, 而Servlet是控制器。 <br>
<br>
这个例子是一个简单的留言板。用户登录和留言。 It is also available in the Resin demos <br>
<br>
Role Implementation <br>
Model A GuestBook of Guests. <br>
View login.jsp for new users<br>
add.jsp for logged-in users. <br>
Controller GuestJsp, a servlet to manage the state. <br>
<br>
<br>
样板的框架: Hello, World <br>
<br>
GuestJsp的框架把 &quot;Hello, World&quot; 这个字符串传给login.jsp页面。这个框架为留言板设立结构。具体细节将在下面补充。 <br>
<br>
这个例子被编译后可以浏览到:<br>
<br>
http://localhost:8080/servlet/jsp.GuestJsp <br>
<br>
你可以看到这样的页面: <br>
<br>
Hello, world <br>
<br>
JSP模板是以Servlet的处理开始然后把处理结果传给JSP页进行格式化。 <br>
<br>
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. <br>
<br>
GuestJsp.java: Skeleton package jsp.GuestJsp;<br>
<br>
import java.io.*;<br>
import java.util.*;<br>
<br>
import javax.servlet.*;<br>
import javax.servlet.http.*;<br>
<br>
/**<br>
* GuestJsp is a servlet controlling user<br>
* interaction with the guest book.<br>
*/<br>
public class GuestJsp extends HttpServlet {<br>
/**<br>
* doGet handles GET requests<br>
*/<br>
public void doGet(HttpServletRequest req,<br>
HttpServletResponse res)<br>
throws ServletException, IOException<br>
{<br>
// Save the message in the request for login.jsp<br>
req.setAttribute(&quot;message&quot;, &quot;Hello, world&quot;);<br>
<br>
// get the application object<br>
ServletContext app = getServletContext();<br>
<br>
// select login.jsp as the template<br>
RequestDispatcher disp;<br>
disp = app.getRequestDispatcher(&quot;login.jsp&quot;);<br>
<br>
// forward the request to the template<br>
disp.forward(req, res);<br>
}<br>
}<br>
<br>
<br>
The servlet and the jsp page communicate with attributes in the HttpRequest object. The skeleton stores &quot;Hello, World&quot; in the &quot;message&quot; attribute. When login.jsp starts, it will grab the string and print it. <br>
<br>
Since Resin's JavaScript understands extended Bean patterns, it translates the request.getAttribute(&quot;message&quot;) into the JavaScript equivalent request.attribute.message. <br>
<br>
login.jsp: Skeleton &lt;%@ page language=javascript %&gt;<br>
<br>
&lt;head&gt;<br>
&lt;title&gt;&lt;%= request.attribute.message %&gt;&lt;/title&gt;<br>
&lt;/head&gt;<br>
<br>
&lt;body bgcolor='white'&gt;<br>
&lt;h1&gt;&lt;%= request.attribute.message %&gt;&lt;/h1&gt;<br>
&lt;/body&gt;<br>
<br>
<br>
<br>
Servlet Review<br>
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. <br>
<br>
doc <br>
index.html <br>
login.jsp <br>
add.jsp <br>
WEB-INF <br>
classes <br>
jsp <br>
GuestJsp.class <br>
GuestBook.class <br>
Guest.class <br>
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. <br>
<br>
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. <br>
<br>
Displaying the Guest Book <br>
<br>
The next step, after getting the basic framework running, is to create the model. <br>
<br>
The GuestBook model<br>
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. <br>
<br>
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. That's the responsibility of the GuestJsp servlet. <br>
<br>
jsp.Guest.java API package jsp;<br>
<br>
public class Guest {<br>
Guest();<br>
public String getName();<br>
public String getComment();<br>
}<br>
<br>
<br>
Resin's JavaScript recognizes Bean patterns. So JSP pages using JavaScript can access getName() and getComment() as properties. For example, you can simply use guest.name and guest.comment <br>
<br>
jsp.GuestBook.java API package jsp;<br>
<br>
public class GuestBook {<br>
GuestBook();<br>
void addGuest(String name, String comment);<br>
public Iterator iterator();<br>
}<br>
<br>
<br>
Resin's JavaScript also recognizes the iterator() call, so you can use a JavaScript for ... each to get the guests: <br>
<br>
for (var guest in guestBook) {<br>
...<br>
}<br>
<br>
<br>
<br>
GuestBook as application attribute<br>
To keep the example simple, GuestJsp stores the GuestBook in the application (ServletContext). As an example, storing data in the application is acceptable but for full-fledged applications, it's better just to use the application to cache data stored elsewhere. <br>
<br>
jsp.GuestJsp.java // get the application object<br>
ServletContext app = getServletContext();<br>
<br>
GuestBook guestBook;<br>
<br>
// The guestBook is stored in the application<br>
synchronized (app) {<br>
guestBook = (GuestBook) app.getAttribute(&quot;guest_book&quot;);<br>
<br>
// If it doesn't exist, create it.<br>
if (guestBook == null) {<br>
guestBook = new GuestBook();<br>
guestBook.addGuest(&quot;Harry Potter&quot;, &quot;Griffindor rules&quot;);<br>
guestBook.addGuest(&quot;Draco Malfoy&quot;, &quot;Slytherin rules&quot;);<br>
app.setAttribute(&quot;guest_book&quot;, guestBook);<br>
}<br>
}<br>
<br>
RequestDispatcher disp;<br>
disp = app.getRequestDispatcher(&quot;login.jsp&quot;);<br>
<br>
// synchronize the Application so the JSP file <br>
// doesn't need to worry about threading<br>
synchronized (app) {<br>
disp.forward(req, res);<br>
}<br>
<br>
<br>
The JSP file itself is simple. It grabs the guest book from the application and displays the contents in a table. Normally, application objects need to be synchronized because several clients may simultaneously browse the same page. GuestJsp has taken care of synchronization before the JSP file gets called. <br>
<br>
login.jsp: Display Guest Book &lt;%@ page language=javascript %&gt;<br>
<br>
&lt;head&gt;<br>
&lt;title&gt;Hogwarts Guest Book&lt;/title&gt;<br>
&lt;/head&gt;<br>
<br>
&lt;body bgcolor='white'&gt;<br>
<br>
&lt;h1&gt;Hogwarts Guest Book&lt;/h1&gt;<br>
&lt;table&gt;<br>
&lt;tr&gt;&lt;td width='25%'&gt;&lt;em&gt;Name&lt;/em&gt;&lt;td&gt;&lt;em&gt;Comment&lt;/em&gt;<br>
&lt;%<br>
var guestBook = application.attribute.guest_book<br>
<br>
for (var guest in guestBook) {<br>
out.writeln(&quot;&lt;tr&gt;&lt;td&gt;&quot; + guest.name + &quot;&lt;td&gt;&quot; + guest.comment);<br>
}<br>
%&gt;<br>
&lt;/table&gt;<br>
<br>
&lt;/body&gt;<br>
<br>
<br>
Hogwarts Guest Book<br>
Name Comment <br>
Harry Potter Griffindor Rules <br>
Draco Malfoy Slytherin Rules <br>
<br>
<br>
<br>
Guest book logic <br>
<br>
The guest book logic is simple. If the user has not logged in, she sees comments and a form to log in. After login, she'll see the comments and a form to add a comment. login.jsp formats the login page and add.jsp formats the add comment page. <br>
<br>
GuestJsp stores login information in the session variable. <br>
<br>
Form Variable Meaning <br>
action 'login' to login or 'add' to add a comment <br>
name user name <br>
password user password <br>
comment comment for the guest book <br>
<br>
Guest book logic ...<br>
<br>
// name from the session<br>
String sessionName = session.getValue(&quot;name&quot;);<br>
<br>
// action from the forms<br>
String action = request.getParameter(&quot;action&quot;);<br>
<br>
// name from the login.jsp form<br>
String userName = request.getParameter(&quot;name&quot;);<br>
<br>
// password from the login.jsp form<br>
String password = request.getParameter(&quot;password&quot;);<br>
<br>
// comment from the add.jsp form<br>
String comment = request.getParameter(&quot;comment&quot;);<br>
<br>
// login stores the user in the session<br>
if (action != null &amp;&amp; action.equals(&quot;login&quot;) &amp;&amp;<br>
userName != null &amp;&amp;<br>
password != null &amp;&amp; password.equals(&quot;quidditch&quot;)) {<br>
session.putValue(&quot;name&quot;, userName);<br>
}<br>
<br>
// adds a new guest<br>
if (action != null &amp;&amp; action.equals(&quot;add&quot;) &amp;&amp;<br>
sessionName != null &amp;&amp;<br>
comment != null) {<br>
guestBook.addGuest(sessionName, comment);<br>
}<br>
<br>
String template;<br>
// if not logged in, use login.jsp<br>
if (session.getValue(&quot;name&quot;) == null)<br>
template = &quot;login.jsp&quot;;<br>
// if logged in, use add.jsp<br>
else<br>
template = &quot;add.jsp&quot;;<br>
<br>
RequestDispatcher disp;<br>
disp = app.getRequestDispatcher(template);<br>
<br>
...<br>
<br>
<br>
login.jsp and add.jsp just append different forms to the display code in the previous section. <br>
<br>
login.jsp &lt;%@ page language=javascript %&gt;<br>
&lt;head&gt;<br>
&lt;title&gt;Hogwarts Guest Book: Login&lt;/title&gt;<br>
&lt;/head&gt;<br>
&lt;body bgcolor='white'&gt;<br>
<br>
&lt;h1&gt;Hogwarts Guest Book&lt;/h1&gt;<br>
&lt;table&gt;<br>
&lt;tr&gt;&lt;td width='25%'&gt;&lt;em&gt;Name&lt;/em&gt;&lt;td&gt;&lt;em&gt;Comment&lt;/em&gt;<br>
&lt;%<br>
var guestBook = application.attribute.guest_book<br>
<br>
for (var guest in guestBook) {<br>
out.writeln(&quot;&lt;tr&gt;&lt;td&gt;&quot; + guest.name + &quot;&lt;td&gt;&quot; + guest.comment);<br>
}<br>
%&gt;<br>
&lt;/table&gt;<br>
&lt;hr&gt;<br>
<br>
&lt;form action='GuestJsp' method='post'&gt;<br>
&lt;input type=hidden name='action' value='login'&gt;<br>
&lt;table&gt;<br>
&lt;tr&gt;&lt;td&gt;Name:&lt;td&gt;&lt;input name='Name'&gt;<br>
&lt;tr&gt;&lt;td&gt;Password:&lt;td&gt;&lt;input name='Password' type='password'&gt;<br>
&lt;tr&gt;&lt;td&gt;&lt;input type=submit value='Login'&gt;<br>
&lt;/table&gt;<br>
&lt;/form&gt;<br>
&lt;/body&gt;<br>
<br>
<br>
<br>
Conclusion <br>
<br>
The Resin demo shows a few ways to extend the guest book, including adding some intelligence to the form processing. However, as forms get more intelligent, even JSP templates become complicated. There is a solution: XTP templates. <br>
</p>

  </table>
<p align="center"><script src="../../2.js"></script></a>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -