初学Java Web(四)サーブレットConfigインタフェースサーブレット初期化パラメータ取得

1435 ワード

public void init(サーブレットコンフィグconfig)関数を書き換えることで、サーブレット初期化のパラメータを取得し、コンソールから出力することを実現します.
Servlet1.java
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Servlet1
 */
@WebServlet(name = "/Servlet1", urlPatterns = { "/Servlet1" }, initParams = {
		@WebInitParam(name = "name", value = "guosiqi"), })
public class Servlet1 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Servlet1() {
		super();
		// TODO Auto-generated constructor stub
	}

	public void init(ServletConfig config) {
		String name1 = config.getInitParameter("name");
System.out.println(name1);
protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {	}
/**
	 * @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);
	}

}