在线看毛片视频-国产免费av在线-欧美日韩一区二区三区-国产成人无码av在线播放无广告-亚洲人va欧美va人人爽-国产第一草草-西班牙黄色片-四虎在线网站8848-最新av片免费网站入口-东京热无码中文字幕av专区-日本大人吃奶视频xxxx-欧美精品一区二区三区四区五区-国产片天天弄-国产免费内射又粗又爽密桃视频-欧美爱爱网站-日韩v欧美

當前位置:雨林木風下載站 > 技術開發教程 > 詳細頁面

JSP 的模板

JSP 的模板

更新時間:2022-05-13 文章作者:未知 信息來源:網絡 閱讀次數:

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
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);
}
}


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 JavaScript equivalent 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>



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. That's the responsibility of the GuestJsp servlet.

jsp.Guest.java API package jsp;

public class Guest {
Guest();
public String getName();
public String getComment();
}


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

jsp.GuestBook.java API package jsp;

public class GuestBook {
GuestBook();
void addGuest(String name, String comment);
public Iterator iterator();
}


Resin's JavaScript also recognizes the iterator() call, so you can use a JavaScript for ... each to get the guests:

for (var guest in guestBook) {
...
}



GuestBook as application attribute
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.

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);
}


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.

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



Guest book logic

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.

GuestJsp stores login information in the session variable.

Form Variable Meaning
action 'login' to login or 'add' to add a comment
name user name
password user password
comment comment for the guest book

Guest book logic ...

// 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 and add.jsp just append different forms to the display code in the previous 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>



Conclusion

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.


--------------------------------------------------------------------------------

Home | Resin | Download | Sales | FAQ | Site Map
Copyright ? 1998-2000 Caucho Technology. All rights reserved.

Last modified: Sat, 11 Mar 2000 20:22:52 -0800 (PST) 

溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統下載排行

