Servlet--ServletConfig接口,GenericServlet类
- ServletConfig接口
定义:public interface ServletConfig
这个接口定义了一个对象, 通过这个对象, Servlet 引擎配置一个 Servlet 并且允许 Servlet获得一个有关它的 ServletContext 接口的说明。每一个 ServletConfig 对象对应着一个唯一的Servlet。
方法:
1、getInitParameter
public String getInitParameter(String name);
这个方法返回一个包含 Servlet 指定的初始化参数的 String。 如果这个参数不存在, 返加空值。
2、getInitParameterNames
public Enumeration getInitParameterNames();
这个方法返回一个列表 String 对象,该对象包括 Servlet 的所有初始化参数名。如果Servlet 没有初始化参数,getInitParameterNames 返回一个空的列表。
3、getServletContext
public ServletContext getServletContext();
返回这个 Servlet 的 ServletContext 对象。
- GenericServlet接口
public abstract class GenericServlet implements Servlet,ServletConfig, Serializable;
这个类的存在使得编写 Servlet 更加方便。它提供了一个简单的方案,这个方案用来执
行有关Servlet生命周期的方法以及在初始化时对ServletConfig对象和ServletContext对象进
行说明。
方法
1、destroy
public void destroy();
在这里 destroy 方法不做任何其他的工作。
2、getInitParameter
public String getInitParameter(String name);
这是一个简便的途径,它将会调用 ServletConfig 对象的同名的方法。
3、getInitParameterNames
public Enumeration getInitParameterNames();
这是一个简便的途径,它将会调用 ServletConfig 对象的同名的方法。
4、getServletConfig
public ServletConfig getServletConfig();
返回一个通过这个类的 init 方法产生的 ServletConfig 对象的说明。
5、getServletContext
public ServletContext getServletContext();
这是一个简便的途径,它将会调用 ServletConfig 对象的同名的方法。
6、getServletInfo
public String getServletInfo();
返回一个反映 Servlet 版本的 String。
7、init
public void init() throws ServletException;
public void init(ServletConfig config) throws ServletException;
init(ServletConfig config)方法是一个对这个 Servlet 的生命周期进行初始化的简便的途径。
init(ServletConfig config)方法会存储config对象然后调用init() 。 如果你重载了这个方法 ,你必须调用 super.init(config),这样 GenericServlet 类的其他方法才能正常工作。
init()方法是用来让你对 GenericServlet 类进行扩充的,使用这个方法时,你不需要存储config 对象,也不需要调用 super.init(config)。
8、 log
public void log(String msg);
public void log(String msg, Throwable cause);
通过 Servlet content 对象将 Servlet 的类名和给定的信息写入 log 文件中。
9、 service
public abstract void service(ServletRequest request, ServletResponse response) throws ServletException, IOException;
这是一个抽象的方法,当你扩展这个类时,为了执行网络请求,你必须执行它。
package javax.servlet; import java.io.IOException;
import java.io.Serializable;
import java.util.Enumeration; public abstract class GenericServlet implements Servlet, ServletConfig, Serializable
{
//这里封装一个ServletConfig对象,每一个 ServletConfig 对象对应着一个唯一的Servlet。
private transient ServletConfig config; public void destroy()
{
} //这是一个简便的途径,它将会调用 ServletConfig 对象的同名的方法。以下3个方法都是:
public String getInitParameter(String name)
{
return getServletConfig().getInitParameter(name);
} public Enumeration getInitParameterNames()
{
return getServletConfig().getInitParameterNames();
} //Servlet引擎配置一个 Servlet,并且允许 Servlet,获得一个有关它的 ServletContext 接口的说明
public ServletContext getServletContext()
{
return getServletConfig().getServletContext();
} //我们常常在自己写的Servlet用这个方法,其实就是在这里被继承过去的
public ServletConfig getServletConfig()
{
return this.config;
} public String getServletInfo()
{
return "";
} //重写Servlet的init方法
public void init(ServletConfig config) throws ServletException
{
this.config = config;
init();
} public void init() throws ServletException
{
} public void log(String msg)
{
getServletContext().log(getServletName() + ": " + msg);
} public void log(String message, Throwable t)
{
getServletContext().log(getServletName() + ": " + message, t);
} //推迟到子类实现,这里仍然是抽象方法
public abstract void service(ServletRequest paramServletRequest, ServletResponse paramServletResponse) throws ServletException, IOException; public String getServletName()
{
return this.config.getServletName();
}
}
Servlet--ServletConfig接口,GenericServlet类的更多相关文章
- Servlet基础之一:Servlet基本接口与类
1.概述 Servlet API中共有5个包,约113个接口和类: javax.servlet javax.servlet.http javax.servlet.jsp javax.servlet.a ...
- Servlet的接口实现类
Sun公司提供了两个默认实现类 GenericServlet和HttpServlet HttpServlet指能够处理Http请求的Servlet,它在原有的Servlet基础上添加了与HTTp相关的 ...
- javax.servlet.ServletConfig接口(五)
主要作用是保存web.xml文件里面的配置信息 一个servlet对应一个ServletConfig,100个servlet对应100个ServletConfig. 代码如下(单个获取和获取所有) ...
- Servlet各种接口和类
http://blog.csdn.net/jediael_lu/article/details/25036019
- Servlet API遍程常用接口和类
本文主要总结Servlet API遍程常用接口和类 Servlet API http://tomcat.apache.org/tomcat-5.5-doc/servletapi/index.html ...
- JavaWeb学习——Servlet相关的接口和类
JavaWeb学习——Servlet相关的接口和类 摘要:本文主要学习了Servlet相关的接口和类. Servlet的接口和类 三种方式 实现Servlet有三种方式: 实现javax.servle ...
- Javaweb学习笔记——(九)——————Servlet的进入,及ServletConfig、GenericServlet、HttpServlet、ServletContext、获取类路径资源
Servlet1.什么是Servlet? *Servlet是Javaweb三大组件之一(Servlet,Filter,Listener) *Servlet是用来处理客户端请求的动态资源 *Servle ...
- Servlet接口、GenericServlet类、HttpServlet类
Servlet是最顶层的接口,其提供的方法有: init(ServletConfig config):void // 初始化 getServletConfig():ServletConfig // 取 ...
- javaWeb学习总结(3)- Servlet总结(servlet的主要接口、类)
Servlet总结01——servlet的主要接口.类 (一)servlet类 Servlet主要类.接口的结构如下图所示: 要编写一个Servlet需要实现javax.servlet.Servlet ...
随机推荐
- linux下安装运行LoadrGenerator
注:在LoadGenerator的安装使用的过程,涉及到了shell变量与环境变量.用户使用的当前shell.创建用户等一系列的linux操作系统的问题,关注我后续的博客,会为大家继续讲解这些问题. ...
- Java学习笔记20(String类应用、StringBuffer类、StringBuilder类)
1.获取指定字符串中大小写和数字的个数: package demo; public class StringTest { public static void main(String[] args) ...
- Linux常见命令(权限)
创建a.txt和b.txt文件,将他们设为其拥有者和所在组可写入,但其他以外的人则不可写入:chmod ug+w,o-w a.txt b.txt 创建c.txt文件所有人都可以写和执行chmod a= ...
- 【LCT+主席树】BZOJ3514 Codechef MARCH14 GERALD07加强版
3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 2023 Solved: 778 ...
- Java生成微信二维码及logo二维码
依赖jar包 二维码的实现有多种方法,比如 Google 的 zxing 和日本公司的 QrCode,本文以 QrCode 为例. QrCode.jar:https://pan.baidu.com/s ...
- MVVMLight 1:MVVMLight介绍以及在项目中的使用
一.MVVM 和 MVVMLight介绍 MVVM是Model-View-ViewModel的简写.类似于目前比较流行的MVC.MVP设计模式,主要目的是为了分离视图(View)和模型(Model)的 ...
- 类Unix平台程序调试
GNU Binutils GNU Binutils 建立main.c文件,内容如下: #include <stdio.h> void main() { int a = 5/0; } 编译m ...
- 14、ABPZero系列教程之拼多多卖家工具 新建微信公众号模块
说是模块,其实在MVC中就是区域,新建一个区域专门管理整个微信功能. Web项目新建区域 在Web项目Areas目录下新建一个区域,名称为“Weixin",如下图: 接着打开web.conf ...
- Ubuntu环境下IPython的搭建和使用
1. Ubuntu操作系统版本 说明:Ubuntu 12.04.3 LTS自带的Python 2.7.3版本. 2. 安装IPython 说明: 输入命令sudo apt-get install ip ...
- Vue中的$set的使用
在我们使用vue进行开发的过程中,可能会遇到一种情况:当生成vue实例后,当再次给数据赋值时,有时候并不会自动更新到视图上去: 当我们去看vue文档的时候,会发现有这么一句话:如果在实例创建之后添加新 ...