javax.servlet包介紹


 

來源:http://blog.163.com/jianhuren2005@126/blog/static/17158171200782333849784/

 

使用Java技術開發WEB應用程序,深入了解Servlet的機制對應用的開發將有重要的推動作用.而想深入了解Servlet的機制就不得不了解javax.servlet包.

以Servlet2.3規范,我們介紹javax.servlet包中的類:

javax.servlet包中包含了7個接口,3個類和2個異常類,它們分別是:

接口:RequestDispatcher,Servlet,ServletConfig,ServletContext,ServletRequest,ServletResponse和SingleThreadModel

類:GenericServlet,ServletInputStream和ServletOutputStream

異常類:ServletException和UnavailableException

Servlet的生命周期

在Servlet的接口中定義了一個Servlet的生命周期方法,分別是Init,Service和Destroy

演示了Servlet生命周期方法的簡單Servlet:

import javax.servlet.*;
import java.io.IOException;

public class PrimitiveServlet implements Servlet {

  public void init(ServletConfig config) throws ServletException {
    System.out.println("init");
  }

  public void service(ServletRequest request, ServletResponse response)
    throws ServletException, IOException {
    System.out.println("service");
  }  
  public void destroy() {
    System.out.println("destroy");
  }

  public String getServletInfo() {
    return null;
  }
  public ServletConfig getServletConfig() {
    return null;
  }

}

在Servlet中如何獲取ServletConfig對象?

在Servlet的Init方法中,Servlet Container將會傳入一個ServletConfig對象,開發人員可以通過這個對象獲取在web.xml文件中定義的Servlet初始化參數.

下面是一個獲取Servlet初始參數的示例:

import javax.servlet.*; 
import java.util.Enumeration; 
import java.io.IOException; 

public class ConfigDemoServlet implements Servlet {

  public void init(ServletConfig config) throws ServletException {
    Enumeration parameters = config.getInitParameterNames(); 
    while (parameters.hasMoreElements()) {
      String parameter = (String) parameters.nextElement(); 
      System.out.println("Parameter name : " + parameter); 
      System.out.println("Parameter value : " + 
        config.getInitParameter(parameter)); 
    } 
  } 

  public void destroy() {
  } 

  public void service(ServletRequest request, ServletResponse response) 
    throws ServletException, IOException {
  } 

  public String getServletInfo() {
    return null; 
  } 

  public ServletConfig getServletConfig() {
    return null; 
  } 
}
如何獲取ServletContext對象?
可以通過ServletConfig對象的getServletContext方法獲取ServletContext對象
import javax.servlet.*; 
import java.util.Enumeration; 
import java.io.IOException; 

public class ContextDemoServlet implements Servlet {
  ServletConfig servletConfig; 

  public void init(ServletConfig config) throws ServletException {
    servletConfig = config; 
  } 

  public void destroy() {
  } 

  public void service(ServletRequest request, ServletResponse response) 
    throws ServletException, IOException {  
    ServletContext servletContext = servletConfig.getServletContext(); 
    Enumeration attributes = servletContext.getAttributeNames(); 
    while (attributes.hasMoreElements()) {
      String attribute = (String) attributes.nextElement(); 
      System.out.println("Attribute name : " + attribute); 
      System.out.println("Attribute value : " + 
        servletContext.getAttribute(attribute)); 
    } 

    System.out.println("Major version : " + 
servletContext.getMajorVersion()); 
    System.out.println("Minor version : " + 
servletContext.getMinorVersion()); 
    System.out.println("Server info : " + servletContext.getServerInfo()); 
  } 

  public String getServletInfo() {
    return null; 
  } 
  public ServletConfig getServletConfig() {
    return null; 
  } 

}
如何在Servlet之間共享信息?
我們可以通過ServletContext來維護在不同Servlet之間共享的信息.
如何解決Servlet的多Thread問題?
如果Servlet需要讀寫外部資源,我們需要考慮Thread的問題, 我們可以使用聲明性接口SingleThreadModel來避免多Thread之間的資源沖突問題.
但是需要注意的是,如果Servlet僅僅只是讀外部資源的話,我們通常不應該實現這個接口.如果實現這個接口,Servlet在同一時刻只能服務一個用戶請求,后至的用戶請求必須在隊列中等待.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM