国产午夜成人免费看片无遮挡_日本免费xxxx色视频_免费人成网上在线观看_黄网址在线永久免费观看

當(dāng)前位置:雨林木風(fēng)下載站 > 技術(shù)開發(fā)教程 > 詳細(xì)頁(yè)面

JSP 的模板

JSP 的模板

更新時(shí)間:2022-05-13 文章作者:未知 信息來源:網(wǎng)絡(luò) 閱讀次數(shù):

By Scott Ferguson

引論
樣板的框架: Hello, World
Servlet 評(píng)論
展示留言板
留言板的模式
作為應(yīng)用屬性的留言板
留言板的邏輯
結(jié)論



引論

JSP的強(qiáng)大優(yōu)勢(shì)在于把一種應(yīng)用的商務(wù)邏輯和它的介紹分離開來。用 Smalltalk的面向?qū)ο蟮男g(shù)語(yǔ)來說, JSP鼓勵(lì)MVC(model-view-controller)的web應(yīng)用。JSP的classes 或 beans 是模型, JSP 是這個(gè)視圖, 而Servlet是控制器。

這個(gè)例子是一個(gè)簡(jiǎn)單的留言板。用戶登錄和留言。 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" 這個(gè)字符串傳給login.jsp頁(yè)面。這個(gè)框架為留言板設(shè)立結(jié)構(gòu)。具體細(xì)節(jié)將在下面補(bǔ)充。

這個(gè)例子被編譯后可以瀏覽到:

http://localhost:8080/servlet/jsp.GuestJsp

你可以看到這樣的頁(yè)面:

Hello, world

JSP模板是以Servlet的處理開始然后把處理結(jié)果傳給JSP頁(yè)進(jìn)行格式化。

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) 

溫馨提示:喜歡本站的話,請(qǐng)收藏一下本站!

本類教程下載

系統(tǒng)下載排行

