欢迎使用 Web 应用程序的 NetBeans IDE 6.0 和 6.1 教程。在本教程中,您将在学习 NetBeans IDE 中的一些工具和技术的同时构建一个相当复杂的 Web 应用程序,还将体验从创建到部署这样一个典型的 Web 开发周期。本教程的高级部分内容从创建 Servlet 和 JSP 页开始,向您说明了如何使用 JavaServer Pages 标准标记库 (JavaServer Pages Standard Tag Library, JSTL) 来获取动态数据以及将此应用程序中使用的 JSP 页进行国际化。在本教程的结尾处将向您介绍 IDE 的 HTTP 监视器,此监视器可以帮助您在部署 Web 应用程序后对其进行分析。
注意:如果这是您第一次使用 NetBeans IDE 开发 Web 应用程序,则建议您在继续之前先学习开发 Web 应用程序简介。
要利用 NetBeans IDE 的 Java EE 5 功能,请使用完全符合 Java EE 5 规范的应用服务器,例如 GlassFish V2 UR2 应用服务器。如果使用的是其他服务器,请查阅发行说明和常见问题解答,了解已知问题和解决方法。有关支持的服务器和 Java EE 平台的详细信息,请参见发行说明。
复合视图。一种设计模式,用于显示 JSP 页中的信息。此设计模式创建一个基于组件视图的聚合视图。组件视图可能包括该页的动态部分和模块化部分。当您创建一个基于许多子视图的视图时,复合视图设计模式适用于 Web 应用程序设计。复杂网页通常包含派生自各种资源的内容。页面的布局是独立于它的子视图的内容来管理的。例如,一个视图可能有“导航”、“搜索”、“功能说明”和“标题”等子视图。
创建复合视图时,可以包括静态内容和动态内容。静态内容可能包含一个 HTML 文件。动态内容是 JSP 页的一个片段。可以在 JSP 转换时或运行时确定具体内容。
前端控制器。一种组件,负责路由传入的请求,并强制在 Web 应用程序中导航。有关使用前端控制器设计模式的详细信息,请参见 J2EE 模式目录。
JSP 页。在 Web 应用程序中使用的文件,用于向最终用户显示信息,并使来自最终用户的数据流回到服务器。要使 JSP 页能够在 IDE 中执行,必须将它置于 Web 应用程序内。
Servlet。在 Servlet 容器中执行的 Java 类,通常在 Web 服务器中运行。Servlet 用于执行以下操作:
生成动态内容。
扩展 Web 服务器以及支持 Web 的应用服务器的功能。
使用请求-响应范例与 Web 客户端交互(通常是浏览器应用程序,如 Netscape 或 Internet Explorer)。
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getRequestURI();
id = id.substring(request.getContextPath().length());
getServletContext().log
("Midnight Cookies FrontController received a request for " + id);
// The page variable holds the path to the page that will be included
// in the content area of the template page /WEB-INF/docs/main.jsp.
// It is passed to the template in a request attribute.
String page;
// If no request is received or if the request received is
// for the root, serve /WEB-INF/docs/index.jsp
if (id == null || id.trim().equals("") || id.equals("/")){
page = "/WEB-INF/docs/index.jsp";
}
// If a request received is for a file that does not end in
// .jsp or .html, there was an error. Include the
// error page (nonesuch.jsp) and set the nonesuch attribute with
// the path requested.
else if (!id.endsWith(".jsp") && !id.endsWith(".html")) {
page = "/WEB-INF/docs/nonesuch.jsp";
request.setAttribute("nonesuch", id);
}
else {
page = "/WEB-INF".concat(id);
if (id.equals("/docs/cookies/CookieMake.jsp")) {
Cookie cookie = createCookie(request);
if (cookie == null) {
page = "/WEB-INF/docs/cookies/CookieCutter.jsp";
}
else {
response.addCookie(cookie);
request.setAttribute("cookie", cookie);
}
}
}
request.setAttribute("page", page);
try {
request.getRequestDispatcher("/WEB-INF/docs/main.jsp").forward(request, response);
}
catch(Throwable t) {
getServletContext().log(t.getMessage());
}
}
<%@page contentType="text/html;charset=UTF-8"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@taglib prefix="midnight" uri="/WEB-INF/tlds/midnight.tld"%>
<fmt:setBundle basename="com.midnightcookies.Midnight" var="bundle"
scope="page"/>
<h3><fmt:message key="provide_input" bundle="${bundle}"/></h3>
<form method="POST" action="Output.jsp">
<table>
<tr>
<td><fmt:message key="type_input" bundle="${bundle}"/>:</td>
<%-- The value of the Input field will be placed in a
request parameter named "input" and it will be
passed to Output.jsp --%>
<td><input type="text" size="20" name="input" value=""></td>
</tr>
<tr>
<td><fmt:message key="submit" bundle="${bundle}"
var="buttonLabel" scope="page"/>
<midnight:button/>
</td>
<td></td>
</tr>
</table>
</form>
<%@page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<fmt:setBundle basename="com.midnightcookies.Midnight" var="bundle" scope="page"/>
<h3><fmt:message key="display_input" bundle="${bundle}"/></h3>
<fmt:message key="datareceived" bundle="${bundle}"/>:
<!--The following line gets the value of the request parameter named
"input". This is a demo application. For security reasons, one should
never echo user input without parsing first. -->
<c:out value="${param.input}"/>
在 cookies 文件夹中,使用与创建其他 JSP 文件相同的方法来创建 Tray.jsp 文件。确保将此文件放入 cookies 文件夹。
在源代码编辑器中,将 Tray.jsp 文件中的内容替换为以下代码:
<%@page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<fmt:setBundle basename="com.midnightcookies.Midnight" var="bundle" scope="page"/>
<h3><fmt:message key="cookies" bundle="${bundle}"/></h3>
<table border="1">
<tr>
<th halign="center">#</th>
<th align="left">
<fmt:message key="name" bundle="${bundle}"/>
</th>
<th align="left">
<fmt:message key="value" bundle="${bundle}"/>
</th>
<tr>
<%-- We have to use expression to get at an array.
If we use ${cookie} then we get a map and
the entries are not automatically cast --%>
<% request.setAttribute("cookies", request.getCookies()); %>
<c:set var="i" value="0"/>
<c:forEach var="ck" items="${cookies}">
<c:set var="i" value="${i+1}"/>
<tr>
<td><c:out value="${i}"/></td>
<td><c:out value="${ck.name}"/></td>
<td><c:out value="${ck.value}"/></td>
</tr>
</c:forEach>
</table>
按 Ctrl-S 组合键保存更改。
包装
完成上述开发阶段之后,“项目”窗口和“文件”窗口中的视图应如下所示:
注意:上面演示的应用程序将部署到 Tomcat Web Server。如果已注册并选择了不同的目标服务器,您将不会拥有 META-INF/context.xml 文件。例如,如果您的服务器是 GlassFish,您将拥有 WEB-INF/sun-web.xml 文件作为部署描述符。
一种设计模式,用于显示 JSP 页中的信息。此设计模式创建一个基于组件视图的聚合视图。组件视图可能包括该页的动态部分和模块化部分。当您创建一个基于许多子视图的视图时,复合视图设计模式适用于 Web 应用程序设计。复杂网页通常包含派生自各种资源的内容。页面的布局是独立于它的子视图的内容来管理的。例如,一个视图可能有“导航”、“搜索”、“功能说明”和“标题”等子视图。
创建复合视图时,可以包括静态内容和动态内容。静态内容可能包含一个 HTML 文件。动态内容是 JSP 页的一个片段。可以在 JSP 转换时或运行时确定具体内容。
前端控制器
前端控制器负责路由传入的请求,并强制在 Web 应用程序中导航。有关使用前端控制器设计模式的详细信息,请参见 J2EE 模式目录。
JSP 页
在 Web 应用程序中使用的页面,用于向最终用户显示信息,并使来自最终用户的数据流回服务器。要使 JSP 页能够在 IDE 中执行,必须将它置于 Web 应用程序内。
Servlet
Servlet 是在 Servlet 容器中执行的 Java 类,通常在 Web 服务器中运行。Servlet 用于执行以下操作:
生成动态内容。
扩展 Web 服务器以及支持 Web 的应用服务器的功能。
使用请求-响应范例与 Web 客户端交互(通常是浏览器应用程序,如 Firefox 或 Internet Explorer)