欢迎光临
我们一直在努力

Resin中JSP的范例-JSP教程,Jsp/Servlet

建站超值云服务器,限时71元/月

by scott ferguson
blueski编译

目录如下:
1 引论
2 范例的框架: hello, world
3 servlet 评论
4 展示留言本
5 留言本的模式
6 作为应用属性的留言本
7 留言本的逻辑
8 结论

1 引论

jsp的强大优势在于把一种应用的商务逻辑和它的介绍分离开来。用 smalltalk的面向对象的术语来说, jsp鼓励mvc(model-view-controller)的web应用。jsp的classes 或 beans 是模型, jsp 是这个视图, 而servlet是控制器。

这个例子是一个简单的留言本,包括用户登录和留言。它被作为resin平台的示范:
–执行角色
–模型 a 留言本
–用于新用户的login.jsp
–用于已注册用户的add.jsp
–控制器 guestjsp, 一个用来管理状态的servlet

2 样板的框架: hello, world

guestjsp servlet的框架把 "hello, world" 这个字符串传给login.jsp页面。这个框架为留言本设立结构。具体细节将在下面补充。

这个例子被编译后可以浏览到:

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

你可以看到页面上有这样的显示: hello, world

jsp模板是以servlet的处理开始然后把处理结果传给jsp页进行格式化。

以下使用了一个servlet2.1 servletcontext的特性 getrequestdispatcher()。
请求的调度器在服务器上让servlets直接向前传送并包括了任何可能的子请求。对ssi包含来说这是一个更灵活的取代做法。
在servlet文件中请求的调度器可以包含任何页面,servlet,或jsp的结果。 guestjsp将使用dispatcher.forward()来将控制传给jsp页进行格式化。

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

servlet和jsp页使用httprequest对象中的属性进行通信。skeleton在"message"属性中保存了"hello, world"。
当login.jsp启动时,它将捕捉到该字符串并将其打印出来。

由于resin的javascript能够读取扩充的bean模型,它可以将request.getattribute("message")转换成为
javascript的对应物 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>

3 servlet的复习

对于来自于asp或cgi背景并转向jsp的人来说,
servlets代替cgi脚本体现了java在动态类加载方面的优势。servlet就是一个java类,
它对servlet或httpservlet进行了扩展并放置到适当的路径中。resin将自动加载servlet并执行它。

url /servlet/classname将request提交给servlet请求器。请求器会从doc/web-inf/classes自动加载java类的类名
并试图执行servlet的service方法。

resin将定期检查类文件以判断是否被修改过。如果被修改过,则将用新的servlet取代旧的。

4 显示留言本

在基本框架已经运行后, 下一步是创建model。

5 留言本模型

留言本是很直接的,这里知识包含了一下api。它遵从bean模型以简化javascript。
同样的api可以工作于hashmap, 基于文件,以及数据库应用。

jsp文件只能存取public方法。所以jsp文件无法创建一个新的留言本或者增加一个新用户。
这是guestjsp servlet的责任。

jsp.guest.java api package jsp;

public class guest {
guest();
public string getname();
public string getcomment();
}

resin的javascript能读取bean模型。所以使用javascript的jsp页面可以存取getname()和getcomment()
作为属性。例如,你可以简化使用guest.name和guest.comment。

jsp.guestbook.java api package jsp;

public class guestbook {
guestbook();
void addguest(string name, string comment);
public iterator iterator();
}

resin的javascript同样可以读取iterator()调用,所以你可以使用javascript用于 … 任何一个来取得用户:

for (var guest in guestbook) {

}

guestbook作为application属性
为了使得例子保持简单,guestjsp在application (servletcontext)中存取guestbook。作为例子,
在application中保存数据是可以接受的,但对于完全成熟的应用,最好仅使用application将数据放到其它地方。

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 doesnt 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
// doesnt need to worry about threading
synchronized (app) {
disp.forward(req, res);
}

jsp文件本身是简单的。它从application获取留言本并在表中显示内容。通常,
application对象需要同步,因为一些客户端可能同时浏同一页面。
guestjsp在jsp文件被调用之前小心处理了同步情况。

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);
}
%>
</table>

</body>

hogwarts guest book
name comment
harry potter griffindor rules
draco malfoy slytherin rules

6 留言本的规则(logic)–作为应用属性的留言本

留言本的规则是很简单的。如果用户没有登录,他会看到一个提示和登录表。
登录后他会看到提示并在一个表中加入留言。 login.jsp给出了登录的页面,add.jsp给出了
增加流言的页面。

guestjsp在session变量中保存了规则信息。

执行login来登录或 add来增加留言。 其中
name: 用户名
password: 口令
comment:留言

7 留言本规则 …

// 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和add.jsp仅加上了不同forms在前一个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);
}
%>
</table>
<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>
</table>
</form>
</body>

8 结论

resin示例演示了扩充留言本的一些方法,包括加入一些智能的东西用于form处理。然而,由于forms取得更多的只能,即使是jsp模板也变得复杂化了。
有一个结论:xtp模板。

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » Resin中JSP的范例-JSP教程,Jsp/Servlet
分享到: 更多 (0)

相关推荐

  • 暂无文章