随笔-0  评论-0  文章-16  trackbacks-0
Application 提供了对应用程序环境属性访问的方法。

在WEB-INF目录下面新建web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <context-param>    <!-- 定义连接数据库URL -->
  <param-name>url</param-name>
  <param-value>jdbc:mysql://localhost:3306/db_database15</param-value>
 </context-param>
 <context-param>   <!-- 定义连接数据库用户名 -->
  <param-name>name</param-name>
  <param-value>root</param-value>
 </context-param>
 <context-param>   <!-- 定义连接数据库mim -->
  <param-name>password</param-name>
  <param-value>111</param-value>
 </context-param>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

t9.jsp代码如下:

<%@ page language="java" contentType="text/html; charset=gbk"
    pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>Insert title here</title>
<base href="<%=basePath%>">
</head>
<body>
<%
 
   String url = application.getInitParameter("url"); //获取初始化参数,与web.xml文件中内容对应
   String name = application.getInitParameter("name");
   String password = application.getInitParameter("password");
   out.println("URL: "+url+"<br>");
   out.println("name: "+name+"<br>");
   out.println("password: "+password+"<br>");
%>
</body>
</html>



posted on 2012-07-04 14:39 jhtchina 阅读(164) 评论(0)  编辑  收藏 所属分类: jsp初学