JSP-使用ServletConfig

package cuit;

import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = {"/cuit"}, initParams = {@WebInitParam(name = "CUIT", value = "zkl")})
public class cuit extends HttpServlet {
	//注意:成员变量只能定义在方法外、类内部
	public void init() throws ServletException{
		super.init();
		//获取初始化参数	
		ServletConfig config = getServletConfig();
		String name = config.getServletName();
		String param = config.getInitParameter("CUIT");
		System.out.println(name + ":" + param);
	}
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
		//获取ServletConfig对象,在需要时可以在任意方法中通过这种形式去调用
		//获取初始化参数
		ServletConfig config = getServletConfig();
		String name = config.getServletName();
		String param = config.getInitParameter("CUIT");
		//指定响应容器格式
		response.setContentType("text/html;charset=UTF-8");
		//write操作与底层的I/O端操作有关,但write()操作本身并不会声明抛出的任何已检查的异常
		//但在运行过程中遇到I/O错误,它会通过其调用栈向上抛出IOException
		//输出
		response.getWriter().write(name + ":" + param);	
		//输出为:cuit.cuit:zkl,原因为:config.getServletName() 返回的是Servlet的类名(包括包名,如果有的话)
		//如果没有包名的化=话返回该是;cuit:zkl
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值