java 在 Servlet 中设置初始化参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13818244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting Initialization Parameters in a Servlet
提问by
I have to set initialization parameters based on environment to a servlet, NOT via web.xml but via code, but my servlet version is not 3.0 , so I cannot use this http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html#setInitParameter%28java.lang.String,%20java.lang.String%29
我必须根据环境将初始化参数设置为 servlet,不是通过 web.xml 而是通过代码,但我的 servlet 版本不是 3.0 ,所以我不能使用这个 http://docs.oracle.com/javaee/6/api /javax/servlet/ServletContext.html#setInitParameter%28java.lang.String,%20java.lang.String%29
I dont have access to the servlet code so I am writing a new servlet that extends it and I want to add initialization parameters via java code ..Any suggestions?
我无权访问 servlet 代码,所以我正在编写一个扩展它的新 servlet,我想通过 java 代码添加初始化参数..有什么建议吗?
<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>abc</servlet-class>
<init-param>
<param-name>abc</param-name>
<param-value>localhost:2001</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Is there a way to do the above thing(adding init-params) by extending servlet abc and setting attributes to the servlet config (overriding init())?
有没有办法通过扩展 servlet abc 并将属性设置为 servlet 配置(覆盖 init())来完成上述操作(添加 init-params)?
回答by
I overrode the method getInitParameter of GenericServlet and I was able to solve my problem..
我覆盖了 GenericServlet 的 getInitParameter 方法,我能够解决我的问题..
@Override
public String getInitParameter(String name) {
//Get initparams here
return "MyInitParams";
}
回答by Saurabh
See if following code helps you
看看下面的代码是否对你有帮助
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class HelloWorld
*/
public class HelloWorld extends HttpServlet {
private static final long serialVersionUID = 1L;
ServletContext context;
/**
* @see HttpServlet#HttpServlet()
*/
public HelloWorld() {
super();
// TODO Auto-generated constructor stub
}
public void init(ServletConfig config) throws ServletException {
// Do required initialization
Properties prop = new Properties();
try {
prop.load(getServletContext().getResourceAsStream(
"/WEB-INF/properties/sample.properties"));
context.setAttribute("abc", prop.getProperty("abc"));
} catch (Exception e) {
// TODO: handle exception
System.out.println(e);
}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}