标签:
package cn.jiemoxiaodi.servlet_servletcontext; 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 FServlet extends HttpServlet { /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext context = this.getServletContext(); String username=context.getInitParameter("username"); Enumeration enu=context.getInitParameterNames(); while(enu.hasMoreElements()){ String key=(String)enu.nextElement(); String value=context.getInitParameter(key); System.out.println(key+"-========>"+value); } } }
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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_2_5.xsd"> <display-name></display-name> <context-param> <param-name>username</param-name> <param-value>tomcate</param-value> </context-param> <context-param> <param-name>pwd</param-name> <param-value>123456</param-value> </context-param> <!-- 配置servlet到项目中 --> <servlet> <!-- 可以随便填只需要注意不要和其他servlet的名字重复即可(建议使用简单类名) --> <servlet-name>AServlet</servlet-name> <!-- 配置servlet的完整类名 --> <servlet-class>cn.jiemoxiaodi.servlet.AServlet</servlet-class> <init-param> <param-name>aa</param-name> <param-value>sdfds</param-value> </init-param> </servlet> <!-- 配置上面这个servlet使用那个路径能被访问 --> <servlet-mapping> <!-- 此处不能随便写了,必须与上面的servlet对应,表示在为那个servlet配置路径 --> <servlet-name>AServlet</servlet-name> <!-- 配置访问这个servlet的路径 (相对路径) /==>http://localhost:8080/day10_servlet/ http://localhost:8080/day10_servlet/AServlet --> <url-pattern>/AServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>BServlet</servlet-name> <servlet-class>cn.jiemoxiaodi.servlet_config.BServlet</servlet-class> <init-param> <param-name>a</param-name> <param-value>123456sdfd</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>BServlet</servlet-name> <url-pattern>/BServlet</url-pattern> </servlet-mapping> <servlet> <servlet-name>CServlet</servlet-name> <servlet-class>cn.jiemoxiaodi.mygenericservlet.CServlet</servlet-class> <init-param> <param-name>name</param-name> <param-value>需哦啊</param-value> </init-param> </servlet> <servlet> <servlet-name>DServlet</servlet-name> <servlet-class>cn.jiemoxiaodi.http.DServlet</servlet-class> </servlet> <servlet> <servlet-name>FServlet</servlet-name> <servlet-class>cn.jiemoxiaodi.servlet_servletcontext.FServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CServlet</servlet-name> <url-pattern>/CServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>DServlet</servlet-name> <url-pattern>/DServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>FServlet</servlet-name> <url-pattern>/FServlet</url-pattern> </servlet-mapping> </web-app>
http://localhost:8080/projectname/FServlet
package cn.jiemoxiaodi.servlet_servletcontext; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HServlet extends HttpServlet { /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.getServletContext().setAttribute("aa", "dsfd"); this.getServletContext().setAttribute("adfa", "dsfd");
this.getServletContext().removeAttribute("aa");
this.getServletContext().removeAttribute("adfa");
System.out.println(this.getServletContext().getAttribute("aa") + "===>" + this.getServletContext().getAttribute("adfa")); } }
ServletContext对象 介绍 1.这个对象代表着咱们的整个项目.==> 这个对象在一个项目中只存在一个实例. 2.启动项目的时候会自动创建. 关闭服务器的时候会销毁该实例. 3.该对象如何获取呢? ==> servletConfig 中 有一个方法叫做 getServletContext方法 来获得. 功能 一:可以获得项目中的一些配置(web.xml) getInitParameter(String name) 根据键获得值 getInitParameterNames() 获得所有键 上面这两个方法名字与servletConfig中的一模一样.但是获得的配置信息是 <context-param> <param-name>userName</param-name> <param-value>tom</param-value> </context-param> <context-param> <param-name>password</param-name> <param-value>1234</param-value> </context-param> 二:作为我们servlet技术中 三大域对象之一. 如果包含jsp的话,其实有4大域 page域. application ==> servletContext session request 域对象涉及到4个方法 getAttribute(key) 根据键获得值 setAttribute(key,value) 往application域中的map 存放一个键值对 removeAttribute(key) 从application域中的map 删除一个键值对 getAttributeNames(); 从application域中的map 获得所有的键
//获取资源的相关方法 //this.getServletContext().getRealPath(path) //this.getServletContext().getResourceAsStream(path) System.out.println(this.getServletContext().getRealPath("/WEB-INF/demo.xml")); InputStream input= this.getServletContext().getResourceAsStream("/WEB-INF/demo.xml"); System.out.println(input);
三:可以获取项目中的资源.(例如获得项目中存放学生的stu.xml) //想对WEB-INF/Demo.xml进行操作.我们要先读取出该文件 //如何来读取呢? //File file = new File("E:/代码/Workspaces0808/Day10-servlet/WebRoot/WEB-INF/demo.xml"); //上面获得demo.xml填写的路径显然是不对的.获得的是工程下的而我们要获得最终部署到服务器上的. //File file = new File("E:/apache-tomcat-6.0.35/webapps/Day10-servlet/WEB-INF/demo.xml"); //上面这种获得方式就对了.但是 如果把项目再部署到其他地方,那上面这个写死的路径就无效了. //这样类似获得外部资源的例子很多,所以servletContext对象给出了解决办法. //getRealPath("/") ==> 获得绝对路径 ,根据参数中的相对路径获得. "/"相对于WebRoot //getResourceAsStream("/"); ==>直接获得 想要找的资源的流 . 参数还是填相对路径,同上 例子: String path = getServletContext().getRealPath("/WEB-INF/demo.xml"); System.out.println(path); InputStream is = getServletContext().getResourceAsStream("/WEB-INF/demo.xml"); System.out.println(is);
使用Class和ClassLoader 获得项目中的资源路径
//web-inf下的
String path1 = getServletContext().getRealPath("/WEB-INF/demo.xml");
//lib下的
String path2 = getServletContext().getRealPath("/WEB-INF/lib/demo.xml");
//src下的
String path3 = getServletContext().getRealPath("/WEB-INF/classes/demo.xml");
//cn.itcast.servlet.servlet_context包下的
String path4 = getServletContext().getRealPath("/WEB-INF/classes/cn/itcast/servlet/servlet_context/demo.xml");
//其实上面例子中path3 和 path4 这两种方式 有些麻烦
//在介绍一种获得资源的方式,是对.class ==> Class 和 classLoader的应用
InputStream is = this.getClass().getResourceAsStream("demo.xml"); // 如果不加"/"==> 相对的是类的当前(包)目录
InputStream is2 = this.getClass().getResourceAsStream("/demo.xml");//如果加了"/" ==> "/"就代表classes目录(src)
//在说下classLoader 下也有方法 classLoader 就是类的加载器. ==> 专门来读取class文件的输入流
//也提供了读取其他文件的方法.(使用classLoader不要读取较大的文件)
// this.getClass().getClassLoader() ==> 获得类的加载器
InputStream is3 = this.getClass().getClassLoader().getResourceAsStream("/demo.xml");// 不论加不加"/",都相对于 classes目录,跟class中加"/" 的相对路径是一样的.
//使用class或classLoader 也可以获得文件的绝对路径
URL url = this.getClass().getResource("demo.xml");
System.out.println(url.getPath());
标签:
原文地址:http://www.cnblogs.com/jiemoxiaodi/p/5126006.html