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應用程式。