ServletConfig参数和ServletContext参数到底应该如何使用,如何取得?
1.新建servlet
public class MyServletConfig extends HttpServlet
{
private static final long serialVersionUID = 1L;
ServletConfig config;
public MyServletConfig() {
super();
}
/**
* 在inii()方法中取到:通过init的重载方法传递
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
this.config = config;
}
public ServletConfig getServletConfig() {
return config;
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String username = config.getInitParameter("username");
PrintWriter pw = response.getWriter();
pw.print(username);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
2.配置WEB.XML文件
MyServletConfig
MyServletConfig
com.tw.util.MyServletConfig
username
myname
MyServletConfig
/MyServletConfig