获得servletcontext及servletconfig的参数

[size=medium]
1.在serlvet中可以通过以下方法得到servletconfig对象

public class LoginServlet extends HttpServlet {
	ServletConfig config;
public void init(ServletConfig sc) throws ServletException{
		this.config=sc;
		super.init(config);
		System.out.print(this.config.getInitParameter("username"));
	}

  <servlet>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>org.test.servlet.LoginServlet</servlet-class>
   <init-param>
   <param-name>username</param-name>
   <param-value>zheng</param-value>
   </init-param>
   
  </servlet>


2.通过现有的ServletConfig对象的getServletContext()就可以得到servvletcontext对象,然后调用getInitParameter()方法可以得到参数。[/size]

你可能感兴趣的:(java,servlet)