JavaWeb 期末考试编程题

5/5/2021 jspjavamysql

# 正经人谁用JSP啊 🤷‍♂️


# 1. 静态HTML页面

# form.html

<html>
<head>
    <meta charset="UTF-8">
    <title>表单</title>
</head>
<body>
    <form name="f1" method="POST" action="XXX.jsp">
    <table>
    <caption>
        用户注册
    </caption>
    <tr>
        <td>用户名</td>
        <td><input type="text" name="t1"></td>
    </tr>
    <tr>
        <td>密码</td>
        <td><input type="password" name="t2"></td>
    </tr>
    <tr>
        <td>确认密码</td>
        <td><input type="password" name="t3"></td>
    </tr>
    <tr>
        <td>性别</td>
        <td>
            <input type="radio" name="r1" value="" checked><input type="radio" name="r2" value=""></td>
    </tr>
    <tr>
        <td>爱好</td>
        <td>
            <input type="checkbox" name="c1" value="音乐">音乐
            <input type="checkbox" name="c2" value="美术">美术
            <input type="checkbox" name="c3" value="旅游">旅游
        </td>
    </tr>
    <tr>
        <td >E-mail</td>
        <td><input type="email" name="t4"></td>
    </tr>
    <tr>
        <td >文件</td>
        <td><input type="file" name="file"></td>
    </tr>
    <tr>
        <td >简介</td>
        <td><textarea name="t5"></textarea></td>
    </tr>
    <tr>
        <td >城市</td>
        <td><select>
            <option value="bj">北京</option>
            <option value="sh">上海</option>
            <option value="sz">深圳</option>
            <option value="gz">广州</option>
          </select></td>
    </tr>
    <tr>
        <td><input type="submit" value="提交"></td>
        <td><input type="reset" value="重置"></td>
    </tr>
    </table>
    </form> 
</body>
<style>
    td:nth-child(odd){
        text-align: right;
    }
</style>
</html>

# 2.1 JSP页面「中央商场」

# example.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>购物中心</title>
</head>
<body>
<p>输入您的名字,连接到中央商场。</p>
<form action="first.jsp" method="get">
<input type="text" name="name">
    <input type="submit" value=送出>
</form>
</body>
</html>

# first.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.*" %>
<%
    String name = request.getParameter("name");
    session.setAttribute("name", name);
%>
<html>
<head>
    <title>商场</title>
</head>
<body>
<p>这里是中央商场,请输入您购买的商品,连接到结账处。</p>
<form action="count.jsp" method="get">
    <input type="text" name="good">
    <input type="submit" value=发送>
</form>
</body>
</html>

# count.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.io.*,java.util.*" %>
<%
    String good = request.getParameter("good");
    String name = (String)session.getAttribute("name");
%>
<html>
<head>
    <title>结账处</title>
</head>
<body>
<p>这里是结账处。</p>
<p>顾客的姓名是:<% out.print(name); %></p>
<p>选购的商品是:<% out.print(good); %></p>
</body>
</html>

# 2.2 JSP页面「注册界面」

# reg.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户注册</title>
</head>
<body>
<form method="POST" action="reg.jsp">
    <table>
        <caption>
            用户注册
        </caption>
        <tr>
            <td>用户名</td>
            <td><input type="text" name="username"></td>
        </tr>
        <tr>
            <td>年龄</td>
            <td><input type="password" name="age"></td>
        </tr>

        <tr>
            <td>性别</td>
            <td>
                <input type="radio" name="gander" value="" checked><input type="radio" name="gander" value=""></td>
        </tr>
        <tr>
            <td><input type="submit" value="提交"></td>
            <td><input type="reset" name="重置"></td>
        </tr>
    </table>
</form>
</body>
</html>

# reg.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.io.*,java.util.*" %>
<%
    String name = request.getParameter("username");
    String age = request.getParameter("age");
    String gander = request.getParameter("gander");
%>
<html>
<head>
    <title>注册结果</title>
</head>
<body>
<%
    if(name.equals("admin")){
        out.println("欢迎您,管理员");
    }else{
        out.println("注册成功");
        out.println("姓名:"+name);
        out.println("年龄:"+age);
        out.println("性别:"+gander);
    }
%>
</body>
</html>

# 3. JDBC连接数据库

# index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.*" %>
<html>
<head>
    <title>连接数据库</title>
</head>
<body>
<center>
    <table border="2">
        <caption>查询数据库</caption>
        <tr>
            <td>ISBN</td>
            <td>书名</td>
            <td>版本</td>
            <td>出版时间</td>
            <td>价格</td>
        </tr>
        <%
            ResultSet rs = null;
            try {
                Class.forName("com.mysql.jdbc.Driver");  ////驱动程序名
                String url = "jdbc:mysql://localhost:3306/books";
                String username = "root";  //数据库用户名
                String password = "123456";  //数据库用户密码
                Connection conn = DriverManager.getConnection(url, username, password); //创建连接
                if (conn != null) {
                    String sql = "SELECT * FROM books;";  //查询语句
                    Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                    rs = stmt.executeQuery(sql);
                } else {
                    out.print("连接失败!");
                }
            } catch (e){}
            rs.absolute(1);
            while (!rs.isAfterLast()) {
        %>
        <tr>
            <td><%=rs.getString("number") %>
            </td>
            <td><%=rs.getString("name") %>
            </td>
            <td><%=rs.getString("model") %>
            </td>
            <td><%=rs.getString("maker") %>
            </td>
            <td><%=rs.getString("price") %>
            </td>
        </tr>
        <%
                rs.next();
            }
        %>
    </table>
</center>
</body>
</html>