servlet 配置資訊及初始化引數
Servlet取得初始化配置資訊:
通過config物件可以讀取web.xml中的配置的初始化引數,此物件實際上是ServletConfig藉口的例項。可以通過init()方法找到ServletConfig介面例項。
讀取初始化配置資訊------InitParamServlet.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;
- publicclass InitParamServlet extends HttpServlet {
- String initParam=null;
- publicvoid init(ServletConfig config) throws ServletException {
-
this
- }
- publicvoid doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- System.out.println("初始化引數:"+this.initParam);
- }
-
publicvoid doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- this.doGet(request, response);
- }
- }
在web.xml中配置初始化資訊
- <?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
- 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;
- publicclass HttpSessionservlet extends HttpServlet {
- publicvoid 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"));
- }
- publicvoid doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- this.doGet(request, response);
- }
- }
配置web.xml檔案
- <?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
- 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;
- publicclass ServletContext extends HttpServlet {
- publicvoid doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- javax.servlet.ServletContext app =super.getServletContext();
- System.out.println("真實路徑:"+app.getRealPath("/"));
- }
- publicvoid doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- this.doGet(request, response);
- }
- }
配置web.xml檔案
- <?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例項後,將虛擬目錄所對應的真實路徑輸出。