1. 程式人生 > 實用技巧 >ServletContext共享資料

ServletContext共享資料

今天觀看視訊複習得時候學到了ServletContext相關知識,發現它可以實現servlet之間得資料共享,可以在servlet中傳遞資料,我還記得原來做老師佈置的專案的時候因為這個問題,我把資料都存在session域再在需要的servlet取用,那樣特別麻煩,還是源於自己之前學的不踏實。

所有的servlet都共用一個servletcontext的物件,利用它存取資料:

下面我貼一下我使用其的例子:

<html>
<body>
<h2>Hello World!</h2>
<form action="servlet_01">
<
input type="submit" value="queding"> </form> </body> </html>
index.jsp
import java.io.IOException;
import java.io.Writer;

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 servlet_01 */ public class servlet_01 extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public servlet_01() { super();
// TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Writer writer = response.getWriter(); writer.append("hello servlet"); ServletContext context = this.getServletContext(); context.setAttribute("username", "一個程式猿!"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
servlet-01.java
package frist_test;

import java.io.IOException;
import java.io.Writer;

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 servlet_02
 */
public class servlet_02 extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public servlet_02() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
//        response.setHeader("utf-8", "text/html");
        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
        String name = (String)context.getAttribute("username");
        response.getWriter().append(name);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
servlet-02.java