JSP页面的组成
JSP页面是由静态内容,指令,表达式,声明,注释。。等元素构成
JSP页面注释
<!--这个是HTML注释(客户端可以看见源代码)-->
<%--这个是jsp注释(客户端不能看见源代码)--%>
静态内容
JSP指令元素
jsp指令一般是以<%@ 开始 以%>结束
<%@ page language="java" import="java.util.*,java.text.*"
contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>输出当前日期</title>
</head>
<!-- 这是HTML注释(客户端可以看到源代码)-->
<%-- 这是JSP注释 (客户端不可以看到源代码) --%>
<body>
你好,今天是
<%
//使用预定格式将日期转换为字符串
SimpleDateFormat formater = new SimpleDateFormat("yyyy年MM月dd日");
String strCurrentTime = formater.format(new Date());
%>
<%= strCurrentTime %>
<%! String declare = "this is declartion"; %>
<%= declare %>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<%!
public boolean findYear(int year){
boolean flag=false;
if((year%4==0 && year%100!=0)|| year%400==0 ){
flag=true;
}
return flag;
}
%>
<%
int count=0;
for(int i=2000;i<=2013;i++){
if(findYear(i)){
count++;
}
}
%>
从2000年到2013年共有<%=count%>个润年
</body>
</html>
JSP中的九大内置对象
1.Out对象
向浏览器中输出数据使用
常用的方法是print()
<%
out.print("hello,jsp");
%>
1.request对象(请求对象)
常用的方法
案例:注册页面请求信息
<%@ page language="java" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>用户注册</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<FORM action="info.jsp" method="post" name="myform">
<TABLE border="0" cellpadding="0" cellspacing="0" align="center" width="530">
<TR>
<TD height="108" colspan="2"><IMG src="images/top.jpg"></TD>
</TR>
<TR>
<TD width="107" height="36">用户名:</TD>
<TD width="524"><INPUT name="txtUser" type="text" maxlength="16">只能输入字母或数字,4-16个字符</TD>
</TR>
<TR>
<TD width="107" height="36">密码:</TD>
<TD width="524"><INPUT name="txtPass" type="password">密码长度6-12位</TD>
</TR>
<TR>
<TD width="107" height="36">确认密码:</TD>
<TD width="524"><INPUT name="txtRPass" type="password"></TD>
</TR>
<TR>
<TD width="107" height="36">性别:</TD>
<TD width="524">
<INPUT name="gen" type="radio" value="男" checked>男
<INPUT name="gen" type="radio" value="女" class="input">女
</TD>
</TR>
<TR>
<TD width="117" height="36">电子邮件地址:</TD>
<TD width="524"><INPUT name="txtEmail" type="text">
输入正确的Email地址</TD>
</TR>
<TR>
<TD width="107" height="36">出生日期:</TD>
<TD width="524">
<INPUT name="year" id="year" size="4" maxlength="4" > 年
<SELECT name="month" >
<OPTION value=1>一月</OPTION>
<OPTION value=2>二月</OPTION>
<OPTION value=3>三月</OPTION>
<OPTION value=4>四月</OPTION>
<OPTION value=5>五月</OPTION>
<OPTION value=6>六月</OPTION>
<OPTION value=7>七月</OPTION>
<OPTION value=8>八月</OPTION>
<OPTION value=9>九月</OPTION>
<OPTION value=10>十月</OPTION>
<OPTION value=11>十一月</OPTION>
<OPTION value=12>十二月 </OPTION>
</SELECT> 月
<SELECT name="day" >
<OPTION value=1>1</OPTION><OPTION value=2>2</OPTION><OPTION value=3>3</OPTION><OPTION value=4>4</OPTION>
<OPTION value=5>5</OPTION><OPTION value=6>6</OPTION><OPTION value=7>7</OPTION><OPTION value=8>8</OPTION>
<OPTION value=9>9</OPTION><OPTION value=10>10</OPTION><OPTION value=11>11</OPTION><OPTION value=12>12 </OPTION>
<OPTION value=13>13</OPTION><OPTION value=14>14</OPTION><OPTION value=15>15</OPTION><OPTION value=16>16</OPTION>
<OPTION value=17>17</OPTION><OPTION value=18>18</OPTION><OPTION value=19>19</OPTION><OPTION value=20>20</OPTION>
<OPTION value=21>21</OPTION><OPTION value=22>22</OPTION><OPTION value=23>23</OPTION><OPTION value=24>24</OPTION>
<OPTION value=25>25</OPTION><OPTION value=26>26</OPTION><OPTION value=27>27</OPTION><OPTION value=28>28</OPTION>
<OPTION value=29>29</OPTION><OPTION value=30>30</OPTION><OPTION value=7>31</OPTION>
</SELECT> 日
</TD>
</TR>
<TR><TD colspan="2" align="center">
<INPUT type="submit" value="同意以下协议条款并提交">
</TD></TR>
<TR><TD colspan="2">
<TEXTAREA cols="" rows="" readonly="readonly" style="width:480px;height:110px;font-size:12px;color:#666">
一、总则
1.1 用户应当同意本协议的条款并按照页面上的提示完成全部的注册程序。用户在进行注册程序过程中点击"同意"按钮即表示用户与百度公司达成协议,完全接受本协议项下的全部条款。
1.2 用户注册成功后,百度将给予每个用户一个用户帐号及相应的密码,该用户帐号和密码由用户负责保管;用户应当对以其用户帐号进行的所有活动和事件负法律责任。
1.3 用户可以使用百度各个频道单项服务,当用户使用百度各单项服务时,用户的使用行为视为其对该单项服务的服务条款以及百度在该单项服务中发出的各类公告的同意。
1.4 百度会员服务协议以及各个频道单项服务条款和公告可由百度公司随时更新,且无需另行通知。您在使用相关服务时,应关注并遵守其所适用的相关条款。
您在使用百度提供的各项服务之前,应仔细阅读本服务协议。如您不同意本服务协议及/或随时对其的修改,您可以主动取消百度提供的服务;您一旦使用百度服务,即视为您已了解并完全同意本服务协议各项内容,包括百度对服务协议随时所做的任何修改,并成为百度用户。
</TEXTAREA>
</TD>
</TR>
</TABLE>
</FORM>
</body>
</HTML>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>注册信息</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("txtUser");
String gender = request.getParameter("gen");
String email = request.getParameter("txtEmail");
String year = request.getParameter("year");
String month = request.getParameter("month");
String day = request.getParameter("day");
%>
<table>
<tr>
<td>姓名</td><td><%=name %></td>
</tr>
<tr>
<td>性别</td><td><%=gender%></td>
</tr>
<tr>
<td>邮件地址</td><td><%=email %></td>
</tr>
<tr>
<td>出生日期</td><td><%=year %>-<%=month %>-<%=day %></td>
</tr>
</table>
</body>
</html>
3.response对象(响应对象)
方法:
1.void addCookie(Cookie cookie); 向客户端添加cookie
2.void setContentType(String type);设置HTTP响应的contentType类型
3.void setcharcterEncoding(String charset) 设置响应的字符编码类型
4.void sendRedirect(String location) 将请求重新定位到一个新的url中去
1.转发
请求一次
地址栏不显示第二次转发的地址由request完成
可以就收到数据
2.重定向
请求两次
地址跳转
接受不到数据
页面转发
<%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
<title>登录处理页面</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("userName");
String pwd = request.getParameter("pwd");
if("sa".equals(name)&&"sa".equals(pwd)){
request.getRequestDispatcher("welcome.jsp").forward(request,response);
}
%>
</body>
</html>
页面重定向
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>用户登录</title>
</head>
<body>
<form name="form1" method=post action="control.jsp">
用户名:<input type="text" name="userName">
密码:<input type="password" name="pwd">
<input type="submit" value="登录">
</form>
</body>
</html>
<%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
<title>登录处理页面</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("userName");
String pwd = request.getParameter("pwd");
if("sa".equals(name)&&"sa".equals(pwd)){
response.sendRedirect("welcome.jsp");
}
%>
</body>
</html>
<%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
<title>欢迎页面</title>
</head>
<body>
欢迎进入本页面!
</body>
</html>
查询字符串的应用
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>选择喜欢的颜色</title>
</head>
<body>
请从以下7种颜色中,选择你最喜欢的颜色<br/>
<a href="color.jsp?color=赤色">赤色</a>
<a href="color.jsp?color=橙色">橙色</a>
<a href="color.jsp?color=黄色">黄色</a>
<a href="color.jsp?color=绿色">绿色</a>
<a href="color.jsp?color=青色">青色</a>
<a href="color.jsp?color=蓝色">蓝色</a>
<a href="color.jsp?color=紫色">紫色</a>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>我喜欢的颜色</title>
</head>
<body>
<%
//已在Tomcat中设置字符集为“UTF-8”
String color = request.getParameter("color");
%>
你最喜欢的颜色是:<%=color%>
</body>
</html>
4.session对象(会话)
用来保存用户的信息,在用户退出浏览器之前session对象一直保存
session与窗口之间的关系
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>获取sessonId</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
out.print("sessionid:"+session.getId());
out.print("<br/>");
%>
<a href="son.jsp">子窗口</a>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>子窗口获取sessonId</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
out.print("sessionid:"+session.getId());
out.print("<br/>");
%>
子窗口
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>使用session</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
session.setAttribute("test","hello");
response.sendRedirect("getSessonId.jsp");
%>
</body>
</html>
使用session实现访问控制
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>用户登录</title>
</head>
<body>
<form name="form1" method="post" action="doLogin.jsp">
用户名:
<input type="text" name="userName">
密码:
<input type="password" name="pwd">
<input type="submit" value="登录">
</form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>登录处理页面</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
//获取请求数据,并去除空格
String name = request.getParameter("userName").trim();
String pwd = request.getParameter("pwd").trim();
boolean valid = false; //判断用户验证是否成功
if("admin".equals(name)&&"admin".equals(pwd)){
valid = true;
//设置用户登录信息
session.setAttribute("login", name);
//设置session过期时间
session.setMaxInactiveInterval(10*60);
}
if (valid) {
request.getRequestDispatcher("admin.jsp")
.forward(request,response);
} else {
response.sendRedirect("index.jsp");
}
%>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>管理员</title>
</head>
<body>
<%
String login = (String) session.getAttribute("login");
if (login == null){
response.sendRedirect("index.jsp");
return;
}
%>
您好,欢迎您!
</body>
</html>
include指令
解决代码陈宇的问题,可以节省代码
声明一个空的jsp写入验证内容,这样实现每一个文件都可以调用的情景
<%
String login = (String) session.getAttribute("login");
if (login == null){
response.sendRedirect("index.jsp");
return;
}
%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>用户登录</title>
</head>
<body>
<form name="form1" method="post" action="doLogin.jsp">
用户名:
<input type="text" name="userName">
密码:
<input type="password" name="pwd">
<input type="submit" value="登录">
</form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="loginControl.jsp" %> <%-- 引入验证文件 --%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>管理员</title>
</head>
<body>
您好,欢迎您!
</body>
</html>
JSP内置对象application(全局对象)
网页计数器功能
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
Integer i = (Integer) application.getAttribute("count");
out.println("统计访问量:目前有 " + i + " 个人访问过本网站" );
%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'login.jsp' starting page</title>
</head>
<%
Integer count = (Integer) application.getAttribute("count");
if(count != null){
count = 1 + count;
}else{
count = 1;
}
application.setAttribute("count", count);
%>
<body>
<form name="loginForm" method="post" action="showCount.jsp">
用户名:<input type="text" name="userName" />
密码:<input type="password" name="pwd" />
<input type="submit" value="登录">
</form>
</body>
</html>
JSP常用的内置对象
对象的作用域
JSP外置对象Cookie
cookie中的方法
添加cookie
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>添加cookie</title>
</head>
<body>
<%
response.addCookie(new Cookie("username","Jack"));
response.addCookie(new Cookie("password","123456"));
response.sendRedirect("getCookies.jsp");
%>
</body>
</html>
获取cookie
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>读取cookie</title>
</head>
<body>
<%
Cookie[] cookies = request.getCookies();
String user="";
String pwd = "";
if(cookies!=null){
for(int i=0;i<cookies.length;i++){
if(cookies[i].getName().equals("username"))
user = cookies[i].getValue();
else if(cookies[i].getName().equals("password"))
pwd = cookies[i].getValue();
}
}
out.print("用户名:"+user+" ,密码: "+pwd);
%>
</body>
</html>
cookie的有效期
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>添加cookie</title>
</head>
<body>
<%
Cookie nc=new Cookie("info", "ok");
nc.setMaxAge(60); //设置Cookie失效前时间为60秒
response.addCookie(nc);
response.sendRedirect("showCookie.jsp");
%>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>读取cookie</title>
</head>
<body>
<%
Cookie[] cookies = request.getCookies();
boolean sign = false; //标识
if (cookies != null) {
for (int i=0; i<cookies.length; i++) {
if (cookies[i].getName().equals("info")) {
sign = true;
out.print("读取Cookie的值:" + cookies[i].getValue());
}
}
}
if (!sign) {
out.print("超过Cookie有效期,无法读取Cookie");
}
%>
</body>
</html>