日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Servlet中如何获取param-name对应的值?

發布時間:2025/3/15 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Servlet中如何获取param-name对应的值? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一個web.xml,配置一個servlet如下

<servlet>
<servlet-name>BeerParamTests</servlet-name>
<servlet-class>TestInitParams</servlet-class>
<init-param>
<param-name>adminEmail</param-name>
<param-value>likewecare@wickedlysmart.com</param-value>
</init-param>
</servlet>

Servlet中是如何獲取到這個參數adminEmail對應的值likewecare@wickedlysmart.com呢?

《Head First Servlet and JSP》上面有這樣一段話

When the Container
initializes a servlet,
it makes a unique
ServletConfig for the
servlet.
The Container “reads”
the servlet init
parameters from the
DD and gives them to
the ServletConfig, then
passes the ServletConfig
to the servlet’s init()
method.

大概意思是說,當容器比如tomcat實例化一個servlet的時候,會為這個servlet造一個獨一無二的ServletConfig,容器從DD也就是web.xml文件中讀取servlet的<init-param>的name和value,并且將其傳遞到ServletConfig中,然后再將這個ServletConfig傳遞到servlet的init()方法中.

將ServletConfig傳遞到servlet的init()方法中是什么意思呢?

其實指的就是就是指傳遞到?GenericServlet的init(ServletConfig config)方法中,

我們先看下?GenericServlet這個類

public abstract class GenericServletimplements Servlet, ServletConfig, Serializable {private static final long serialVersionUID = 1L;private transient ServletConfig config;public void destroy(){}public String getInitParameter(String name){return getServletConfig().getInitParameter(name);}public Enumeration<String> getInitParameterNames(){return getServletConfig().getInitParameterNames();}public ServletConfig getServletConfig(){return this.config;}public ServletContext getServletContext(){return getServletConfig().getServletContext();}public String getServletInfo(){return "";}public void init(ServletConfig config)throws javax.servlet.ServletException{this.config = config;init();}public void init()throws javax.servlet.ServletException{}public void log(String msg){getServletContext().log(getServletName() + ": " + msg);}public void log(String message, Throwable t){getServletContext().log(getServletName() + ": " + message, t);}public abstract void service(ServletRequest paramServletRequest, ServletResponse paramServletResponse)throws javax.servlet.ServletException, IOException;public String getServletName(){return this.config.getServletName();} } 所以GenericServlet通過init(ServletConfig config)獲取到了web.xml中的ServletConfig
又因為public abstract class HttpServlet extends GenericServlet(HttpServlet繼承于GenericServlet)

我們可以在doGet或者doPost方法中通過

this.getServletConfig().getInitParameter("emailAdress")獲取


如果我們想在init()方法中獲取參數,就應該重寫init()方法,而不是init(arg)方法

當然也可以重寫init(arg)方法

不過得保證執行了父類的super.init(arg)比如

@Override
?? ?public void init(ServletConfig config) throws ServletException {
?? ??? ?System.out.println("你進入了init(arg)方法"+config.getInitParameter("emailAdress")+"---");
?? ??? ?super.init(config);//注釋掉這一句,ServletConfig將為null
?? ?}

這樣一來,在ServletConfig對象才不為null,



附送一個測試的Servlet類

package servlet;import java.io.IOException;import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /*** 測試InitParameter* @author ken**/ public class ServletInitParameter extends HttpServlet {/*** */private static final long serialVersionUID = 7094673076240375858L;@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {doPost(req, resp);}@Overridepublic void init() throws ServletException {// TODO Auto-generated method stubsuper.init();}@Overridepublic void init(ServletConfig config) throws ServletException {System.out.println("你進入了init(arg)方法"+config.getInitParameter("emailAdress")+"---");super.init(config);//注釋掉這一句,下面的getServletConfig()將返回是null}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {System.out.println("aaaa"+this.getServletConfig().getInitParameter("emailAdress"));}}



轉載于:https://my.oschina.net/liangzhenghui/blog/183791

總結

以上是生活随笔為你收集整理的Servlet中如何获取param-name对应的值?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。