Javaweb ServletConfig类的使用

一个 ServletConfig 只对应一个 Servlet实现类

1. ServletConfig读取配置信息

1.1 创建Servlet实现类ServletConfigTest

package com.lchh.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;

public class ServletConfigTest extends HttpServlet{

	@Override
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		super.init(config);
		
		//1. 获取servlet程序的别名  serlvet-name
		System.out.println("servlet-name   "+ config.getServletName());
		
		//2. 获取初始化参数
		System.out.println("initParameter  username="+ config.getInitParameter("username"));
		System.out.println("initParameter  password="+ config.getInitParameter("password"));
		
		//3. 获取ServletContext对象
		System.out.println("ServletContext  "+ config.getServletContext());
		
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doGet(req, resp);
		System.out.println("get ");
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doPost(req, resp);
		System.out.println("post ");
	}
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.service(req, resp);
		System.out.println("service ");
	}
}

1.2 配置 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>servlet_01</display-name>
  <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>
  
  <servlet>
  	<servlet-name>ServletConfigTest</servlet-name>
  	<servlet-class>com.lchh.servlet.ServletConfigTest</servlet-class>
  	<!-- 配置初始化参数 -->
  	<init-param>
  		<param-name>username</param-name>
  		<param-value>lchh</param-value>
  	</init-param>
  	<!-- 配置初始化参数 -->
  	<init-param>
  		<param-name>password</param-name>
  		<param-value>123456</param-value>
  	</init-param>
  </servlet>
  <servlet-mapping>
  	<servlet-name>ServletConfigTest</servlet-name>
    <url-pattern>/test</url-pattern>
  </servlet-mapping>
  
</web-app>

1.3 启动项目,发送浏览器请求

  • 输出结果如下:
    在这里插入图片描述

2. ServletConfig类的3大作用

获取Servlet程序的别名 servlet-name

获取初始化参数 init-param

获取ServletContext对象

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神奇洋葱头

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值