新手学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 heretitle>
 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 heretitle>
 8 head>
 9 <body>
10     <%
11         //定义局部变量,编写语句
12         int x = 10;
13         String info = "www.mldnjava.cn";
14         out.println("

x="+x+"

"); 15 out.println("

info="+info+"

"); 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 heretitle>
 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 heretitle>
 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 heretitle>
 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标签

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

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 heretitle>
 8 head>
 9 <body>
10     
11     
12     
13     
14     
15     
16     
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

错误页的设置

服务器端跳转

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 heretitle>
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 heretitle>
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    : 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 目录中,重启服务器

使用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 heretitle>
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.htmfont>
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.incfont>
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 heretitle>
 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 heretitle>
 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 heretitle>
 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 heretitle>
 8 head>
 9 <body>
10 <%
11     int x = 100;
12 %>
13 <h1>include_demo04.jsp -- <%=x %>h1>
14 <%@include file="include.jsp" %>
15 
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 heretitle>
 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 heretitle>
 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 heretitle>
 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

 

你可能感兴趣的:(新手学Html之JSP基础语法——入门(二))