昨天做了一道有关servlet的面试题,题目如下:
-
Servlet
-
ServletContext
-
ServletConfig(正确答案)
-
GenericServlet
我当时选的是第二个ServletContext,后面查阅资料大致弄明白了,这里记录一下,希望对大家有所帮助。
先说说两个参数
-
context-param:元素含有一对参数名和参数值,用作应用的ServletContext上下文初始化参数。参数名在整个Web应用中必须是惟一的。
-
init-param:相应的某个Serlet的初始化参数
然后讲讲ServletContext和ServletConfig对象,下面是我个人的看法
-
ServletConfig对象是当前Servlet的对象,就是在当前Servlet执行init初始化方法之后产生的,是属于当前servlet的对象。
-
ServletContext对象是web应用上下文对象,是web服务器启动之后就产生了的,因此在每个servlet里面都可以通过getServletConfig().getServletContext()拿到ServletContext对象
经过以上分析,相信大家应该大致清楚了吧,这里总结一下
1. init-param参数可以通过ServletConfig对象的getInitParameter()方法获取到
2. context-param参数可以通过ServletContext对象的getInitParameter()方法获取到
对此我还写了个测试程序,代码如下:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name></display-name>
<context-param>
<param-name>context_param</param-name>
<param-value>context_param_value</param-value>
</context-param>
<servlet>
<servlet-name>Test</servlet-name>
<servlet-class>com.servlet.Test</servlet-class>
<init-param>
<param-name>init_param</param-name>
<param-value>init_param_value</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
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("<br/>");
out.println("<br/>");
out.println("getServletConfig().getServletContext().getInitParameter(context_param) : " + context_param1);
out.println("<br/>");
out.println("<br/>");
out.println("getServletContext().getInitParameter(context_param) : " + context_param2);
out.println("<br/>");
out.println("<br/>");
out.println("getServletConfig().getInitParameter(init_param) : " + init_param);
out.println("<br/>");
out.println("<br/>");
out.println("getServletConfig().getServletContext().getInitParameter(init_param) : " + init_param1);
out.println("<br/>");
out.println("<br/>");
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 {
}
}
之后这里是在浏览器显示的结果:
http://blog.csdn.net/yannanying/article/details/44964877