在线看毛片视频-国产免费av在线-欧美日韩一区二区三区-国产成人无码av在线播放无广告-亚洲人va欧美va人人爽-国产第一草草-西班牙黄色片-四虎在线网站8848-最新av片免费网站入口-东京热无码中文字幕av专区-日本大人吃奶视频xxxx-欧美精品一区二区三区四区五区-国产片天天弄-国产免费内射又粗又爽密桃视频-欧美爱爱网站-日韩v欧美
  • <li id="86scu"><menu id="86scu"></menu></li>
    <li id="86scu"></li>
    <button id="86scu"></button>
  • <s id="86scu"></s><button id="86scu"><menu id="86scu"></menu></button>
  • 精品久久久久久久免费人妻| 91国产精品视频在线观看| 欧美大黑帍在线播放| 一本久道中文无码字幕av| 欧美三级在线观看视频| av动漫在线播放| 日韩精品aaa| 国产卡一卡二在线| 在线免费黄色网| 99sesese| 超碰超碰超碰超碰超碰| 99中文字幕在线| 九九九九九九九九| 一女二男3p波多野结衣| 久久久久久久久影视| 很污的网站在线观看| 国产日韩av网站| 福利视频免费在线观看| 视频在线观看免费高清| 超碰在线播放91| 福利在线一区二区三区| 国产aaaaa毛片| 国产精品无码一本二本三本色| 久久久久久久久久久视频| 精品这里只有精品| 无码人妻精品一区二区三区在线| 日韩国产成人无码av毛片| 成品人视频ww入口| 男女高潮又爽又黄又无遮挡| 日韩不卡一二区| 波多野结衣网页| 国产精品久久久久9999爆乳| 四虎免费在线观看视频| 日韩 欧美 自拍| 久久国产成人精品国产成人亚洲| 日韩视频第二页| 在线免费视频a| 欧美aaa在线观看| 免费无码毛片一区二三区| 性欧美极品xxxx欧美一区二区| 日韩视频免费在线播放| 国产精品自在自线| 福利视频一二区| 免费黄色特级片| 久久精品亚洲天堂| 国产夫妻自拍一区| 日日噜噜噜噜久久久精品毛片| 日本精品一区在线| 成年人网站免费视频| www.欧美日本| 国产91porn| 欧美亚洲另类色图| 香港日本韩国三级网站| 一起操在线视频| 欧美国产日韩激情| 中日韩av在线播放| 给我免费播放片在线观看| 欧美成人黄色网址| 国产色一区二区三区| 中文字幕第17页| 国产精品12p| 亚洲一级免费观看| 免费高清在线观看免费| 国产又粗又爽又黄的视频| 黄色片一级视频| 青青在线免费视频| 成人羞羞国产免费网站| 日韩不卡视频一区二区| 国产免费人做人爱午夜视频| 先锋影音男人资源| 涩多多在线观看| 国产免费黄色小视频| 一级片黄色免费| 九九热精品在线播放| www一区二区www免费| 日韩成人午夜影院| 色网站在线视频| 一区二区在线免费看| 熟女少妇精品一区二区| 婷婷视频在线播放| www.成年人| 国产精品久久久毛片| 日韩专区第三页| www.久久av.com| 欧美精品 - 色网| 一区二区三区四区毛片| 狠狠操狠狠干视频| 爱爱爱爱免费视频| 免费网站在线观看黄| 午夜久久久精品| 第一区免费在线观看| wwwwwxxxx日本| 国产高清999| 欧美又黄又嫩大片a级| 在线免费观看av网| 国产永久免费网站| www.亚洲一区二区| 日韩av高清在线看片| 亚洲区成人777777精品| 欧美激情第一区| 色哟哟免费网站| 欧美变态另类刺激| 国产成人久久777777| 九色91popny| 国产 日韩 亚洲 欧美| 天天摸天天碰天天添| 久久久精品麻豆| 在线免费黄色网| 特级西西人体www高清大胆| www精品久久| 激情 小说 亚洲 图片: 伦| 91视频这里只有精品| 最新av在线免费观看| 国产午夜大地久久| 香蕉视频禁止18| www.18av.com| 免费在线观看日韩视频| 亚洲36d大奶网| 中文字幕12页| 国产尤物av一区二区三区| 亚洲爆乳无码专区| 在线观看免费成人av| 精品一二三四五区| 黄色三级视频在线| 福利视频一二区| 手机精品视频在线| 精品免费国产一区二区| www.中文字幕在线| aaa免费在线观看| 日韩免费在线观看av| 中文字幕国内自拍| 日本丰满少妇xxxx| 91xxx视频| 牛夜精品久久久久久久| 日本中文字幕在线视频观看| 小泽玛利亚视频在线观看| 亚洲熟妇无码另类久久久| 男女啪啪的视频| 婷婷六月天在线| 无罩大乳的熟妇正在播放| 中文字幕av不卡在线| 成人免费观看在线| 9l视频白拍9色9l视频| 午夜精品久久久内射近拍高清 | 天堂一区在线观看| 国产精品欧美激情在线观看 | 最近中文字幕一区二区| 国产情侣第一页| 少妇性l交大片| 国产免费成人在线| 两根大肉大捧一进一出好爽视频| 免费的av在线| 美女av免费观看| 国产91porn| 国产女教师bbwbbwbbw| 久久综合在线观看| 日韩高清在线一区二区| 99在线精品免费视频| 国产美女在线一区| 国产精品无码电影在线观看| 欧美一区二区三区综合| 青青草免费在线视频观看| 国产精品12p| 97中文字幕在线| 国产手机视频在线观看| 91国产精品视频在线观看| 污色网站在线观看| 性欧美极品xxxx欧美一区二区| 日本女优爱爱视频| 在线免费看v片| 大桥未久一区二区| 免费av手机在线观看| 国产高清精品软男同| 精品一二三四五区| 男女av免费观看| aⅴ在线免费观看| 中文字幕66页| 黄色片免费在线观看视频| 精品久久久久久久久久中文字幕| 黄色一级二级三级| 国产精品嫩草影视| www.99热这里只有精品| 欧美日韩二三区| 久久福利一区二区| 日本a√在线观看| 日韩国产精品毛片| 少妇性l交大片| 免费高清一区二区三区| jizz欧美激情18| 日韩精品手机在线观看| 少妇性l交大片| 欧美在线一区视频| 亚洲免费一级视频| 日本中文字幕在线视频观看 | 污版视频在线观看| 热99这里只有精品| 网站在线你懂的| 激情五月六月婷婷| 国产欧美激情视频| 国产精品动漫网站|