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();進行輸出
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="https://www.cnblogs.com/dream-by-dream/p/input_table_value.jsp";46 //document.location.href="https://www.cnblogs.com/dream-by-dream/p/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標簽
此標簽具有和<% %>一樣的效果,更加美觀一些,無強制要求
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.doc20 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
錯誤頁的設定
服務器端跳轉
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開發包
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 : MYSQL10 Target Server Version : 5056211 File Encoding : 6500112 ?13 Date: 2019-04-27 02:23:4814 */15 ?16 SET FOREIGN_KEY_CHECKS=0;17 ?18 -- ----------------------------19 -- Table structure for emp20 -- ----------------------------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 emp33 -- ----------------------------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 目錄中,重啟服務器
使用JSP列出emp表資料
驅動程式使用
com.mysql.jdbc.Driver
list_emp.jsp

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
動態包含
先處理,再包含
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 : MYSQL10 Target Server Version : 5056211 File Encoding : 6500112 ?13 Date: 2019-04-27 03:28:4814 */15 ?16 SET FOREIGN_KEY_CHECKS=0;17 ?18 -- ----------------------------19 -- Table structure for user20 -- ----------------------------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 user31 -- ----------------------------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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/34037.html
標籤:HTML5

