新手學Html之JSP基礎語法——入門(二)

JSP基礎語法

JSP註釋

comment.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <!-- 這個註釋客戶端能夠看見 -->
11     <%-- jsp中的註釋,客戶端沒法看見 --%>
12     <%
13         //java中的單行註釋,客戶端沒法看見
14         /*
15             java中的多行註釋,客戶端沒法看見
16         */
17     %>
18 </body>
19 </html>
View Code

Scriptlet

scriptlet_demo01.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <%
11         //定義局部變量,編寫語句
12         int x = 10;
13         String info = "www.mldnjava.cn";
14         out.println("<h2>x="+x+"</h2>");
15         out.println("<h2>info="+info+"</h2>");
16     %>
17 </body>
18 </html>
View Code

scriptlet_demo02.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <!-- 定義全局變量,方法,類 -->
11     <%! public static final String INFO = "www.mldnjava.cn"; %>
12     <!-- 用於輸出一個變量或一個具體的常量 -->
13     <%=1 %><br/>
14     <%=INFO %>
15 </body>
16 </html>
View Code

儘可能不要使用system.out.print();進行輸出javascript

input_table_value.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <form action="print_table.jsp" method="post">
11         <table border="1" width="100%">
12             <tr>
13                 <td>輸入表格的行數:</td>
14                 <td><input type="text" name="row"></td>
15             </tr>
16             <tr>
17                 <td>輸入表格的列數:</td>
18                 <td><input type="text" name="col"></td>
19             </tr>
20             <tr>
21                 <td>
22                     <input type="submit" value="顯示">
23                     <input type="reset" value="重置">
24                 </td>
25             </tr>
26         </table>
27     </form>
28 </body>
29 </html>
View Code

print_table.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <%
11         int rows = 0;
12         int cols = 0;
13         //讀取input_table_value.jsp   post的row和col,將之強制轉換爲int類型
14         try{
15             rows = Integer.parseInt(request.getParameter("row"));
16             cols = Integer.parseInt(request.getParameter("col"));
17         }catch(Exception e){}
18         if(rows>0&&cols>0){
19     %>
20             <table border="1" width="100%">
21             <%
22                 for(int x = 1;x <= rows; x++){
23             %>
24                 <tr>
25             <%
26                     for(int y = 1; y <= cols; y++){ 
27             %>
28                         <td> <%=x%> * <%=y%> = <%=(x * y)%></td>
29             <%
30                     }
31             %>
32                 </tr>
33             <%
34                 }
35             %>
36             </table>
37             <a href="input_table_value.jsp"><input type="button" value="返回"></a>
38     <%
39         }else{
40     %>
41             <%--輸入不符合時彈出對話框指示,並自動返回到輸入數值處 --%>
42             <script type="text/javascript" language="javascript">
43                 alert("輸入不合法!");
44                 /* alert(document.location === window.location);//true */
45                 //window.location.href="input_table_value.jsp";
46                 //document.location.href="input_table_value.jsp";
47                 //以上兩種好像等價,待探索
48                 window.document.location.href="input_table_value.jsp";
49             </script>
50     <%
51             }
52     %>
53 </body>
54 </html>
View Code

scriptlet標籤

此標籤具備和<% %>同樣的效果,更加美觀一些,無強制要求html

scriptlet_tag.jsp

1 <jsp:scriptlet>
2     String url = "www.MLDNJAVA.cn";
3 </jsp:scriptlet>
4 <h2><%=url %></h2>
View Code

page指令

設置頁面的MIME、文件編碼

page_demo01.jsp

 1 <%@ page language="java" contentType="application/msword; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <!-- pageEncoding是JSP文件自己的編碼,contentType是服務器發送給客戶端的內容編碼 -->
11     
12     <!--        txt     text/plain -->
13     <!--        doc     application/msword -->
14     <!--        png     image/png -->
15     <!--    jpg/jpeg    image/jpeg -->
16     <!--    htm/html    text/html-->
17     <table border="1">
18         <%
19             //指定文件下載後的保存名稱是mldn.doc
20             response.setHeader("Content-Disposition", "attachment;filename=mldn.doc");
21         %>
22         <tr><td>歡迎你們</td></tr>
23         <tr><td>歡迎你們!!</td></tr>
24         <tr><td>歡迎你們!!!</td></tr>
25     </table>
26 </body>
27 </html>
View Code

