Servlet中的属性是可以从以下范围之一设置,获取或删除的对象:
- 请求范围
- 会话范围
- 应用范围
Servlet程序员可以使用属性将信息从一个servlet传递给另一个servlet。它就像将对象从一个类传递给另一个类一样,以便我们可以一次又一次地重用同一个对象。
ServletRequest,HttpSession和ServletContext接口的属性特定方法
Servlet中有以下4
种属性方法,它们具体如下:
序号 | 方法 | 描述 |
---|---|---|
1 | public void setAttribute(String name,Object object) |
在应用程序范围内设置给定的对象。 |
2 | public Object getAttribute(String name) |
返回指定名称的属性。 |
3 | public Enumeration getInitParameterNames() |
返回上下文的初始化参数的名称,转为String 对象的枚举。 |
4 | public void removeAttribute(String name) |
从servlet上下文中删除具有给定名称的属性。 |
ServletContext的示例设置和获取属性
在这个例子中,在应用程序范围内设置属性,并从另一个servlet获取该值。
打开Eclipse创建一个动态Web项目:ServletAttribute ,其完整的项目结构如下所示 -
以下是几个主要代码文件 -
文件: ServletAttr.java -
package com.yiibai;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ServletAttr
*/
public class ServletAttr extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
try {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
ServletContext context = getServletContext();
context.setAttribute("company", "Lenovo");
out.println("Welcome to first servlet");
out.println("在第二个Servlet<a href='servlet2'>查看属性值</a>");
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
文件: ServletAttr2.java -
package com.yiibai;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ServletAttr
*/
public class ServletAttr2 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
ServletContext context = getServletContext();
String n = (String) context.getAttribute("company");
out.println("Welcome to " + n);
out.close();
}
}
文件: web.xml -
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>ServletAttribute</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>ServletAttr1</servlet-name>
<servlet-class>com.yiibai.ServletAttr</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletAttr1</servlet-name>
<url-pattern>/servlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ServletAttr2</servlet-name>
<servlet-class>com.yiibai.ServletAttr2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletAttr2</servlet-name>
<url-pattern>/servlet2</url-pattern>
</servlet-mapping>
</web-app>
在编写完成上面的代码后,部署此Web应用程序,打开浏览器访问URL:http://localhost:8080/ServletAttribute/servlet ,如果程序没有错误,应该会看到以下结果 -
点击查看属性值链接,应该会看到以下结果 -
ServletConfig和ServletContext的区别
servletconfig
对象引用单个servlet,而servletcontext
对象引用整个Web应用程序。