首页 > 代码库 > jsp-application应用
jsp-application应用
application有两种应用,1是当作map,代码如下
1 <body> 2 <%! 3 int i=1; 4 %> 5 <% 6 application.setAttribute("count",String.valueOf(i++)); 7 %> 8 <%=application.getAttribute("count") %> 9 </body>
利用setAttribute和getAttribute。
2是读取web.xml配置信息,代码如下:首先web.xml代码:
1 <!--配置欢迎页 --> 2 <welcome-file-list> 3 <welcome-file>index.jsp</welcome-file> 4 </welcome-file-list> 5 <!-- 驱动 --> 6 <context-param> 7 <param-name>driver</param-name> 8 <param-value>com.mysql.jdbc.Driver</param-value> 9 </context-param> 10 <context-param> 11 <!-- url --> 12 <param-name>url</param-name> 13 <param-value>jdbc:mysql://localhost:3306/house</param-value> 14 </context-param> 15 <!-- 连接用户 --> 16 <context-param> 17 <param-name>user</param-name> 18 <param-value>root</param-value> 19 </context-param> 20 <!-- 连接密码 --> 21 <context-param> 22 <param-name>pwd</param-name> 23 <param-value>123456</param-value> 24 </context-param>
然后jsp代码:
<%@page import="java.sql.ResultSet"%> <%@page import="com.mysql.jdbc.Statement"%> <%@page import="java.sql.DriverManager"%> <%@page import="com.mysql.jdbc.Connection"%> <%@ page language="java" contentType="text/html; charset=gbk" pageEncoding="gbk"%> <!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> </head> <body> <% String driver = application.getInitParameter("driver"); String url = application.getInitParameter("url"); String user = application.getInitParameter("user"); String pwd = application.getInitParameter("pwd"); //加载驱动 Class.forName(driver); //建立连接 Connection conn = (Connection) DriverManager.getConnection(url, user, pwd); //创建Statement Statement stm = (Statement) conn.createStatement(); //执行查询 ResultSet rs = stm.executeQuery("select username,pwd from user"); %> <table border="1" width="300"> <% //遍历结果 while (rs.next()) { %> <tr> <td><%=rs.getString(1)%></td> <td><%=rs.getString(2)%></td> </tr> <% } %> </table> </body> </html>
jsp-application应用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。