錯誤頁的設置

服務器端跳轉java

show_error.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ page errorPage="error.jsp" %>
 4 <%-- 出現錯誤將會跳轉到error.jsp --%>
 5  6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 7 <html>
 8 <head>
 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
10 <title>Insert title here</title>
11 </head>
12 <body>
13     <%
14         int result = 10 / 0;
15     %>
16     <%=result %>
17 </body>
18 </html>
View Code

error.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ page isErrorPage="true" %>
 4 <%-- 表示出現錯誤該頁面能夠處理錯誤 --%>
 5 <% response.setStatus(200); %>
 6 <%-- 設置了200的HTTP狀態碼,表示本頁沒有錯誤,防止tomcat也認爲本頁出現了錯誤,從而沒法顯示 --%>
 7 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 8 <html>
 9 <head>
10 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
11 <title>Insert title here</title>
12 </head>
13 <body>
14     <h1>程序出現了錯誤!</h1>
15 </body>
16 </html>
View Code

數據庫鏈接操做

page指令使用import導入所須要的Java開發包mysql

mldn.sql

 1 /*
 2 Navicat MySQL Data Transfer
 3  4 Source Server         : myproject
 5 Source Server Version : 50562
 6 Source Host           : localhost:3306
 7 Source Database       : mldn
 8  9 Target Server Type    : MYSQL
10 Target Server Version : 50562
11 File Encoding         : 65001
12 13 Date: 2019-04-27 02:23:48
14 */
15 16 SET FOREIGN_KEY_CHECKS=0;
17 18 -- ----------------------------
19 -- Table structure for emp
20 -- ----------------------------
21 DROP TABLE IF EXISTS `emp`;
22 CREATE TABLE `emp` (
23   `empno` int(4) NOT NULL,
24   `ename` varchar(10) DEFAULT NULL,
25   `job` varchar(9) DEFAULT NULL,
26   `hiredate` date DEFAULT NULL,
27   `sal` float(7,2) DEFAULT NULL,
28   PRIMARY KEY (`empno`)
29 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
30 31 -- ----------------------------
32 -- Records of emp
33 -- ----------------------------
34 INSERT INTO `emp` VALUES ('6060', '李興華', '經理', '2001-09-16', '2000.30');
35 INSERT INTO `emp` VALUES ('7369', '董鳴楠', '銷售', '2003-10-09', '1500.90');
36 INSERT INTO `emp` VALUES ('7698', '張惠', '銷售', '2005-03-12', '800.00');
37 INSERT INTO `emp` VALUES ('7762', '劉明', '銷售', '2005-03-09', '1000.00');
38 INSERT INTO `emp` VALUES ('7782', '楊軍', '分析員', '2005-01-12', '2500.00');
39 INSERT INTO `emp` VALUES ('7839', '王月', '經理', '2006-09-01', '2500.00');
40 INSERT INTO `emp` VALUES ('8964', '李祺', '分析員', '2003-10-01', '3000.00');
View Code

將mysql的驅動"mysql-connector-java-5.1.47-bin.jar"複製到Tomcat\lib 目錄中,重啓服務器sql

使用JSP列出emp表數據數據庫

驅動程序使用 com.mysql.jdbc.Driverapache

list_emp.jsptomcat

 1 <%@page import="org.apache.tomcat.dbcp.dbcp2.PStmtKey"%>
 2 <%@page import="com.sun.crypto.provider.RSACipher"%>
 3 <%@ page language="java" contentType="text/html; charset=UTF-8"
 4     pageEncoding="UTF-8"%>
 5 <%@ page import="java.sql.*" %>
 6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 7 <html>
 8 <head>
 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
10 <title>Insert title here</title>
11 </head>
12 <body>
13 <%!
14     //定義數據庫驅動程序
15     public static final String DBDRIVER = "com.mysql.jdbc.Driver";
16     //數據庫鏈接地址
17     public static final String DBURL = "jdbc:mysql://localhost:3306/mldn";
18     public static final String DBUSER = "root";
19     public static final String DBPASS = "2580";
20 %>
21 <%
22     Connection conn = null;                 //聲明數據庫鏈接對象
23     PreparedStatement pstmt = null;         //聲明數據庫操做
24     ResultSet rs = null;                    //聲明數據庫結果集
25 %>
26 <%
27 try{                                        //數據庫操做中會出現異常,因此要使用try...catch處理
28     Class.forName(DBDRIVER);                //數據庫驅動程序加載
29     conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);//取得數據庫鏈接
30     String sql = "SELECT empno,ename,job,sal,hiredate FROM emp";
31     pstmt = conn.prepareStatement(sql);     //實例化prepareStatement對象
32     rs = pstmt.executeQuery();              //執行查詢操做
33 %>
34 <center>
35     <table border="1" width="80%">
36         <tr>                                <!-- 輸出表格的行顯示 -->
37             <td>僱員編號</td>                   <!-- 輸出表格的行顯示信息 -->
38             <td>僱員姓名</td>
39             <td>僱員工做</td>
40             <td>僱員工資</td>
41             <td>僱傭日期</td>
42         </tr>
43 <%
44     while(rs.next()){
45         int empno = rs.getInt(1);           //循環emp表中的行記錄
46         String ename = rs.getString(2);     //取出僱員編號
47         String job = rs.getString(3);       //取出僱員姓名
48         float sal = rs.getFloat(4);         //取出僱員工做
49         java.util.Date date = rs.getDate(5);//取出僱傭日期
50 %>
51         <tr>                                <!-- 循環輸出僱員的信息 -->
52             <td><%=empno %></td>
53             <td><%=ename %></td>
54             <td><%=job %></td>
55             <td><%=sal %></td>
56             <td><%=date %></td>
57         </tr>
58 <%
59         }
60 %>
61     </table>
62 </center>
63 <%
64 }catch(Exception e){                        //異常處理
65     System.out.println(e);                  //向Tomcat中打印
66 }finally{
67     rs.close();
68     pstmt.close();
69     conn.close();
70 }
71 %>
72 </body>
73 </html>
View Code

