servletConfig对象
在Servlet的配置文件中,可以使用一个或多个<init-param>标签为servlet配置一些初始化参数。
当servlet配置了初始化参数后,web容器在创建servlet实例对象时,会自动将这些初始化参数封装到ServletConfig对象中,并在调用servlet的init方法时,将ServletConfig对象传递给servlet。进而,程序员通过ServletConfig对象就可以得到当前servlet的初始化参数信息。
例子:在servlet中获取ServletConfig对象:
package com.bjsxt.config.servlet;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 学习ServletConfig(I)对象
* servlet容器使用的servletconfig配置对象在初始化期间将信息传递到servlet对象。
*
* @author Administrator
*
*/
public class LearnServletConfig extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取ServletConfig配置对象
ServletConfig config=this.getServletConfig();
//利用ServletConfig对象获取当前Servlet的配置信息
System.out.println(config.getServletName());
System.out.println(config.getInitParameter("Sclass"));
System.out.println(config.getInitParameter("Sgrade"));
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>web_project_1018_servletconfig</display-name>
<servlet>
<servlet-name>LearnServletConfig</servlet-name>
<servlet-class>com.bjsxt.config.servlet.LearnServletConfig</servlet-class>
<init-param>
<description>描述信息</description>
<param-name>Sclass</param-name>
<param-value>506</param-value>
</init-param>
<init-param>
<description>描述信息</description>
<param-name>Sgrade</param-name>
<param-value>2016</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>LearnServletConfig</servlet-name>
<url-pattern>/config.action</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
servletConfig对象的更多相关文章
- ServletConfig对象详解
在Servlet 的配置文件中,可以用一个或多个<init-param>标签为servlet配置一些初始化参数. 当servlet配置了初始化参数之后,web容器在创建servlet实例对 ...
- ServletConfig对象和它在开发中的应用场
package cn.itcast; import java.io.IOException; import java.io.PrintWriter; import java.util.Enumerat ...
- Java Servlet(三):Servlet中ServletConfig对象和ServletContext对象
本文将记录ServletConfig/ServletContext中提供了哪些方法,及方法的用法. ServletConfig是一个抽象接口,它是由Servlet容器使用,在一个servlet对象初始 ...
- HTTP协议 Servlet入门 Servlet工作原理和生命周期 Servlet细节 ServletConfig对象
1 HTTP协议特点 1)客户端->服务端(请求request)有三部份 a)请求行--请求行用于描述客户端的请求方式.请求的资源名称,以及使用的HTTP协议版本号 请求行中的GET ...
- ServletConfig对象 【通过此对象获取到web.xml中的信息】
用途: 1)想让当前的Servlet读取一些在web.xml文件配置的初始化参数时, 可以使用ServletConfig对象,他是Servlet运 ...
- javaWEB总结(3):ServletConfig对象
1.首先找到ServletConfig的API: ServletConfig封装了servlet的配置信息,并且可以获取servletContext对象. ServletConfig共有四个方法: 1 ...
- 小谈-—ServletConfig对象和servletContext对象
一.servletContext概述 servletContext对象是Servlet三大域对象之一,每个Web应用程序都拥有一个ServletContext对象,该对象是Web应用程序的全局对象或者 ...
- Servlet(八):ServletContext对象和ServletConfig对象
ServletContext 对象:问题: Request 解决了一次请求内的数据共享问题,session 解决了用户不同请求的数据共享问题,那么不同的用户的数据共享该怎么办呢?解决: 使用 Serv ...
- Servlet 使用ServletConfig对象来配置Servlet
ServletContext和ServletConfig的关联 相同点: 1.都可以用来配置Servlet 2.都可以写在web.xml中. 区别点: 1.ServletContext对象,对于所有的 ...
随机推荐
- sqlsever2008数据库的备份与还原
本文数据库的名称为ProjectControl public static SqlConnection conn = new SqlConnection("server=(local);u ...
- MVC5框架解析之Controller的创建
在上一讲中我们介绍了MvcHandler,知道在Handler里面注入两个接口属性分别为IControllerFactory和IController的factory和controller.并且通过IO ...
- TCP/IP入门学习(1)---分层概述
本文旨在记述一些学习中的笔记 OSI分层:应用层,表示层,会话层,传输层,网络层,数据链路层,物理层 详细点: 1.应用层:为应用程序提供服务并规定程序中通信相关细节. 2.表示层:将应用处理的信息转 ...
- Codeforces Round #197 (Div. 2) : B
也是水题一个,不过稍微要细心点.... 贴代码: #include<iostream> using namespace std; long long n,m; ; int main() { ...
- Docker日志自动化: ElasticSearch、Logstash、Kibana以及Logspout
http://www.open-open.com/lib/view/open1432107136989.html
- 练习--python中的Queue与多进程(multiprocessing)
按官方说法: This module is OBSOLETE and is only provided on PyPI to support old projects that still use i ...
- Markdown各种小问题汇总
如何分割Quote? How can I write two separate blockquotes in sequence using markdown? > Imagination is ...
- How to convert an IPv4 address into a integer in C#?
仅仅针对于IPv4的处理方法 从string转换为int 以及从int转换为string // IPv4 int intAddress = BitConverter.ToInt32(IPAddress ...
- CF 370
A:http://codeforces.com/problemset/problem/370/A #include<stdio.h> #include<string.h> #i ...
- UVA_437_The_Tower_of_the_Babylon_(DAG上动态规划/记忆化搜索)
描述 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...