BlogJava 联系 聚合 管理  

Blog Stats

随笔档案

文章档案

Infernu的Google site


Infernus-JXH

全局:
X:\jakarta-tomcat-5.0.28\conf\web.xml

 <servlet>
        <servlet-name>jsp</servlet-name>
        <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
        <init-param>
            <param-name>fork</param-name>
            <param-value>false</param-value>
        </init-param>
        <init-param>
            <param-name>xpoweredBy</param-name>
            <param-value>false</param-value>
        </init-param>
下加入字段实现mysql的驱动调用:
<init-param>
            <param-name>driver</param-name>
            <param-value>com.mysql.jdbc.Driver</param-value>
        </init-param>
 <init-param>
            <param-name>url</param-name>
            <param-value>jdbc:mysql://192.168.0.13:3306/myweb</param-value>
        </init-param>
 <init-param>
            <param-name>username</param-name>
            <param-value>root</param-value>
        </init-param>
 <init-param>
            <param-name>password</param-name>
            <param-value>lovejack</param-value>
        </init-param>
        <load-on-startup>3</load-on-startup>
    </servlet>

全局变量实现连接数据库的构造器:
public DbBean(ServletConfig config) {
  try {
   Class.forName(config.getInitParameter("driver"));
   con = DriverManager.getConnection(
     config.getInitParameter("url"),
     config.getInitParameter("username"),
     config.getInitParameter("password"));
  }
  catch (ClassNotFoundException e) {
   e.printStackTrace();
  }
  catch (SQLException e) {
   e.printStackTrace();
  }
 }

局部:
工程文件/WebRoot/WEB-INF/web.xml
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
后加字段:
<context-param>
   <param-name>chairman</param-name>
   <param-value>牛人</param-value>
  </context-param>
  <context-param>
   <param-name>driver</param-name>
   <param-value>com.mysql.jdbc.Driver</param-value>
  </context-param>
  <context-param>
   <param-name>url</param-name>
   <param-value>jdbc:mysql://192.168.0.13:3306/myweb</param-value>
  </context-param>
  <context-param>
   <param-name>username</param-name>
   <param-value>root</param-value>
  </context-param>
  <context-param>
   <param-name>password</param-name>
   <param-value>lovejack</param-value>
  </context-param>

在网页中可以使用pageContext.getServletContext().getInitParameter("name")直接得到value
如:
<%@page pageEncoding="GBK" %>
<%
 out.println(pageContext.getServletContext().getInitParameter("driver"));
 out.println(pageContext.getServletContext().getInitParameter("url"));
 out.println(pageContext.getServletContext().getInitParameter("username"));
 out.println(pageContext.getServletContext().getInitParameter("password"));
 
 out.println(pageContext.getServletContext().getInitParameter("chairman"));
%>
posted on 2009-09-30 10:39 Infernus 阅读(69) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: