代表当前Servlet在web.xml中的配置信息
String getServletName() -- 获取当前Servlet在web.xml中配置的名字public class SConfigServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.获取ServletConfig对象 ServletConfig servletConfig = this.getServletConfig(); //2.获取servlet名字 String getServletName() String servletName = servletConfig.getServletName(); System.out.println(servletName); //3.获取初始化参数值 String value1 = getInitParameter("name1"); String value2 = getInitParameter("name2"); System.out.println(value1); System.out.println(value2); System.out.println("-----------------"); //4.获取当前Servlet所有初始化参数的名字组成的枚举 Enumeration namesEnum = this.getInitParameterNames(); while (namesEnum.hasMoreElements()) { String name = (String) namesEnum.nextElement(); String value = servletConfig.getInitParameter(name); System.out.println(name+":"+value); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
代表当前web应用
1.做为域对象可以在整个web应用范围内共享数据//设置属性
public class Demo2Servlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.getServletContext().setAttribute("chairman", "XiDaDa"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }//获取属性值
public class Demo3Servlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String v = (String) this.getServletContext().getAttribute("chairman"); System.out.println(v); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
整个web应用的初始化参数,位于web.xml文件中的<web-app>节点下。
请求参数 parameter --- 浏览器发送过来的请求中的参数信息
public class Demo4Servlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /** * 获取整个web应用的初始化信息 * ServletContext是整个web应用唯一的,所有的servlet都共享整个context * ServletConfig是每个servlet可以获取的,从而得到当前servlet的一些信息 */ ServletContext sContext = this.getServletContext(); Enumeration names = sContext.getInitParameterNames(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); String value = sContext.getInitParameter(name); System.out.println(name+":"+value); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
public class Demo5Servlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { RequestDispatcher requestDispatcher = this.getServletContext() .getRequestDispatcher("/servlet/Demo6Servlet"); //转发到Demo6Servlet requestDispatcher.forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }Demo6Servlet文件:
public class Demo6Servlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().write("$99999999999999999999"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
4.加载资源文件
①第一种情况是在在Servlet中读取资源文件。
在Servlet中读取资源文件时:this.getServletContext().getRealPath("config.properties")
举例:先在项目文件下建立config.properties文件,在其中写入以下代码:
username=zhang
password=123
servlet访问代码:
public class Demo7Servlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Properties prop = new Properties(); //prop.load(new FileReader("D:\\tomcat6\\webapps\\Day0202\\config.properties")); //这种方法可以,但是不好!!! prop.load(new FileReader(this.getServletContext().getRealPath("config.properties")));//推荐使用的方法 System.out.println(prop.getProperty("username")); System.out.println(prop.getProperty("password")); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
②第二种情况是在非Servlet环境下
比如,我又单开一个包,包中有个service方法,需要加载资源文件。如何解决?
如果在非Servlet环境下要读取资源文件时可以采用类加载器加载文件的方式读取资源Service.class.getClassLoader().getResource("../../../config
包com.jnu509中的文件:
package com.jnu509; import com.jnu509.service.Service; public class Demo8Servlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Service service = new Service(); service.method(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
包com.jnu509.service中的文件,可以看到,我们现在要访问项目中的config.properties文件。
package com.jnu509.service; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Properties; public class Service { public void method() throws FileNotFoundException, IOException { Properties prop = new Properties(); prop.load(new FileReader(Service.class.getClassLoader().getResource("../../config.properties").getPath())); System.out.println(prop.getProperty("user")); System.out.println(prop.getProperty("password")); } }
ServletConfig和ServletContext,布布扣,bubuko.com
原文地址:http://blog.csdn.net/dwyers/article/details/38439879