博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
五、JavaWeb总结:Servlet开发2(ServletConfig,ServletContext)
阅读量:6551 次
发布时间:2019-06-24

本文共 7828 字,大约阅读时间需要 26 分钟。

hot3.png

一、ServletConfig讲解

1.1、配置Servlet初始化参数

  在Servlet的配置文件web.xml中,可以使用一个或多个<init-param>标签为servlet配置一些初始化参数。

例如:

1 
2
ServletConfigDemo1
3
gacl.servlet.study.ServletConfigDemo1
4
5
6
name
7
admin
8
9
10
password
11
123456
12
13
14
charset
15
UTF-8
16
17

1.2、通过ServletConfig获取Servlet的初始化参数

  当servlet配置了初始化参数后,web容器在创建servlet实例对象时,会自动将这些初始化参数封装到ServletConfig对象中,并在调用servlet的init方法时,将ServletConfig对象传递给servlet。进而,我们通过ServletConfig对象就可以得到当前servlet的初始化参数信息。

例如:

1 package gacl.servlet.study; 2  3 import java.io.IOException; 4 import java.util.Enumeration; 5 import javax.servlet.ServletConfig; 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse;10 11 public class ServletConfigDemo1 extends HttpServlet {12 13     /**14      * 定义ServletConfig对象来接收配置的初始化参数15      */16     private ServletConfig config;17     18     /**19      * 当servlet配置了初始化参数后,web容器在创建servlet实例对象时,20      * 会自动将这些初始化参数封装到ServletConfig对象中,并在调用servlet的init方法时,21      * 将ServletConfig对象传递给servlet。进而,程序员通过ServletConfig对象就可以22      * 得到当前servlet的初始化参数信息。23      */24     @Override25     public void init(ServletConfig config) throws ServletException {26         this.config = config;27     }28 29     public void doGet(HttpServletRequest request, HttpServletResponse response)30             throws ServletException, IOException {31         //获取在web.xml中配置的初始化参数32         String paramVal = this.config.getInitParameter("name");//获取指定的初始化参数       String pwd = config.getInitParameter("password");33         response.getWriter().print(paramVal +": " + pwd);36         //获取所有的初始化参数37         Enumeration
e = config.getInitParameterNames();38 while(e.hasMoreElements()){39 String name = e.nextElement();40 String value = config.getInitParameter(name);41 response.getWriter().print(name + "=" + value + "
");42 }43 }44 45 public void doPost(HttpServletRequest request, HttpServletResponse response)46 throws ServletException, IOException {47 this.doGet(request, response);48 }49 50 }

运行结果如下:

081221_PsmX_2453485.png

二、ServletContext对象

 WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,它代表当前web应用。

 ServletConfig对象中维护了ServletContext对象的引用,开发人员在编写servlet时,可以通过ServletConfig.getServletContext方法获得ServletContext对象。
 由于一个WEB应用中的所有Servlet共享同一个ServletContext对象,因此Servlet对象之间可以通过ServletContext对象来实现通讯。ServletContext对象通常也被称之为context域对象。

3.1、多个Servlet通过ServletContext对象实现数据共享

  范例:ServletContextDemo1和ServletContextDemo2通过ServletContext对象实现数据共享

import java.io.IOException;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 ServletContextDemo1 extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        String data = "ABC";        /**         * ServletConfig对象中维护了ServletContext对象的引用,开发人员在编写servlet时,         * 可以通过ServletConfig.getServletContext方法获得ServletContext对象。         */        ServletContext context = this.getServletConfig().getServletContext();//获得ServletContext对象        context.setAttribute("data", data);  //将data存储到ServletContext对象中    }    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        doGet(request, response);    }}
import java.io.IOException;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 ServletContextDemo2 extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        ServletContext context = this.getServletContext();        String data = (String) context.getAttribute("data");//从ServletContext对象中取出数据        response.getWriter().print(data);    }    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        doGet(request, response);    }}

先运行ServletContextDemo1,将数据data存储到ServletContext对象中,然后运行ServletContextDemo2就可以从ServletContext对象中取出数据了,这样就实现了数据共享,如下图所示

095823_JApT_2453485.png

3.2、获取WEB应用的初始化参数

  在web.xml文件中使用<context-param>标签配置WEB应用的初始化参数,如下所示:

1 
2
4
5
6
7
url
8
jdbc:mysql://localhost:3306/test
9
10

  获取Web应用的初始化参数,代码如下:

3 import java.io.IOException; 4 import javax.servlet.ServletContext; 5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 11 public class ServletContextDemo3 extends HttpServlet {12 13     public void doGet(HttpServletRequest request, HttpServletResponse response)14             throws ServletException, IOException {15 16         ServletContext context = this.getServletContext();17         //获取整个web站点的初始化参数18         String contextInitParam = context.getInitParameter("url");19         response.getWriter().print(contextInitParam);20     }21 22     public void doPost(HttpServletRequest request, HttpServletResponse response)23             throws ServletException, IOException {24         doGet(request, response);25     }26 27 }

运行结果:

  105624_TJVZ_2453485.png

3.3、用servletContext实现请求转发

ServletContextDemo4 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 import javax.servlet.RequestDispatcher; 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 ServletContextDemo4 extends HttpServlet {13 14     public void doGet(HttpServletRequest request, HttpServletResponse response)15             throws ServletException, IOException {16         String data = "

abcdefghjkl

";17 response.getOutputStream().write(data.getBytes());18 ServletContext context = this.getServletContext();//获取ServletContext对象19 RequestDispatcher rd = context.getRequestDispatcher("/servlet/ServletContextDemo5");//获取请求转发对象(RequestDispatcher)20 rd.forward(request, response);//调用forward方法实现请求转发21 }22 23 public void doPost(HttpServletRequest request, HttpServletResponse response)24 throws ServletException, IOException {25 }26 }
ServletContextDemo5 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8  9 public class ServletContextDemo5 extends HttpServlet {10 11     public void doGet(HttpServletRequest request, HttpServletResponse response)12             throws ServletException, IOException {13         response.getOutputStream().write("servletDemo5".getBytes());14     }15 16     public void doPost(HttpServletRequest request, HttpServletResponse response)17             throws ServletException, IOException {18         this.doGet(request, response);19     }20 21 }

  运行结果:

  105624_eGPO_2453485.png

访问的是ServletContextDemo4,浏览器显示的却是ServletContextDemo5的内容,这就是使用ServletContext实现了请求转发。

3.4、利用ServletContext对象读取资源文件

 

转载于:https://my.oschina.net/tianshuo/blog/733758

你可能感兴趣的文章
Quartz总结(二):定时任务中使用业务类(XXService)
查看>>
MyBatis部分细节归档
查看>>
jstl标签
查看>>
Python3.x和Python2.x的区别
查看>>
text mining 2(similarity compare)
查看>>
FPGA之乒乓操作
查看>>
[120_移动开发Android]017_Anroid开发——GPS
查看>>
Number Triangles
查看>>
远程桌面无法使用,登录后立即自动注销
查看>>
oracle数据库安装组成
查看>>
Leetcode | Swap Nodes in Pairs
查看>>
常规诊疗条件下比较依那西普生物类似药(益赛普)与阿达木、英夫利西对RA的疗效[EULAR2015_SAT0360]...
查看>>
Oracle如何导入导出数据(转自)
查看>>
第十一天笔记
查看>>
安装SQl 2008为SQL Server代理服务提供的凭据无效
查看>>
Hadoop综合大作业
查看>>
python PIL 图像处理操作
查看>>
小程序分享好友和群聊
查看>>
从range和xrange的性能对比到yield关键字(中)
查看>>
007-因果图判定表
查看>>