包含指令

info.htm

1 <h2>
2     <font color="red">info.htm</font>
3 </h2>
View Code

info.jsp

1 <h2>
2     <font color="green"><%="info.jsp" %></font>
3 </h2>
View Code

info.inc

1 <h2>
2     <font color="blue">info.inc</font>
3 </h2>
View Code

靜態包含

先包含,再處理服務器

include_demo01.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <h1>靜態包含操做</h1>
11     <%@include file="info.htm" %>
12     <%@include file="info.jsp" %>
13     <%@include file="info.inc" %>
14 </body>
15 </html>
View Code

動態包含

先處理,再包含session

include_demo02.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <h1>靜態包含操做</h1>
11     <jsp:include page="info.htm"/>      <!-- 此處爲標籤指令,必須完結 -->
12     <jsp:include page="info.jsp"/>      <!-- 此處爲標籤指令,必須完結 -->
13     <jsp:include page="info.inc"/>      <!-- 此處爲標籤指令,必須完結 -->
14 </body>
15 </html>
View Code

使用request.getParameter()方法進行參數的傳遞

receive_param.jsp

1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2     pageEncoding="UTF-8"%>
3 <h1>參數一:<%=request.getParameter("name") %></h1>
4 <h1>參數二:<%=request.getParameter("info") %></h1>
View Code

include_demo03.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <%
11     String username="LiXinHua";     //定義一個變量
12 %>
13     <h1>動態包含並傳遞參數</h1>
14     <jsp:include page="receive_param.jsp">
15         <jsp:param value="<%=username %>" name="name"/>
16         <jsp:param value="www.mldnjava.cn" name="info"/>
17     </jsp:include>          <!-- 此處爲標籤完結指令,必須完結 -->
18 </body>
19 </html>
View Code

靜態包含與動態包含的優劣之分

