ServletConfig接口

Servlet容器初始化Servlet对象时会为Servlet创建一个ServletConfig对象,在ServletConfig对象中包含了Servlet的初始化参数信息,此外ServletConfig对象还与当前web应用中的ServletContext对象关联,Servlet容器在调用Servlet对象的init(SevletConfig config)方法时会把ServletConfig对象作为一个参数传递给Servlet对象,这个方法是的Servlet和ServletConfig对象之间建立关联,

每个初始化参数包含一对参数名和参数值,在web.xml中初始化参数时可以用<init-param>来设置参数值,<param-name>表示参数名,<param-value>表示参数值。

以下代码为一个FontServlet类设置了两个初始化参数size和color.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
           <servlet>
        <servlet-name>Font</servlet-name>
        <servlet-class>com.demo.servlet.FontServlet</servlet-class>
        <init-param>
            <param-name>color</param-name>
            <param-value>red</param-value>
        </init-param>
        <init-param>
            <param-name>size</param-name>
            <param-value>35</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>Font</servlet-name>
        <url-pattern>/font</url-pattern>
    </servlet-mapping>

</web-app>

FontServle.java

package com.demo.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by 谭雪娇 on 2017/4/6.
 */
public class FontServlet extends HttpServlet  {
    public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException ,IOException{
        String word=request.getParameter("word");
        if(word==null){
            word="Hello";
        }
        //读取初始化参数
        String color=getInitParameter("color");
        String size=getInitParameter("size");
        System.out.println("ServletName:"+getServletName());//打印ServletName:Font
        /**设置HTTP响应正文得MIME类型及字符编码*/
        response.setContentType("text/html;charset=UTF-8");
        /*输出HTML文档*/
        PrintWriter out=response.getWriter();
        out.println("<html><head><title>FontServlet</title></head>");
        out.println("<font size='"+size+"'color='"+color+"'>"+word+"</font>");
        out.println("</body></html>");
        out.close();

}
}

tomcat服务器控制台结果

浏览器结果

ServletConfig接口的更多相关文章

  1. ServletConfig接口默认是哪里实现的?

    问题:Servlet接口默认是哪里实现的? 答:GenericServlet 1.结构 2.ServletConfig.GenericServlet.HttpServlet的关系如下: public ...

  2. Servlet(7)—ServletConfig接口和SevletContext接口

    ServletConfig接口 1. 可以获取当前Servlet在web.xml中的配置信息(用的不多) 2. 在不使用"硬编码"的情况下,将部署状态信息传递给Servlet.这个 ...

  3. java EE :GenericServlet 抽象类、ServletConfig 接口

    ServletConfig 接口:当前 Servlet 在 web.xml 中相关配置信息 package javax.servlet; import java.util.Enumeration; p ...

  4. Java EE javax.servlet中的ServletConfig接口

    ServletConfig接口 public interface ServletConfig 实现类:GenericServlet.HttpServlet 一.介绍 一个供servlet容器使用配置对 ...

  5. servlet的ServletConfig接口

    ServletConfig接口 A servlet configuration object used by a servlet container to pass information to a ...

  6. javax.servlet.ServletConfig接口(五)

    主要作用是保存web.xml文件里面的配置信息 一个servlet对应一个ServletConfig,100个servlet对应100个ServletConfig.   代码如下(单个获取和获取所有) ...

  7. java学习笔记—ServletConfig、ServletContext接口(13)

    ServletConfig是一个由Tomcat服务器在初始化Servlet的时候创建并传递进来的一个对象. 该对象主要描述的时候一个servlet的配置信息. 如: <servlet>  ...

  8. ServletConfig和ServletContext接口

    ServletConfig 在web.xml文件中使用一个或多个init-param元素进行配置后,Tomcat初始化Servlet时,都会将该Servlet的配置信息封装到一个ServletConf ...

  9. Servlet学习笔记(二)之Servlet路径映射配置、Servlet接口、ServletConfig、ServletContext

    Servlet路径映射配置 要使Servlet对象正常的运行,需要进行适当的配置,以告诉Web容器哪个请求调用哪个Servlet对象处理,对Servlet起到一个注册的作用.Servlet的配置信息包 ...

随机推荐

  1. Hive 教程(三)-DDL基础

    DDL,Hive Data Definition Language,数据定义语言: 通俗理解就是数据库与库表相关的操作,本文总结一下基本方法 hive 数据仓库配置 hive 数据仓库默认位置在 hd ...

  2. hashmap存储数据

    在HashMap中,为什么不能使用基本数据类型作为key? 其实和HashMap底层的存储原理有关,HashMap存储数据的特点是:无序.无索引.不能存储重复元素. 存储元素采用的是hash表存储数据 ...

  3. 精选 TOP 面试题

    1 两数之和     46.5%简单2 两数相加     35.5%中等3 无重复字符的最长子串     31.1%中等4 寻找两个有序数组的中位数     35.9%困难5 最长回文子串     2 ...

  4. 05 Django之模型层---单表操作

    一 ORM简介 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人 ...

  5. js的作用主要这么几个

    js的作用主要有这么几个表单验证:网页上,用户输入的信息需要进行验证,在客户端验证,可以减少对服务器端的压力.所以,你应该把握正则表达式方面的知识.网页特效:页面上很多特效是非常好的,能产生很好的用户 ...

  6. python 运行sum函数的使用

    sum(iterable[, start]) ,iterable为可迭代对象,如: sum([ ], start) , #iterable为list列表. sum(( ), start ) , #it ...

  7. Windows常用快捷键与常用命令

    应用窗口: Alt+F4 关闭当前窗口Win+上 最大化当前窗口Win+下 最小化当前窗口WIN+D 最小化所有窗口/还原Win+Tab 切换窗口 常用工具: Win+R 打开运行对话框Win+E 打 ...

  8. laravel swoole Call to undefined method Illuminate\Events\Dispatcher::fire()

    报错: Call to undefined method Illuminate\Events\Dispatcher::fire() Whoops\Run::handleError("Unca ...

  9. Zabbix Agent配置文件详解

    # This is a config file for the Zabbix agent daemon (Unix)# To get more information about Zabbix, vi ...

  10. sed命令替换文件内容

    reference: https://www.cnblogs.com/starof/p/4181985.html 抓取目录名并修改 ls | grep "XXX" > 1.t ...