jsp application使用getInitParameter来获取web.xml中配置参数
2017-03-21 08:55
573 查看
在web.xml中配置如下代码:
<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://localhost:3307/test</param-value>
</context-param>
<context-param>
<param-name>user</param-name>
<param-value>root</param-value>
</context-param>
<context-param>
<param-name>pass</param-name>
<param-value>root</param-value>
</context-param>
然后在需要使用参数的页面通过getInitParameter()来获取:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!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=
4000
UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1">
<tr><th>编号<th>用户名</th><th>密码</th></tr>
<%
String driver = application.getInitParameter("driver");
String url = application.getInitParameter("url");
String user = application.getInitParameter("user");
String pass = application.getInitParameter("pass");
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, pass);
Statement stmt = conn.createStatement();
String sql = "select * from user";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
%>
<tr><td><%=rs.getString(1)%></td><td><%=rs.getString(2)%></td><td><%=rs.getString(3)%></td></tr>
<%
}
%>
</table>
</body>
</html>
重启tomcat后,参数才起效,可以看到结果:
相关文章推荐
- jsp application使用getInitParameter来获取web.xml中配置参数
- jsp application使用getInitParameter来获取web.xml中配置参数
- jsp 使用application.getInitParameter来获取web.xml中配置参数
- jsp application使用getInitParameter来获取web.xml中配置参数
- web.xml中在Servlet中获取context-param和init-param内的参数
- web.xml配置参数context-param和init-param的区别
- web.xml中在Servlet中获取context-param和init-param内的参数
- 利用Uri获取返回的参数即getQueryParameter()方法的使用
- 利用Uri获取返回的参数即getQueryParameter()方法的使用
- web.xml中在Servlet中获取context-param和init-param内的参数
- 关于web.xml里相关配置参数的作用及使用
- web.xml中在Servlet中获取context-param和init-param内的参数
- servlet获取web.xml中配置的初始化参数
- web.xml中在Servlet中获取context-param和init-param内的参数
- web.xml中在Servlet中获取context-param和init-param内的参数
- 20160321servlet学习笔记获取web.xml配置参数
- 在Filter中获取web.xml中配置的参数
- servlet中配置文件web.xml中的参数context-param和init-param区别
- servlet中配置文件web.xml中的参数context-param和init-param区别
- 解析web.xml中在Servlet中获取context-param和init-param内的参数