Servlet获取web.xml中的参数方法
2012-03-14 14:34
453 查看
<context-param>
<param-name>count</param-name>
<param-value>1200</param-value>
</context-param>
<servlet>
<description>loginServlet</description>
<display-name>loginServlet</display-name>
<servlet-name>loginServlet</servlet-name>
<servlet-class>loginServlet</servlet-class>
<init-param>
<description>系统初始化数</description>
<param-name>num</param-name>
<param-value>100</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
获取参数的方法以及异同
String username=request.getParameter("username");
response.setCharacterEncoding("gb2312");
System.out.println("getServletContext():"+getServletContext());
ServletContext context=getServletConfig().getServletContext();
String num=context.getInitParameter("num");//
<init-param> 获取不到值
String count=context.getInitParameter("count");//<context-param> 获取到值
System.out.println("num:"+num);//null
System.out.println("count:"+count);//1200
System.out.println("num:"+getServletConfig().getInitParameter("num"));//100获取到值
System.out.println("count:"+getServletConfig().getInitParameter("count"));//null获取不到值
System.out.println("request.getSession().getServletContext():"+request.getSession().getServletContext());
request.setAttribute("username", username);
PrintWriter out=response.getWriter();
out.println("获取的用户名是:"+username);
<param-name>count</param-name>
<param-value>1200</param-value>
</context-param>
<servlet>
<description>loginServlet</description>
<display-name>loginServlet</display-name>
<servlet-name>loginServlet</servlet-name>
<servlet-class>loginServlet</servlet-class>
<init-param>
<description>系统初始化数</description>
<param-name>num</param-name>
<param-value>100</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
获取参数的方法以及异同
String username=request.getParameter("username");
response.setCharacterEncoding("gb2312");
System.out.println("getServletContext():"+getServletContext());
ServletContext context=getServletConfig().getServletContext();
String num=context.getInitParameter("num");//
<init-param> 获取不到值
String count=context.getInitParameter("count");//<context-param> 获取到值
System.out.println("num:"+num);//null
System.out.println("count:"+count);//1200
System.out.println("num:"+getServletConfig().getInitParameter("num"));//100获取到值
System.out.println("count:"+getServletConfig().getInitParameter("count"));//null获取不到值
System.out.println("request.getSession().getServletContext():"+request.getSession().getServletContext());
request.setAttribute("username", username);
PrintWriter out=response.getWriter();
out.println("获取的用户名是:"+username);
相关文章推荐
- Servlet(3)ServletConfig接口的介绍(获取web.xml中某个Servlet中的初始配置参数)
- 如何通过Servlet获取定义在web.xml中的初始化参数
- 20160321servlet学习笔记获取web.xml配置参数
- 解析web.xml中在Servlet中获取context-param和init-param内的参数
- servlet获取并存储web.xml中context-param参数
- servlet都有一个servletConfig对象;四个config对象可以调用的方法;ServletContext对象之获取web项目信息;设置全局初始化参数的配置
- web.xml中在Servlet中获取context-param和init-param内的参数
- Java EE使用Servlet获取web.xml中的参数
- 获取web.xml中参数的几种方法
- web.xml中在Servlet中获取context-param和init-param内的参数
- web.xml中在Servlet中获取context-param和init-param内的参数
- servlet读取web.xml中参数和页面转向方法
- web.xml中在Servlet中获取context-param和init-param内的参数
- 传值:web.xml传递参数 即在Servlet中获取web.xml里的值
- web.xml中在Servlet中获取context-param和init-param内的参数
- Servlet(19)web.xml 配置之Servlet参数配置和读取资源文件,获取文件全路径
- web.xml中在Servlet中获取context-param和init-param内的参数
- 获取web.xml中参数的几种方法
- web.xml中在Servlet中获取context-param和init-param内的参数
- 解析web.xml中在Servlet中获取context-param和init-param内的参数