Javaweb Servlet核心接口学习记录(1)—ServletConfig接口

Javaweb Servlet核心接口学习记录(1)—ServletConfig接口

 servlet接口是servlet主要抽象的API,所有servlet都需要直接实现这一接口或者继承实现了该接口的类。servlet API中有两个类实现了Servlet接口,GenericServlet和HttpServlet。大多数情况下,开发人员只需要在这两个类的基础上扩展来实现他们自己的Servlet。在servlet体系中,除了上述两个类外,还有一些辅助servlet获取相关资源的重要接口:

  • ServletConfig接口:用于获取servlet初始化参数和servletContext对象;
  • ServletContext接口:代表当前servlet运行环境,servlet可以通过servletContext对象来访问Servlet容器的各种资源;
  • HttpServletRequest接口:用于封装Http请求信息;
  • HttpServletResponse接口:用于封装Http响应信息;

本小节单独介绍ServletConfig接口
 1. javax.servlet.ServletConfig接口的定义为:

public abstract interface javax.servlet.ServletConfig;

  容器在初始化一个servlet时,将为该servlet创建一个唯一的的servletConfig对象,并将这个对象通过init(ServletConfig config)方法来传递并保存在此Servlet对象中。
 2.ServletConfig的主要方法:

方法 方法描述
getInitParameter(String param) 根据给定的初始化参数名称,返回匹配的初始化参数值
getInitParameterNames(String param) 返回一个Enumeration对象,里面包含了所有的初始化参数
getServletContext() 返回一个ServletContext()对象
getServletName() 返回当前servlet的名字,即@WebServet的name属性值,
如果没有配置这个属性值,则返回servlet类的全限定名

 3.ServletConfig接口获取Servlet初始化参数
 由容器从Servlet的配置属性(如initParams所指定的参数)中读取。
示例: Servlet初始化参数在web.xml文件中的配置:


	HelloServlet
	com.qst.chapter03.servlet.HelloServlet
	
		url
		 jdbc:oracle:thin:@localhost:1521:orcl
	
	
		user
		qst
	
	
		password
		qst123
	


 Servlet初始化参数的获取:

public class HelloServlet extends HttpServlet {
	public void init(ServletConfig config) throws ServletException {
		String url = config.getInitParameter("url");
		String user = config.getInitParameter("user");
		String password = config.getInitParameter("password");
		try {
			Connection conn = DriverManager.getConnection(url,user,password);
		}catch(SQLException e){
			e.printStackTrace();
		}
		......
	}
}

 4.演示
 创建ServletConfigDemo

package com;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Enumeration;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ServletConfigDemo
 */
public class ServletConfigDemo extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletConfigDemo() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    @Override
    public void init(ServletConfig config) throws ServletException {
    	String name = config.getInitParameter("user");
    	String pass = config.getInitParameter("password");
    	String servletName = config.getServletName();
    	System.out.println("name值为:"+name);
    	System.out.println("pass值为:"+pass);
    	System.out.println("servletName值为:"+servletName);
    	Enumeration eums = config.getInitParameterNames();
    	while(eums.hasMoreElements()) {
    		String eum = eums.nextElement();
    		System.out.println(eum+"对应值为:"+config.getInitParameter(eum));
    	}
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

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

}

使用web.xml的方式进行配置:

"1.0" encoding="UTF-8"?>
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_4_0.xsd"
	id="WebApp_ID" version="4.0">
	ClassDemo03
	
		index.html
		index.htm
		index.jsp
		default.html
		default.htm
		default.jsp
	
	
		myservlet
		com.ServletConfigDemo
		
			user
			qst
		
		
			password
			123
		
	
	
		myservlet
		/myservlet
	

运行ServletConfigDemo
Javaweb Servlet核心接口学习记录(1)—ServletConfig接口_第1张图片
控制台
Javaweb Servlet核心接口学习记录(1)—ServletConfig接口_第2张图片

你可能感兴趣的:(Javaweb Servlet核心接口学习记录(1)—ServletConfig接口)