关闭

初始化servletConfig

288人阅读 评论(0) 收藏 举报
分类:
package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
/**
 * ServletConfig  利用@WebServlet封装servletConfig数据,初始化servlet
 * @author BrightSt
 *
 */
@WebServlet(name="ServletConfigDemoServlet",
			urlPatterns={"/ServletConfigDemo"},
			initParams={
					@WebInitParam(name="admin",value="Harry Taciak"),
					@WebInitParam(name="email",value="admin@example.com")
}	)
public class ServletConfigDemoServlet implements Servlet{
	 private transient ServletConfig servletConfig;
	 
	public void destroy() {
		
	}

	public ServletConfig getServletConfig() {
		
		return this.servletConfig;
	}

	public String getServletInfo() {
	
		return "Servlet Demo";
	}

	public void init(ServletConfig servletConfig) throws ServletException {
		this.servletConfig=servletConfig;
		
	}

	public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
		ServletConfig servletConfig = getServletConfig();
		String admin = servletConfig.getInitParameter("admin");
		String email = servletConfig.getInitParameter("email");
		response.setContentType("text/html");
		PrintWriter writer = response.getWriter();
		writer.print("<html><head></head><body>"
				+ "admin:"+admin
				+ "<br/>email:"+email
				+ "<br/></body>"
				+ "</html>");
	}

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:19874次
    • 积分:880
    • 等级:
    • 排名:千里之外
    • 原创:69篇
    • 转载:5篇
    • 译文:0篇
    • 评论:0条