1.首先通过getServletContext()方法得到ServletContext的一个引用。
ServletContext context = getServletContext();
2.一旦你得到了ServletContext的引用,它将通过getAttribute()方法去获取绑定的名称的对象。绑定名称为USERNAME:.
String userName =(String)context.getAttribute(\
3.检验返回的对象是否正确,如果getAttribute()方法返回null,说明没有对象绑定到名称USERNAME上。如果对象没有找到,它将创建一个,并添加到ServletContext中。绑定名称USERNAME,使用setAttribute()方法
// If there was no attribute USERNAME, then create // one and add it to the ServletContext if ( userName == null ) {
userName = new String(\context.setAttribute(\}
4.通过PrintWriter.println(),传送获取的数据到输出流中。 // Output the current value of the attribute USERNAME out.println(%userName + \
编译你的servlet,并把编译后的class文件放到
在我们即将写的JSP中会和上面的servlet有很多相似之处,但是有两个不同的地方:?ServletContext是在JSP脚本中本访问(这个问题我们将在本章稍后讨论)。 ?如果JSP中没有发现USERNAME属性,它不能添加一个新的。
代码实现的功能在本质上是一样的,只不过在JSP中。你可以查看源代码:
列表 2.3: Context.jsp.
// Try to get the USERNAME attribute from the ServletContext String userName = (String)application.getAttribute(\// If there was no attribute USERNAME, then create // one and add it to the ServletContext if ( userName == null ) {
// Don’t try to add it just, say that you can’t find it out.println(\} else {
out.println(\\} %>
注意:Context.jsp中,我们使用了两个固有对象,application(用于引用ServletContext),out(用于输出流到客户端)。在这章的后面我们将讨论这两个对象。现在复制Context.jsp文件到
Context.jsp没有发现USERNAME属性。如果你输入如下地址: http://localhost:8080/wileyapp/servlet/chapter2.ContextServlet 会输出:The current User is Bob Roberts
运行servlet后,一个对象被绑定到ServletContext 中的属性USERNAME上,查看WEB应用的变化,打开前面的Context.jsp文件,地址为:http://localhost:8080/wileyapp/Context.jsp USERNAME已经不为空。
注意:从ServletContext中删除一个对象的方法:重起JSP/Servlet容器或者使用ServletContext.removeAttribute()方法。
Using Servlets to Retrieve HTTP Data
在这一节,我们将实现servlet如何查找从客户端传送过来的信息。
有三个方法被用来查找:getParameter(), getParameterValues(), 和 getParameterNames()。每个方法的定义如下:
public String ServletRequest.getParameter(String name);
public String[] ServletRequest.getParameterValues(String name); public Enumeration ServletRequest.getParameterNames ();
getParameter()方法返回单个字符串或者null(如果参数不存在),使用这个方法你确保只返回单个值。如果返回多个值,你必须使用getParameterValues()方法,它将返回一个字符串数组或返回null。getParameterNames()返回请求的参数名称的集合或者空集合。
为了了解如何使用这些方法查找数据,让我们来看servlet的Post方法,它是如何查找参数的,并把取得的值返回到客户端。 列表2.4: ParameterServlet.java.
----------------------------------------------------------------- package chapter2; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*;
public class ParameterServlet extends HttpServlet { public void init(ServletConfig config) throws ServletException {
// Always pass the ServletConfig object to the super class
super.init(config); }
// Process the HTTP GET request
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doPost(request, response); }
// Process the HTTP POST request
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType(\PrintWriter out = response.getWriter(); out.println(\out.println(\
out.println(\out.println(\out.println(\
// Get an enumeration of the parameter names
Enumeration parameters = request.getParameterNames(); String param = null;
// Iterate over the paramater names, // getting the parameters values
while ( parameters.hasMoreElements() ) { param = (String)parameters.nextElement(); out.println(param + \request.getParameter(param) + \}
out.println(\out.close(); } }
首先要注意的是servlet通过request的getParameterNames()方法取得所有的参数名。一旦取得参数集合后,它执行while循环来取得参数名和通过getParameter()来取得参数名对应的参数值,并打印。
创建一个HTML页面访问来ParameterServlet,如下:
列表 2.5: Form.html.
-------------------------------------------------------------
Parameter Servlet Form