靜態包含處理頁 include_demo04.jsp(錯誤的頁面

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <%
11     int x = 100;
12 %>
13 <h1>include_demo04.jsp -- <%=x %></h1>
14 <%@include file="include.jsp" %>
15 <!-- 運行出現500錯誤,由於靜態包含時,先將所有的內容包含到一塊兒,而後再編譯,致使了x的屢次定義出錯 -->
16 </body>
17 </html>
View Code

動態包含處理頁 include_demo05.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <%
11     int x = 100;
12 %>
13 <h1>include_demo05.jsp -- <%=x %></h1>
14 <jsp:include page="include.jsp"></jsp:include>
15 </body>
16 </html>
View Code

跳轉指令

服務器跳轉,頁面地址未發生改變

不傳遞參數時

1 <jsp:forword page="{要包含的文件路徑|<%=表達式 %>}"/>
View Code

傳遞參數時(中間不能有空格)

1 <jsp:forward>
2     <jsp:param name="參數名稱" value="參數內容"/>
3 </jsp:forward>
View Code

forward_demo01.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <%
11     String username = "LiXinHua";
12 %>
13 <jsp:forward page="forward_demo02.jsp">
14     <jsp:param value="<%=username %>" name="name"/>
15     <jsp:param value="www.MLDNJAVA.cn" name="info"/>
16 </jsp:forward>
17 </body>
18 </html>
View Code

forward_demo02.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <h1>這是跳轉以後的頁面</h1>
11 <h2>參數一:<%=request.getParameter("name") %></h2>
12 <h2>參數二:<%=request.getParameter("info") %></h2>
13 </body>
14 </html>
View Code

實例操做:用戶登陸程序實現(JSP+JDBC實現)

建立數據庫表

 1 /*
 2 Navicat MySQL Data Transfer
 3  4 Source Server         : myproject
 5 Source Server Version : 50562
 6 Source Host           : localhost:3306
 7 Source Database       : mldn
 8  9 Target Server Type    : MYSQL
10 Target Server Version : 50562
11 File Encoding         : 65001
12 13 Date: 2019-04-27 03:28:48
14 */
15 16 SET FOREIGN_KEY_CHECKS=0;
17 18 -- ----------------------------
19 -- Table structure for user
20 -- ----------------------------
21 DROP TABLE IF EXISTS `user`;
22 CREATE TABLE `user` (
23   `userid` varchar(30) NOT NULL,
24   `name` varchar(30) NOT NULL,
25   `password` varchar(32) NOT NULL,
26   PRIMARY KEY (`userid`)
27 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
28 29 -- ----------------------------
30 -- Records of user
31 -- ----------------------------
32 INSERT INTO `user` VALUES ('admin', 'administrator', 'admin');
View Code

登陸界面

login.html

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="UTF-8">
 5 <title>請登陸...</title>
 6 </head>
 7 <body>
 8 <center>
 9     <h1>登陸操做</h1>
10     <hr>
11     <form action="login_check.jsp" method="post">
12         <table border="1">
13             <tr>
14                 <td colspan="2"><center>用戶登陸</center></td>
15             </tr>
16             <tr>
17                 <td>登陸ID:</td>
18                 <td><input type="text" name="id"></td>
19             </tr>
20             <tr>
21                 <td>登陸密碼:</td>
22                 <td><input type="password" name="password"></td>
23             </tr>
24             <tr>
25                 <td colspan="2">
26                     <input type="submit" value="登陸">
27                     <input type="reset" value="重置">
28                 </td>
29             </tr>
30         </table>
31     </form>
32     <hr>
33 </center>
34 </body>
35 </html>
View Code

校驗界面

login_check.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ page import="java.sql.*" %>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>登陸校驗</title>
 9 </head>
10 <body>
11 <%!
12     //定義數據庫驅動程序
13     public static final String DBDRIVER = "com.mysql.jdbc.Driver";
14     //數據庫鏈接地址
15     public static final String DBURL = "jdbc:mysql://localhost:3306/mldn";
16     public static final String DBUSER = "root";
17     public static final String DBPASS = "2580";
18 %>
19 <%
20     Connection conn = null;                                 //聲明數據庫鏈接對象
21     PreparedStatement pstmt = null;                         //聲明數據庫操做
22     ResultSet rs = null;                                    //聲明數據庫結果集
23     boolean flag = false;                                   //標誌位
24     String name = null;                                     //接收用戶的真實姓名
25 %>
26 <%  //JDBC會拋出異常,使用try...catch處理
27 try{
28     Class.forName(DBDRIVER);                                //加載驅動程序
29     conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);//取得數據庫鏈接
30     //編寫要使用的SQL語句,驗證用戶id和密碼,若是正確,則取出真實姓名
31     String sql = "SELECT name FROM user WHERE userid=? AND password=?";
32     pstmt = conn.prepareStatement(sql);                     //實例化prepareStatement對象
33     pstmt.setString(1, request.getParameter("id"));         //設置查詢所須要的內容
34     pstmt.setString(2, request.getParameter("password"));   //設置查詢所須要的內容
35     rs = pstmt.executeQuery();                              //執行查詢操做
36     if(rs.next()){                                          //若是能夠查詢到,則表示合法用戶
37         name = rs.getString(1);                             //取出真實姓名
38         flag = true;                                        //修改標誌位,若是爲true,表示登陸成功
39     }
40 }catch(Exception e){
41     System.out.println(e);
42 }finally{
43     try{                                                    //關閉操做會拋出異常,使用try...catch處理
44         rs.close();                                         //關閉查詢對象
45         pstmt.close();                                      //關閉操做對象
46         conn.close();                                       //關閉數據庫鏈接
47     }catch(Exception e){}
48 }
49 %>
50 <%
51     if(flag){                                               //登陸成功,跳轉到成功頁
52 %>
53             <%-- <jsp:forward page="login_success.jsp"> --%>            <!-- 執行跳轉操做 -->
54             <%--    <jsp:param value="<%=name %>" name="uname"/> --%>
55             <%-- </jsp:forward> --%>
56 <%
57             response.setHeader("refresh", "3;URL=login_success.jsp");       //定時跳轉
58             session.setAttribute("uname", name);
59 %>
60             <h3>用戶若是登陸成功,三秒後跳轉到歡迎頁!</h3>
61             <h3>若是沒用跳轉,請按<a href="login_success.jsp">這裏</a></h3>
62 <%
63     }else{//登錄失敗,跳轉到失敗頁
64 %>
65 <jsp:forward page="login_failure.jsp"></jsp:forward><!-- 執行跳轉操做 -->
66 <%
67     }
68 %>
69 </body>
70 </html>
View Code

登錄成功頁面

login_success.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>歡迎您,<%=session.getAttribute("uname")%></title>
 8 </head>
 9 <body>
10 <center>
11 <%
12     if(session.getAttribute("uname") != null){          //已經設置過屬性,因此不爲空
13 %>
14         <h1>登陸操做</h1>
15         <hr>
16         <h2>登陸成功</h2>
17         <h2>歡迎<%=session.getAttribute("uname")%>光臨本系統,<a href="logout.jsp">註銷</a>!</h2>
18 <%
19     }else{          //非法用戶,沒有登錄過,session中沒有userid的存在
20 %>
21         <h3>請先進行系統的<a href="login.html">登陸</a>!</h3>
22 <%
23     }
24 %>
25     
26     <%-- <h2>歡迎<font color="red"><%=request.getParameter("uname") %></font>光臨!</h2> --%>
27 </center>
28 </body>
29 </html>
View Code

登陸失敗頁面

login_failure.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>登錄失敗</title>
 8 </head>
 9 <body>
10 <center>
11     <h1>登陸操做</h1>
12     <h2>登陸失敗,請從新<a href="login.html">登陸</a></h2>
13 </center>
14 </body>
15 </html>
View Code

退出頁面

logout.jsp

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <title>已退出系統</title>
 7 </head>
 8 <body>
 9 10 <h3>親愛的<%=session.getAttribute("uname")%>,您已成功退出本系統,三秒後跳轉回登陸界面!</h3>
11 <h3>若果沒有跳轉,請按<a href="login.html">這裏</a></h3>
12 13 <%
14     response.setHeader("refresh", "3;URL=login.html");      //定時跳轉
15     session.invalidate();                                   //註銷,session清空
16 %>
17 18 </body>
19 </html>
View Code
相關文章
相關標籤/搜索