欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

新手学Html之JSP——入门(一)

程序员文章站 2023-04-04 17:45:59
JSP基础语法 JSP注释 comment.jsp 1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3

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>

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>

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>

尽量不要使用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>

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>

scriptlet标签

此标签具有和<% %>一样的效果,更加美观一些,无强制要求

scriptlet_tag.jsp

1 <jsp:scriptlet>
2     string url = "www.mldnjava.cn";
3 </jsp:scriptlet>
4 <h2><%=url %></h2>

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>

错误页的设置

服务器端跳转

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>

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>

数据库连接操作

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    : 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');

将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>

包含指令

info.htm

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

info.jsp

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

info.inc

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

静态包含

先包含,再处理

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>

动态包含

先处理,再包含

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>

使用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>

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>

静态包含与动态包含的优劣之分

静态包含处理页 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>

动态包含处理页 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>

跳转指令

服务器跳转,页面地址未发生改变

不传递参数时

1 <jsp:forword page="{要包含的文件路径|<%=表达式 %>}"/>

传递参数时(中间不能有空格)

1 <jsp:forward>
2     <jsp:param name="参数名称" value="参数内容"/>
3 </jsp:forward>

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>

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>

实例操作:用户登录程序实现(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');

登录界面

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>

校验界面

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>

登陆成功页面

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>

登录失败页面

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>

退出页面

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>