Servlet屬性設置
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應用程序。