javaweb學習第6篇 - 學會使用JSP+Servlet+Javabean模式進行簡單的業務系統開發

目標:學會使用JSP+Servlet+Javabean模式進行簡單的業務系統開發

新建兩張父子表:1、學生表 2、學生髮表的論文表

實現學生基本信息增刪改查,針對某一個學生、對發表的論文進行增刪改查,

說明:不允許使用其它框架,純粹使用Model1(JSP+Servlet+Javabean)來進行開發
 

1.創建index.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<%@ page language="java" contentType="text/html; charset=utf-8"

    pageEncoding="utf-8" import="java.sql.*"%>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<style type = "text/css">

    a:link,a:visited{

        color:#0044DD;

        text:-decoration:none;}

    table th{

        color:#2467fa;

        background:#dadada;

        font-size:16px;

        border:1px solid #fffff;

    }

    table td{

        background:#dadada;

        text-align:center;

        border-align:1px solid #ffffff;

        }

  </style>

<title>主界面</title>

</head>

<body>

<div style="width:700px;margin:100px auto;text-align:center;line-height:100px;background:#ffffaa">

        <h1 style="font-family:仿宋">賬號信息管理系統</h1>

   

    <table align="center" width="400px" border="1">

        <tr><th>用戶名</th><th>密碼</th><th>年齡</th><th>操作</th></tr>

        <%

            Class.forName("com.mysql.cj.jdbc.Driver");

            //Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/user","root","root");

            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?characterEncoding=utf-8&serverTimezone=GMT%2B8","root","laotu123");

            String sql = "select * from userinfo";

            Statement stmt = con.createStatement();

            ResultSet rs = stmt.executeQuery(sql);

            while(rs.next()) {

            %>

                <tr><td><%=rs.getString("name") %></td>

                <td><%=rs.getString("password") %></td>

                <td><%= rs.getInt("age") %></td>

                <td><button type = "button" onclick="alert('請先登陸!')">修改</button>

                    <button type = "button" onclick="alert('請先登陸!')">刪除</button>

                </td>

                </tr>

            <%

                }

                rs.close();

                stmt.close();

                con.close();

               

             %>

    </table>

    <br>

    <button type = "button" align = "center" onclick="window.location.href='login.jsp'" ><h4>登陸</button>

    <br><br>

    </div>

</body>

</html>

 

2.創建login.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<%@ page language="java" contentType="text/html; charset=utf-8"

    pageEncoding="utf-8"%>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<style type = "text/css">

    a:link,a:visited{

        color:#0044DD;

        text:-decoration:none;}

    table th{

        color:#2467fa;

        background:#dadada;

        font-size:16px;

        border:1px solid #fffff;

    }

    table td{

        background:#dadada;

        text-align:center;

        border-align:1px solid #ffffff;

        }

  </style>

<title>登陸界面</title>

</head>

<body>

    <div style="width:700px;margin:100px auto;text-align:center;line-height:100px;background:#ffffaa">

        <h1 style="font-family:仿宋">賬號信息管理系統</h1>

        <form name = "form1" action = "login_do.jsp" method="POST">

            <table align="center" width="300px" border="1">

            <tr><th>用戶名</th>

                <td><input type = "text" name = "username"></td>

            </tr>

            <tr><th>密碼</th>

                <td><input type = "password" name = "password"></td>

            </tr>

            <tr><th colspan = "2" align = "center"><input type = "reset" value = "重置" >

                <input type = "submit" value = "登陸"></th>

            </tr>

            </table>

        </form>

        <br>

    </div>

</body>

</html>

 

3.創建login_success.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<%@ page language="java" contentType="text/html; charset=utf-8"

    pageEncoding="utf-8" import="java.sql.*" %>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<style type = "text/css">

    a:link,a:visited{

        color:#0044DD;

        text:-decoration:none;}

    table th{

        color:#2467fa;

        background:#dadada;

        font-size:16px;

        border:1px solid #fffff;

    }

    table td{

        background:#dadada;

        text-align:center;

        border-align:1px solid #ffffff;

        }

  </style>

 

<title>登陸成功</title>

</head>

<body>