国产午夜成人免费看片无遮挡_日本免费xxxx色视频_免费人成网上在线观看_黄网址在线永久免费观看

  • <label id="pxtpz"><meter id="pxtpz"></meter></label>
      1. <span id="pxtpz"><optgroup id="pxtpz"></optgroup></span>

        日本道精品一区二区三区| 精品人伦一区二区色婷婷| 在线观看亚洲精品视频| 国产精品久久久久久久久免费相片 | 国产sm精品调教视频网站| 欧美xxxx老人做受| 国产伦精品一区二区三区视频青涩 | 色成年激情久久综合| 亚洲一区视频在线| 制服丝袜中文字幕一区| 精品一区二区在线观看| 国产亚洲欧洲一区高清在线观看| 国产高清在线精品| 1000部国产精品成人观看| 欧美性视频一区二区三区| 蜜臀精品久久久久久蜜臀 | 欧美日韩综合在线免费观看| 蜜臀va亚洲va欧美va天堂 | 欧美大胆人体bbbb| 国产suv精品一区二区三区| 一区二区三区四区亚洲| 日韩精品一区二区三区老鸭窝 | 精品一区二区三区久久久| 久久精品人人做人人综合| 色噜噜久久综合| 久久99久久99小草精品免视看| 国产精品人成在线观看免费| 在线观看av不卡| 国模少妇一区二区三区| 一区二区高清视频在线观看| 日韩三级视频中文字幕| 色婷婷一区二区三区四区| 极品少妇一区二区| 亚洲小说春色综合另类电影| 久久久久久久久一| 欧美日韩精品免费观看视频| 成人免费视频一区二区| 日本不卡一二三| 一区二区在线免费观看| 精品国产免费一区二区三区香蕉| 色诱亚洲精品久久久久久| 国产成人综合亚洲91猫咪| 免费在线观看视频一区| 亚洲免费观看高清在线观看| 欧美精品一区二区蜜臀亚洲| 精品视频免费看| 99久久国产综合精品女不卡| 国产精品一区二区果冻传媒| 蜜臀av性久久久久av蜜臀妖精 | ...av二区三区久久精品| 久久影院电视剧免费观看| 欧美日本韩国一区| 91国在线观看| 91老师国产黑色丝袜在线| 国产成人精品免费视频网站| 精品写真视频在线观看 | 亚洲午夜免费电影| 亚洲天堂福利av| 国产精品久久777777| 欧美极品xxx| 国产精品色婷婷久久58| 久久影院视频免费| 欧美国产一区在线| 国产精品欧美经典| 国产精品福利在线播放| 亚洲欧美怡红院| 樱桃视频在线观看一区| 一区二区三区在线播| 亚洲成人激情社区| 日日夜夜免费精品视频| 日韩av一区二区三区| 美日韩一区二区| 国产在线精品一区二区三区不卡 | 亚洲综合一区二区三区| 亚洲精品福利视频网站| 污片在线观看一区二区| 久久成人免费网站| 国产乱码字幕精品高清av| 国产91精品一区二区麻豆网站| 不卡高清视频专区| 色哟哟国产精品| 在线成人免费观看| 26uuu另类欧美亚洲曰本| 中文字幕成人av| 亚洲在线成人精品| 国内国产精品久久| 日本高清视频一区二区| 欧美一区二区三区男人的天堂 | 91麻豆精品国产91久久久资源速度 | 久久婷婷国产综合国色天香| 国产精品视频一二| 日韩高清在线一区| 高清在线成人网| 欧美高清一级片在线| 国产欧美一区二区三区在线看蜜臀| 18欧美乱大交hd1984| 日韩在线卡一卡二| 成人黄页在线观看| 51久久夜色精品国产麻豆| 国产精品国产三级国产a| 日韩中文字幕区一区有砖一区 | 在线看国产一区二区| 精品国内片67194| 亚洲国产一区视频| 丰满亚洲少妇av| 欧美一区二区人人喊爽| 亚洲欧美国产77777| 精品一区二区三区免费| 69堂亚洲精品首页| 亚洲香肠在线观看| 成人av免费在线| 26uuu另类欧美| 青娱乐精品视频| 欧美三级电影一区| 亚洲男人的天堂在线观看| 国产精品资源网| 欧美va在线播放| 日韩高清一区在线| 欧美精品视频www在线观看| 亚洲女同ⅹxx女同tv| 成人午夜视频在线观看| 久久一区二区三区四区| 免费在线观看一区| 日韩视频免费观看高清在线视频| 亚洲成av人在线观看| 欧美在线视频全部完| 亚洲精品久久久久久国产精华液| 成人一区二区三区中文字幕| 久久久久99精品一区| 国产乱码精品1区2区3区| ww久久中文字幕| 国产制服丝袜一区| www国产精品av| 国产精品91一区二区| 国产欧美一二三区| 国产成人av自拍| 国产欧美日韩在线观看| 国产精品一二一区| 国产日韩欧美不卡| 9l国产精品久久久久麻豆| 亚洲欧美在线另类| 色婷婷亚洲精品| 亚洲成在人线在线播放| 欧美久久一区二区| 理论电影国产精品| 精品成人私密视频| 成人午夜电影网站| 亚洲精品免费在线| 欧美日韩美少妇| 蜜臀av一区二区| 国产色产综合产在线视频| 成人久久18免费网站麻豆| 亚洲男人都懂的| 91精品国产综合久久婷婷香蕉| 免费成人av在线| 欧美国产1区2区| 在线观看视频欧美| 麻豆免费精品视频| 日本一区二区视频在线| 色哟哟国产精品| 久久精品99国产精品日本| 久久精品一区二区| 在线视频一区二区免费| 看电影不卡的网站| 亚洲视频在线观看三级| 91精品国产免费| 99久久精品国产一区二区三区| 五月激情综合网| 中文字幕成人在线观看| 欧美精品视频www在线观看| 国产一区二区0| 性欧美疯狂xxxxbbbb| 国产目拍亚洲精品99久久精品| 欧美日韩亚洲综合一区| 国产成人久久精品77777最新版本| 亚洲一区二区三区中文字幕| 精品国产精品网麻豆系列 | 精品视频在线免费| 国产乱码精品一品二品| 香蕉av福利精品导航| 国产欧美日韩麻豆91| 欧美精品99久久久**| 99久久婷婷国产综合精品电影| 日本免费新一区视频| 亚洲色图色小说| 国产日产精品一区| 日韩欧美一级精品久久| 欧美体内she精高潮| 成人的网站免费观看| 久久爱另类一区二区小说| 一区二区三区在线免费观看 | 久久一日本道色综合| 欧美一区二区三区免费视频| 在线观看一区二区视频| 99久久久久免费精品国产| 国产乱一区二区| 韩国v欧美v亚洲v日本v| 人人精品人人爱| 亚洲风情在线资源站| 亚洲综合久久久久|