当前位置: 技术问答>java相关
请问如何取得SERVLET环境下WEB-INF\web.xml中所设置的INIT参数?
来源: 互联网 发布时间:2015-05-07
本文导语: 部分源码: public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{ response.setContentType("text/html;charset=gb2312"); PrintWriter out=response.getWriter(); HTML h=new HTML("SETUP......."); h.add(HTML.HEAD...
部分源码:
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{
response.setContentType("text/html;charset=gb2312");
PrintWriter out=response.getWriter();
HTML h=new HTML("SETUP.......");
h.add(HTML.HEADING,"TEST MYSQL DATABASE",false);
Connection connection=null;
Statement statement=null;
ResultSet resultSet=null;
try{
Class.forName(getInitParameter("Driver")).newInstance();
connection=DriverManager.getConnection(getInitParameter("connectionURL"),getInitParameter("AdminUser"),getInitParameter("PassWord"));
statement=connection.createStatement();
resultSet=statement.executeQuery("SELECT Address,PhoneNum FROM names"
+ " ORDER BY NameID DESC");
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{
response.setContentType("text/html;charset=gb2312");
PrintWriter out=response.getWriter();
HTML h=new HTML("SETUP.......");
h.add(HTML.HEADING,"TEST MYSQL DATABASE",false);
Connection connection=null;
Statement statement=null;
ResultSet resultSet=null;
try{
Class.forName(getInitParameter("Driver")).newInstance();
connection=DriverManager.getConnection(getInitParameter("connectionURL"),getInitParameter("AdminUser"),getInitParameter("PassWord"));
statement=connection.createStatement();
resultSet=statement.executeQuery("SELECT Address,PhoneNum FROM names"
+ " ORDER BY NameID DESC");
|
补充一下:
web.xml中
中的参数用
getServletContext().getInitParameter("name")获得。
中的参数用
getServletConfig().getInitParameter("name")获得
web.xml中
中的参数用
getServletContext().getInitParameter("name")获得。
中的参数用
getServletConfig().getInitParameter("name")获得
|
不一定非要在init()中用,doPost(),doGet()中也可以。
Enumeration enum = getInitParameterNames();
while (enum.hasMoreElements()) {
String paramName = enum.nextElement().toString();
if ("testParam".equalsIgnoreCase(paramName)) {
/*
* do something here
*/
}
}
Enumeration enum = getInitParameterNames();
while (enum.hasMoreElements()) {
String paramName = enum.nextElement().toString();
if ("testParam".equalsIgnoreCase(paramName)) {
/*
* do something here
*/
}
}