有关Servlet初始化参数的获取方法

昨天做了一道有关servlet的面试题,题目如下:

从以下哪一个选项中可以获得Servlet的初始化参数?

  1. Servlet

  2. ServletContext

  3. ServletConfig(正确答案)

  4. GenericServlet

我当时选的是第二个ServletContext,后面查阅资料大致弄明白了,这里记录一下,希望对大家有所帮助。

先说说两个参数

  1. context-param:元素含有一对参数名和参数值,用作应用的ServletContext上下文初始化参数。参数名在整个Web应用中必须是惟一的。

  2. init-param:相应的某个Serlet的初始化参数

然后讲讲ServletContext和ServletConfig对象,下面是我个人的看法

  1. ServletConfig对象是当前Servlet的对象,就是在当前Servlet执行init初始化方法之后产生的,是属于当前servlet的对象。

  2. ServletContext对象是web应用上下文对象,是web服务器启动之后就产生了的,因此在每个servlet里面都可以通过getServletConfig().getServletContext()拿到ServletContext对象

经过以上分析,相信大家应该大致清楚了吧,这里总结一下

1. init-param参数可以通过ServletConfig对象的getInitParameter()方法获取到

2. context-param参数可以通过ServletContext对象的getInitParameter()方法获取到

对此我还写了个测试程序,代码如下:

web.xml



  
  
    context_param
    context_param_value
  
  
Test
com.servlet.Test

    init_param
    init_param_value

  

  
Test
/test
  

  
index.jsp
  


Test.java

package com.servlet;

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;

/**
 * @author phn
 * @date 2015-4-9
 * @TODO
 */
public class Test extends HttpServlet {

    public Test() {
        super();
    }

    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String context_param = getServletConfig().getInitParameter(
                "context_param");
        String context_param1 = getServletConfig().getServletContext()
                .getInitParameter("context_param");
        String context_param2 = getServletContext()
                .getInitParameter("context_param");

        String init_param = getServletConfig().getInitParameter("init_param");
        String init_param1 = getServletConfig().getServletContext().getInitParameter("init_param");
        String init_param2 = getServletContext().getInitParameter("init_param");
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("getServletConfig().getInitParameter(context_param) : " + context_param);
        out.println("
"); out.println("
"); out.println("getServletConfig().getServletContext().getInitParameter(context_param) : " + context_param1); out.println("
"); out.println("
"); out.println("getServletContext().getInitParameter(context_param) : " + context_param2); out.println("
"); out.println("
"); out.println("getServletConfig().getInitParameter(init_param) : " + init_param); out.println("
"); out.println("
"); out.println("getServletConfig().getServletContext().getInitParameter(init_param) : " + init_param1); out.println("
"); out.println("
"); out.println("getServletContext().getInitParameter(init_param) : " + init_param2); out.flush(); out.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); doGet(request, response); } public void init() throws ServletException { } }

之后这里是在浏览器显示的结果:

你可能感兴趣的:(Web开发篇,面试笔试篇)