标签:初始化 val config print 服务器 package 保存 http font
1 public abstract class MyServlet implements Servlet { 2 3 public void init(ServletConfig config) { 4 ServletContext context = config.getServletContext(); 5 } 6 7 }
1 public class MyServlet02 extends HttpServlet{ 2 3 protected void doGet(HttpServletRequest req, HttpServletResponse resp) 4 throws ServletException, IOException { 5 ServletContext context = this.getServletContext(); 6 } 7 8 }
void setAttribute(String name,Object object) 用来储存一个对象,也可以说用来储存一个属性,例如,servletContext.setAttribute("xxx",XXX); 在ServletContext就保存了一个域属性,域属性名为xxx,域属性值为XXX。与Map相同。
ObjectgetAttribute(String name) 通过一个域属性名取得域属性值,如String value=(String)servletContext.getAttribute("xxx");
EnumerationgetAttributeNames() 取得所有域属性名,是所有,不是一个
下面举个例子来说明第一第二种功能
1 public class AServlet extends HttpServlet { 2 3 protected void doGet(HttpServletRequest req, HttpServletResponse resp) 4 throws ServletException, IOException { 5 ServletContext application = this.getServletContext(); 6 application.setAttribute("name", "123"); 7 } 8 }
public class BServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext application=this.getServletContext(); String value=(String) application.getAttribute("name"); System.out.println("通过域属性名取得域属性值 :"+value); } }
启动服务器后,执行AServlet,再执行BServlet 后可以看到如下结果
如配置公共的初始化参数,首先要先配置web.xml文件
再来新建一个CServlet.java
package org.lxh.serletdemo; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; public class CServlet extends HttpServlet { public void init() throws ServletException { ServletContext application =this.getServletContext(); String value=(String)application.getInitParameter("context-param"); System.out.println(value); } }
启动服务器后,再执行CServlet可以看到如下结果
1、可以使用ServletContext 对象来获取Web应用下的资源,例如在web目录下有个input.html文件,现在想在servlet中获取这个资源,就可以通过ServletContext来获取
1 package org.lxh.serletdemo; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletContext; 7 import javax.servlet.ServletException; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 public class DServlet extends HttpServlet { 13 public void doGet(HttpServletRequest request, HttpServletResponse response) 14 throws ServletException, IOException { 15 ServletContext application=this.getServletContext(); //获取ServletContext对象 16 String realPath=application.getRealPath("input.html"); //通过getRealPath得到真实路径 17 System.out.println(realPath); //打印路径 18 } 19 }
2. 获取资源流
1 package org.lxh.serletdemo; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.io.PrintWriter; 6 import java.util.Set; 7 8 import javax.servlet.ServletContext; 9 import javax.servlet.ServletException; 10 import javax.servlet.http.HttpServlet; 11 import javax.servlet.http.HttpServletRequest; 12 import javax.servlet.http.HttpServletResponse; 13 14 public class DServlet extends HttpServlet { 15 public void doGet(HttpServletRequest request, HttpServletResponse response) 16 throws ServletException, IOException { 17 ServletContext application=this.getServletContext(); 18 InputStream input =application.getResourceAsStream("input/jsp");//获取资源路径后,再创建出输入流对象 19 20 } 21 }
3. 获取指定目录下的所有资源路径,如输出WEB-INF下 的所有路径
1 package org.lxh.serletdemo; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.io.PrintWriter; 6 import java.util.Set; 7 8 import javax.servlet.ServletContext; 9 import javax.servlet.ServletException; 10 import javax.servlet.http.HttpServlet; 11 import javax.servlet.http.HttpServletRequest; 12 import javax.servlet.http.HttpServletResponse; 13 14 public class DServlet extends HttpServlet { 15 public void doGet(HttpServletRequest request, HttpServletResponse response) 16 throws ServletException, IOException { 17 ServletContext application=this.getServletContext(); //获取ServletContext对象 18 Set<String > paths=application.getResourcePaths("/WEB-INF"); 19 System.out.println(paths); 20 } 21 }
标签:初始化 val config print 服务器 package 保存 http font
原文地址:http://www.cnblogs.com/limn/p/7204129.html