Servlet笔记(一)


Servlet,用目前网上最流传的定义就是“Servlet是运行在服务器端的JAVA小程序,也是一个JAVA类”。

Servlet类是一个接口,它有着诸如init(),service(),destory(),getServletConfig(),servletRequset(),servletRequest()等方法。

Servlet的生命周期:服务器实例化该Servlet对象>初始化(init())>服务(service())>摧毁(destory())

GenericServlet实现了Servlet接口,并且只需要重写service()方法,相对来讲灵活了一点(适配器模式)

HttpServlet继承了GenericServlet类,这是JAVAWEB开发中最常用的类,其中我们主要用到doPost()和doGet()方法。

ServletConfig()主要是为了取得配置文件中某Servlet的配置信息,它本身就是一个类,取得配置信息的方法主要有三种:

1.

public class d1 extends HttpServlet {
	

	private ServletConfig config;


	@Override
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		this.config = config;
	}


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String encoding = config.getInitParameter("encoding");
	    //String encoding = this.getServletConfig().getInitParameter("encoding");
//		String encoding = this.getInitParameter("encoding");
		System.out.println(encoding);
	}

2.

public class d1 extends HttpServlet {
	

//	private ServletConfig config;
//
//
//	@Override
//	public void init(ServletConfig config) throws ServletException {
//		// TODO Auto-generated method stub
//		this.config = config;
//	}


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//String encoding = config.getInitParameter("encoding");
	    String encoding = this.getServletConfig().getInitParameter("encoding");
//		String encoding = this.getInitParameter("encoding");
		System.out.println(encoding);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

3.

public class d1 extends HttpServlet {
	

//	private ServletConfig config;
//
//
//	@Override
//	public void init(ServletConfig config) throws ServletException {
//		// TODO Auto-generated method stub
//		this.config = config;
//	}


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//String encoding = config.getInitParameter("encoding");
	    //String encoding = this.getServletConfig().getInitParameter("encoding");
		String encoding = this.getInitParameter("encoding");
		System.out.println(encoding);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值