• servlet-获取web.xml配置的init-param参数


    1.web.xml配置的参数

     1      <servlet>
     2         <servlet-name>firstServlet</servlet-name>
     3         <servlet-class>com.Conservlet</servlet-class>
     4         <init-param>
     5             <param-name>url</param-name>
     6             <param-value>jdbc:mysql://localhost:3306/house</param-value>
     7         </init-param>
     8         <init-param>
     9             <param-name>user</param-name>
    10             <param-value>root</param-value>
    11         </init-param>
    12         <init-param>
    13             <param-name>pwd</param-name>
    14             <param-value>123456</param-value>
    15         </init-param>
    16         <init-param>
    17             <param-name>driver</param-name>
    18             <param-value>com.mysql.jdbc.Driver</param-value>
    19         </init-param>
    20     </servlet>
    21     <servlet-mapping>
    22         <servlet-name>firstServlet</servlet-name>
    23         <url-pattern>/a</url-pattern>
    24     </servlet-mapping>

    2.servlet的doGet()获取值

     1 protected void doGet(HttpServletRequest request, HttpServletResponse response)
     2             throws ServletException, IOException {
     3             // TODO Auto-generated method stub
     4             try {            
     5             ServletConfig config = getServletConfig();
     6             String driver=config.getInitParameter("driver");
     7             String url=config.getInitParameter("url");
     8             String user=config.getInitParameter("user");
     9             String pwd=config.getInitParameter("pwd");
    10             response.setCharacterEncoding("gbk");
    11             //注册驱动
    12             Class.forName(driver);
    13             //建立连接
    14             Connection conn = (Connection)DriverManager.getConnection(url, user, pwd);
    15             Statement stm = (Statement)conn.createStatement();
    16             ResultSet rs = stm.executeQuery("select username,pwd from user");
    17             response.setContentType("text/html;charset=gbk");
    18             PrintStream out = new PrintStream(response.getOutputStream());
    19             out.println("<html>");
    20             out.println("<head>");
    21             out.println("<title>测试连接</title>");
    22             out.println("</head>");
    23             out.println("<body>");
    24             out.println("<table border="1" width="300"");
    25             while(rs.next())
    26             {
    27                 out.println("<tr>");
    28                 out.println("<td>"+rs.getString(1)+"</td>");
    29                 out.println("<td>"+rs.getString(2)+"</td>");
    30                 out.println("</tr>");
    31             }    
    32             out.println("</table>");
    33             out.println("</body>");
    34             out.println("</html>");
    35         } catch (Exception e) {
    36             // TODO: handle exception
    37         }
    38     }
  • 相关阅读:
    Java中字符串中子串的查找共有四种方法(indexof())
    idea常用快捷键
    用hive或mr清洗app数据
    使用kafka作为生产者生产数据到hdfs(单节点)
    使用kafka作为生产者生产数据到hdfs
    c++ map的使用
    c++ set集合的使用
    c++ 木块问题
    c++ 大理石
    c 蛇形数字
  • 原文地址:https://www.cnblogs.com/luoxiaozhao/p/6963824.html
Copyright © 2020-2023  润新知