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. # 深圳杯D题爬取电视收视率排行榜

    目录 深圳杯D题爬取电视收视率排行榜 站点分析 代码实现 深圳杯D题爬取电视收视率排行榜 站点分析 http://www.tvtv.hk/archives/category/tv 每天的排行版通过静态 ...

  2. python基础之 线程_进程关系

    上图

  3. c# 不让窗体显示在alt tab中

    protected override CreateParams CreateParams { get { const int WS_EX_APPWINDOW = 0x40000; const int ...

  4. 第七篇 CSS盒子

    CSS盒子模型   在页面上,我们要控制元素的位置,比如:写作文一样,开头的两个字会空两个格子(这是在学校语文作文一样),其后就不会空出来,还有,一段文字后面跟着一张图,它们距离太近,不好看,我们要移 ...

  5. java网络编程+通讯协议的理解

    参考: http://blog.csdn.net/sunyc1990/article/details/50773014 网络编程对于很多的初学者来说,都是很向往的一种编程技能,但是很多的初学者却因为很 ...

  6. pip 报错找不到pip问题

    具体报错如下 解决办法: wget https://bootstrap.pypa.io/get-pip.py  --no-check-certificate 使用当前python3运行

  7. ThinkPHP依赖注入

    D:\wamp64\www\thinkphp5.1\tp5.1\application\index\controller\Demo1.php文件 <?php namespace app\inde ...

  8. 12、Nginx代理缓存服务

    通常情况下缓存是用来减少后端压力, 将压力尽可能的往前推, 减少后端压力,提高网站并发延时 1.缓存常见类型 服务端缓存 代理缓存, 获取服务端内容进行缓存 客户端浏览器缓存 Nginx代理缓存原理 ...

  9. select,poll.epoll区别于联系

    select,poll,epoll都是IO多路复用中的模型.再介绍他们特点时,先来看看多路复用的 模型. 同其他IO的不同的是,IO多路复用一次可以等多个文件描述符.大大提高了等待数据准备好的时间的效 ...

  10. Linux编译阻塞型驱动遇到'TASK_NORMAL' undeclared (first use in this function)问题解决办法

    http://blog.csdn.net/qq_16405157/article/details/49281793