servlet获取web.xml中的信息

web.xml中的文件配置如下:

   a
   com.qq1009108034.zxg.AServlet
   
   
      driver
      com.mysql.jdbc.Driver
   

   
      url
      jdbc:mysql://localhost:3306/shop
   

   
      user
      root
   

   
      password
      123
   



   a
   /a

 JAVA代码如下:

public class AServlet implements Servlet {
    private ServletConfig config;
    public void init(ServletConfig config) throws ServletException {
//       System.out.println("servletConfig="+arg0.toString());
this.config=config;
}
public void service(ServletRequest arg0, ServletResponse response)
throws ServletException, IOException {

               //获取servletConfig
 ServletConfig config=getServletConfig();
//通过初始化的name获取value
 String driver=config.getInitParameter("driver");
 String url=config.getInitParameter("url");
 String user=config.getInitParameter("user");
 String password=config.getInitParameter("password");
 response.setContentType("text/html;charset=UTF-8");
 PrintWriter out=response.getWriter();
out.print(driver);
out.print(url);
out.print(user);
out.print(password);


}

你可能感兴趣的:(JAVAWEB)