<%String name1 = (String)session.getAttribute("username");

    if(name1 == null) {

        String mgs = "您尚未登陸,請先登陸,3秒後跳轉登陸界面!";%>

        <h1  align="center" />

            <%=mgs %>

    <%

        response.setHeader("Refresh", "3;login.jsp");

    }

    else{

    %>

    <div style="width:700px;margin:100px auto;text-align:center;line-height:100px;background:#ffffaa">

        <h1 style="font-family:仿宋">賬號信息管理系統</h1>

        <h2 style="font-family:仿宋"><%=name1 %>,歡迎您!</h2>

   

    <table align="center" width="400px" border="1">

        <tr><th>用戶名</th><th>密碼</th><th>年齡</th><th>操作</th></tr>

        <%

            Class.forName("com.mysql.cj.jdbc.Driver");

            //Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/user","root","root");

            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?characterEncoding=utf-8&serverTimezone=GMT%2B8","root","laotu123");

            String sql = "select * from userinfo";

            Statement stmt = con.createStatement();

            ResultSet rs = stmt.executeQuery(sql);

            while(rs.next()) {

            %>

                <tr><td><%=rs.getString("name") %></td>

                <td><%=rs.getString("password") %></td>

                <td><%= rs.getInt("age") %></td>

                <td><input type="button" onclick="window.location.href='edit.jsp?name=<%=rs.getString("name") %>'" value="修改">

                    <a href="del.jsp?name=<%=rs.getString("name") %>" onclick="return confirm('確定刪除嗎?')"><input type = "button"  value="刪除"></a>

                </td>

                </tr>

            <%

                }

                rs.close();

                stmt.close();

                con.close();

       

             %>

    </table>

    <br>

    <table align="center" >

        <tr>

           

            <td><input type="button" onclick="window.location.href='query_paper.jsp'" value="查詢論文"></td>

            <td> &nbsp;</td>

            <td><input type="button" onclick="window.location.href='add.jsp'" value="增加用戶"></td>

            <td> &nbsp;</td>          

            <td><input type="button" onclick="window.location.href='query.jsp'" value="查詢用戶"></td>

            <td> &nbsp;</td>

            <td><a href="login_out.jsp" onclick="return confirm('確定退出嗎?')"><input type="button"  value="退出登陸"></a></td>

        </tr>

    </table>

    <br>

    </div>

   

    <%} %>

</body>

</html>

 

  1. 創建login_out.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<%@ page language="java" contentType="text/html; charset=utf-8"

    pageEncoding="utf-8"%>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>退出登陸處理界面</title>

</head>

<body>

    <%session.setAttribute("username",null);

    String mgs = "退出成功,3秒後跳轉到主界面!";%>

    <h1  align="center" /><%=mgs %>

    <% response.setHeader("Refresh", "3;index.jsp");%>

   

</body>

</html>

 

  1. 創建login_do.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<%@ page language="java" contentType="text/html; charset=utf-8"

    pageEncoding="utf-8" import="java.sql.*"%>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>登陸驗證頁面</title>

</head>

<body>

 <%

    request.setCharacterEncoding("UTF-8");

 

    String name = request.getParameter("username");

    String password = request.getParameter("password");

    String sql = "select count(*) from userinfo where name = ? and password = ?";

    Class.forName("com.mysql.cj.jdbc.Driver");   

    //Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/user","root","root");

        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?characterEncoding=utf-8&serverTimezone=GMT%2B8","root","laotu123");

    PreparedStatement pstmt = con.prepareStatement(sql);

    pstmt.setString(1, name);

    pstmt.setString(2,password);

    ResultSet rs = pstmt.executeQuery();

    String a = "1";

    while(rs.next()) {

color:#3f7f7f;">body>

 <%

    request.setCharacterEncoding("UTF-8");

 

    String name = request.getParameter("username");

    String password = request.getParameter("password");

    String sql = "select count(*) from userinfo where name = ? and password = ?";

    Class.forName("com.mysql.cj.jdbc.Driver");   

    //Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/user","root","root");

        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?characterEncoding=utf-8&serverTimezone=GMT%2B8","root","laotu123");

    PreparedStatement pstmt = con.prepareStatement(sql);

    pstmt.setString(1, name);

    pstmt.setString(2,password);

    ResultSet rs = pstmt.executeQuery();

    String a = "1";

    while(rs.next()) {

    if(a.equals(rs.getString("count(*)"))) {

        session.setAttribute("username", name);

    %>

    <jsp:forward page="login_success.jsp"></jsp:forward>

    <% }

    else {

        String mgs = "登錄失敗,3秒後跳轉到主界面!";%>

        <h1  align="center" />

            <%=mgs %>

    <%

        response.setHeader("Refresh", "3;index.jsp");

    }

    }

     rs.close();

    pstmt.close();

    con.close();

    %>

 

 

</body>

</html>

 

  1. 創建query.jsp

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<%@ page language="java" contentType="text/html; charset=utf-8"

    pageEncoding="utf-8"%>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<style type = "text/css">

        Class.forName("com.mysql.cj.jdbc.Driver");   

    //Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/user","root","root");

        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?characterEncoding=utf-8&serverTimezone=GMT%2B8","root","laotu123");

    PreparedStatement pstmt = con.prepareStatement(sql);

    pstmt.setString(1, name);

    pstmt.setString(2,password);

    ResultSet rs = pstmt.executeQuery();

    String a = "1";

    while(rs.next()) {

    if(a.equals(rs.getString("count(*)"))) {

        session.setAttribute("username", name);

    %>

    <jsp:forward page="login_success.jsp"></jsp:forward>

    <% }

    else {

        String mgs = "登錄失敗,3秒後跳轉到主界面!";%>

        <h1  align="center" />

            <%=mgs %>

    <%

        response.setHeader("Refresh", "3;index.jsp");

    }

    }

     rs.close();

    pstmt.close();

    con.close();

    %>

 

 

</body>

</html>

 

  1. 創建query.jsp

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<%@ page language="java" contentType="text/html; charset=utf-8"

    pageEncoding="utf-8"%>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<style type = "text/css">

    a:link,a:visited{

        color"laotu123");

    PreparedStatement pstmt = con.prepareStatement(sql);

    pstmt.setString(1, name);

    pstmt.setString(2,password);

    ResultSet rs = pstmt.executeQuery();

    String a = "1";

相關文章
相關標籤/搜索