• 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>
  • 相关阅读:
    poj 2481
    poj 3928 Ping pong
    再见oi
    NOIP 2014 解方程
    poj1836:Alignment
    poj2479:Maximum sum
    tyvj1510:专家复仇
    tyvj:P1467 通向聚会的道路
    tyvj1176: 火焰巨魔的惆怅
    tyvj1326:剑人合一
  • 原文地址:https://www.cnblogs.com/luoxiaozhao/p/6957779.html
Copyright © 2020-2023  润新知