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对应的值?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 面试中有这些特征的公司可以pass了
- 下一篇: 产品经理需要向上思考