Servlet取得初始化配置信息及內置對象 – JAVA編程語言程序開發技術文章

Servlet取得初始化配置信息:

 

通過config對象可以讀取web.xml中的配置的初始化參數,此對象實際上是ServletConfig借口的實例。可以通過init()方法找到ServletConfig接口實例。

 

讀取初始化配置信息——InitParamServlet.java

[java]
package com.ls.servlet_01; 
 
import java.io.IOException; 
import java.io.PrintWriter; 
 
import javax.servlet.ServletConfig; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
public class InitParamServlet extends HttpServlet { 
 
    String initParam=null; 
    public void init(ServletConfig config) throws ServletException { 
        this.initParam=config.getInitParameter("ref"); 
    } 
    public void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException { 
        System.out.println("初始化參數:"+this.initParam); 
         
    } 
    public void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException { 
        this.doGet(request, response); 
    } 

在web.xml中配置初始化信息

[java] 
<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5"  
    xmlns="http://java.sun.com/xml/ns/javaee"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
<servlet> 
    <servlet-name>InitParamServlet</servlet-name> 
    <servlet-class>com.ls.servletd_01.InitParamServlet</servlet-class> 
    <init-param> 
        <param-name>ref</param-name> 
        <param-value>liusheng</param-value> 
    </init-param> 
 </servlet> 
 <servlet-mapping> 
    <servlet-name>InitParamServlet</servlet-name> 
    <url-pattern>/InitParamServlet</url-pattern> 
  </servlet-mapping> 
  <welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
  </welcome-file-list> 
</web-app>   
 

程序運行結果:在Tomcat後臺打印

                   初始化參數:liusheng

用<init-param>直接配置初始化

<param-name>表示參數名稱

<param-value>表示參數的內容

 

 

註意:

    在Servlet中初始化方法一共有init()和init(ServletConfig config)兩個,如果兩個初始化方法同時出現,則調用的是init(ServletConfig config)方法。

 

Servlet取得其他內置對象:

  

    1、取得HttpSession實例

在servlet程序中要想取得一個session對象,則可以通過HttpServletRequest接口完成,有以下操作方法:

public HttpSession getSession()      返回當前的session

public HttpSession  getSession(boolean  create)     返回當前的session,如果沒有則創建一個新的session對象返回

 

取得HttpSession對象—HttpSessionservlet.java

[java] 
package com.ls.servlet_02; 
 
import java.io.IOException; 
import java.io.PrintWriter; 
 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.http.HttpSession; 
 
public class HttpSessionservlet extends HttpServlet { 
 
    public void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException { 
        HttpSession ses = request.getSession(); 
        System.out.println("SESSION–>" + ses.getId()); 
        ses.setAttribute("username", "劉勝"); 
        System.out.println("username屬性內容是:" + ses.getAttribute("username")); 
    } 
 
    public void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException { 
        this.doGet(request, response); 
 
    } 
 

配置web.xml文件

[java] 
<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5"  
    xmlns="http://java.sun.com/xml/ns/javaee"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
 <servlet> 
    <servlet-name>HttpSession</servlet-name> 
    <servlet-class>com.ls.servlet_02.HttpSessionservlet</servlet-class> 
  </servlet> 
 <servlet-mapping> 
    <servlet-name>HttpSession</servlet-name> 
    <url-pattern>/servlet_HttpSession</url-pattern> 
  </servlet-mapping> 
  <welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
  </welcome-file-list> 
</web-app> 

程序運行結果: Tomcat後臺顯示

          

 

 本程序通過getSession()方法取得瞭一個HttpSession對象後,輸出瞭Session Id以及屬性的設置及取得操作。

 

      2、取得ServletContext實例

application內置對象是ServletContext 接口的實例,表示的是Servlet上下文。如果要在一個servlet中使用此現象,直接通過GenericServlet類提供的方法即可:

public ServletContext getServletContext()      取得ServletContext對象

 

取得application對象—–ServletContext.java

[java]
package com.ls.servlet_03; 
 
import java.io.IOException; 
import java.io.PrintWriter; 
 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
public class ServletContext extends HttpServlet { 
 
    public void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException { 
        javax.servlet.ServletContext app =super.getServletContext(); 
        System.out.println("真實路徑:"+app.getRealPath("/")); 
    } 
 
    public void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException { 
 
        this.doGet(request, response); 
    } 
 
     

配置web.xml文件

[java] 
<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5"  
    xmlns="http://java.sun.com/xml/ns/javaee"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
 <servlet> 
    <servlet-name>ServletContext</servlet-name> 
    <servlet-class>com.ls.servlet_03.ServletContext</servlet-class> 
  </servlet> 
<servlet-mapping> 
    <servlet-name>ServletContext</servlet-name> 
    <url-pattern>/servlet_ServletContext</url-pattern> 
  </servlet-mapping> 
  <welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
  </welcome-file-list> 
</web-app> 

程序運行結果:  Tomcat後臺輸出

          

 

    本程序通過getServletContext()方法取得ServletContext實例後,將虛擬目錄所對應的真實路徑輸出。

 

 

 

發佈留言