第五章 jsp技术
5.1 jsp简介
jsp(javaserver pages)是一种基于java的脚本技术。在jsp 的众多优点之中,其中之一是它能将 html 编码从 web 页面的业务逻辑中有效地分离出来。用 jsp 访问可重用的组件,如 servlet、javabean 和基于 java 的 web 应用程序。jsp 还支持在 web 页面中直接嵌入 java 代码。可用两种方法访问 jsp 文件:浏览器发送 jsp 文件请求、发送至 servlet 的请求。
1. jsp 文件访问 bean 或其它能将生成的动态内容发送到浏览器的组件。图5-1说明了该 jsp 访问模型。当 web 服务器接收到一个 jsp 文件请求时,服务器将请求发送至 websphere应用服务器。websphere应用服务器 对 jsp 文件进行语法分析并生成 java 源文件(被编译和执行为 servlet)。java 源文件的生成和编译仅在初次调用 servlet 时发生,除非已经更新了原始的 jsp 文件。在这种情况下,websphere应用服务器 将检测所做的更新,并在执行它之前重新生成和编译 servlet。
图5-1:浏览器发送 jsp 文件请求
2. 发送至 servlet 的请求生成动态内容,并调用 jsp 文件将内容发送到浏览器。图5-2说明了该访问模型。该访问模型使得将内容生成从内容显示中分离出来更为方便。websphere应用服务器 支持 httpservicerequest 对象和 httpserviceresponse 对象的一套新方法。这些方法允许调用的 servlet 将一个对象放入(通常是一个 bean)请求对象中,并将该请求传递到另一个页面(通常是一个 jsp 文件)以供显示。调用的页面从请求对象中检索 bean, 并用 jsp 来生成客户机端的 html。
图5-2:发送至 servlet 的请求
5.2 jsp示例
浏览器通过一个web 页面中的html表单请求一个servlet(populatebeanservlet),该servlet创建一个名为 databean 的databean 实例,并调用 jsp 文件将内容发送到浏览器。servlet 示例和 jsp 文件示例说明了启用内容分离的 jsp 访问模型。
a. servlet 是由下列web 页面中的 html 表单来调用的。
<html>
<body>
<h1>运行 populatebeanservlet</h1>
<p>您是否希望运行 populatebeanservlet?
<form action="/servlet/populatebeanservlet" method="get">
<input type="submit" value="yes">
<input type="submit" value="no">
</form>
</body>
</html>
b. 被请求的servlet为populatebeanservlet, 其源代码如下:
/******************************************************************
*servlet 示例:populatebeanservlet.java
*这个servlet创建一个名为 databean 的databean 实例,设置databean的若干个属性,
*将databean放置在当前“请求”对象中,
*调用 jsp 文件(displaydata.jsp)来格式化并显示databean的数据
********************************************************************/
import java.io.*;
import java.beans.beans;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.ejb.createexception;
public class populatebeanservlet extends httpservlet {
public void service(httpservletrequest req, httpservletresponse res)
throws servletexception, ioexception {
try {
databean = (databean) beans.instantiate(this.getclass().getclassloader(), "databean");
}
catch (exception ex) {
throw new servletexception("cant create bean of class databean: "
metadata.setsql(getsqlstring());
}
// set some bean properties (content generation)
databean.setprop1("value1");
databean.setprop2("value2");
databean.setprop3("value3");
// to send the bean to a jsp file for content formatting and display
// 1) 将databean放置在当前“请求”对象中,
((com.sun.server.http.httpservicerequest) req).setattribute("databean", databean);
// 2) 使用callpage 方法调用jsp文件,文件名为displaydata.jsp,并把请求对象传递给jsp。
((com.sun.server.http.httpserviceresponse) res).callpage("/displaydata.jsp", req);
} //end of service mehtod
} /* end of class populatebeanservlet */
c. 被调用的jsp文件为displaydata.jsp,其内容如下:
<!– 该 jsp 文件获得在请求对象中传递的 databean,并显示该 bean 的属性。 –>
<html>
<head>
<title>bean data display</title>
</head>
<!– get the bean using the bean tag –>
<bean name="databean" type="databean" introspect="no" create="no" scope="request">
</bean>
<body>
<!– there are three ways to access bean properties –>
<!– using a jsp scriptlet –>
<% out.println("the value of bean property 1 is " + databeans.getprop1());
%>
<!– using a jsp expression –>
<p>the value of bean property 2 is
<%= databean.getprop2() %> </p>
<!–using the insert tag –>
<p>the value of bean property 3 is
<insert bean=databean property=prop3 default="no property value" >
</insert></p>
</body>
</html>
