Servlet--ServletContext(三)获取web.xml配置参数
【摘要】
Servlet--ServletContext(三)获取web.xml配置参数
一、ServletContext作用之获取配置文件参数
1、获取配置文件实例:
package test07_3context; import java.io.IOException;import jav...
Servlet--ServletContext(三)获取web.xml配置参数
一、ServletContext作用之获取配置文件参数
1、获取配置文件实例:
-
package test07_3context;
-
-
import java.io.IOException;
-
import java.io.PrintWriter;
-
import java.util.Enumeration;
-
-
import javax.servlet.ServletContext;
-
import javax.servlet.ServletException;
-
import javax.servlet.http.HttpServlet;
-
import javax.servlet.http.HttpServletRequest;
-
import javax.servlet.http.HttpServletResponse;
-
-
public class WebServlet extends HttpServlet {
-
-
@Override
-
public void init() throws ServletException {
-
// TODO Auto-generated method stub
-
System.out.println("=========在控制台输出Web配置信息=========");
-
-
//获取ServletContext
-
ServletContext app = getServletContext();
-
//获取web配置的信息
-
System.out.println("①、通过参数名称获取Web配置的信息");
-
String names = app.getInitParameter("name");
-
System.out.println("name的值:"+names);
-
String ages = app.getInitParameter("age");
-
System.out.println("age的值:"+ages);
-
System.out.println("--------------------");
-
//通过遍历方式获取web配置的信息
-
System.out.println("②、通过Enumration遍历方式获取web配置的信息");
-
Enumeration<String> en = app.getInitParameterNames();
-
while(en.hasMoreElements()){
-
String key = en.nextElement();
-
System.out.println("key的值为:"+app.getInitParameter(key));
-
}
-
-
}
-
public void doGet(HttpServletRequest request, HttpServletResponse response)
-
throws ServletException, IOException {
-
-
response.setContentType("text/html;charset=UTF-8");
-
PrintWriter out = response.getWriter();
-
out.println("===========在网页输出Web配置信息==========="+"<br/>");
-
-
out.println("①、通过参数名称获取Web配置的信息"+"<br/>");
-
-
ServletContext ap = getServletContext();
-
String nm = ap.getInitParameter("name");
-
out.print("name的值:"+nm+"<br/>");
-
String ag = ap.getInitParameter("age");
-
out.print("age的值:"+ag+"<br/>");
-
-
out.println("②、通过Enumration遍历方式获取web配置的信息"+"<br/>");
-
Enumeration<String> eu = ap.getInitParameterNames();
-
while(eu.hasMoreElements()){
-
String ke = eu.nextElement();
-
out.print("key的值为:"+ap.getInitParameter(ke)+"<br/>");
-
}
-
-
}
-
-
}
2、配置文件参数
-
<?xml version="1.0" encoding="UTF-8"?>
-
<web-app version="3.0"
-
xmlns="http://java.sun.com/xml/ns/javaee"
-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
-
<!-- servletContext获取web配置的参数如下有name和age两个参数 -->
-
<context-param>
-
<param-name>name</param-name>
-
<param-value>我的家乡在中国</param-value>
-
</context-param>
-
<context-param>
-
<param-name>age</param-name>
-
<param-value>65</param-value>
-
</context-param>
-
<servlet>
-
<description>This is the description of my J2EE component</description>
-
<display-name>This is the display name of my J2EE component</display-name>
-
<servlet-name>WebServlet</servlet-name>
-
<servlet-class>test07_3context.WebServlet</servlet-class>
-
<load-on-startup>1</load-on-startup>
-
</servlet>
-
-
<servlet-mapping>
-
<servlet-name>WebServlet</servlet-name>
-
<url-pattern>/WebServlet</url-pattern>
-
</servlet-mapping>
-
-
</web-app>
3、测试
启动该项目,在浏览器中输入测试地址:http://127.0.0.1:8080/test07_3/WebServlet
测试结果:
A、控制台结果:
B、网页端结果:
文章来源: brucelong.blog.csdn.net,作者:Bruce小鬼,版权归原作者所有,如需转载,请联系作者。
原文链接:brucelong.blog.csdn.net/article/details/76165769
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)