JSP基础
- jsp的作用:
- Servlet:
缺点:不适合设置html响应体,需要大量的response.getWriter().print("")
优点:动态资源,可以编程。 - html:
缺点:html是静态页面,不能包含动态信息
优点:不用为输出html标签而发愁 - jsp(java server pages):
优点:在原有html的基础上添加java脚本,构成jsp页面。
- jsp和Servlet的分工:
- JSP:
作为请求发起页面,例如显示表单、超链接。
作为请求结束页面,例如显示数据。 - Servlet:
作为请求中处理数据的环节。
- jsp的组成
-
jsp = html + java脚本 + jsp标签(指令)
-
jsp中无需创建即可使用的对象一共有9个,被称之为9大内置对象。例如:request对象、out对象
-
3种java脚本:
<%…%>:java代码片段(常用),用于定义0~N条Java语句!方法内能写什么,它就可以放什么!
<%=…%>:java表达式,用于输出(常用),用于输出一条表达式(或变量)的结果。response.getWriter().print( … );这里能放什么,它就可以放什么!
<%!..%>:声明,用来创建类的成员变量和成员方法(基本不用,但容易被考到),类体中可以放什么,它就可以放什么!案例:演示jsp与servlet分工!
Aservlet文件:
package add.day11_1;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Aservlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String n1 = request.getParameter("num1");
String n2 = request.getParameter("num2");
int num1 = Integer.parseInt(n1);
int num2 = Integer.parseInt(n2);
int sum = num1 + num2;
//sum为int类型,经过setAttribute自动装箱后 变成了Integer了
request.setAttribute("result", sum);
//请求转换到页面result.jsp 路径以/开头表示当前的项目路径
request.getRequestDispatcher("/add/result.jsp").forward(request, response);
}
}
form.jsp
<%@ 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 'form.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="/day11_1/Aservlet" method="post">
整数1<input type="text" name="num1"/><br/>
整数2<input type="text" name="num2"/><br/>
<input type="submit" value="点击提交"/>
</form>
</body>
</html>
result.jsp
<%@ 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 'result.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This is result page. <br>
<%= request.getAttribute("result") %>
</body>
</html>
Cookie
- Http协议与Cookie(了解)
- Cookie是HTTP协议制定的!先由服务器保存Cookie到浏览器,再下次浏览器请求服务器时把上一次请求得到Cookie再归还给服务器
- 由服务器创建保存到客户端浏览器的一个键值对!服务器保存Cookie的响应头:Set-Cookie: aaa=AAA Set-Cookie: bbb=BBB
response.addHeader(“Set-Cookie”, “aaa=AAA”);response.addHeader(“Set-Cookie”, “bbb=BBB”);
- 当浏览器请求服务器时,会把该服务器保存的Cookie随请求发送给服务器。浏览器归还Cookie的请求头:Cookie: aaa=AAA; bbb=BBB
- Http协议规定(保证不给浏览器太大压力):
1个Cookie最大4KB
1个服务器最多向1个浏览器保存20个Cookie
1个浏览器最多可以保存300个Cookie - 浏览器大战:因为浏览器竞争很激励,所以很多浏览器都会在一定范围内违反HTTP规定,但也不会让一个Cookie为4GB!
- Cookie的用途
- 服务器使用Cookie来跟踪客户端状态!
- 保存购物车(购物车中的商品不能使用request保存,因为它是一个用户向服务器发送的多个请求信息)
- 显示上次登录名(也是一个用户多个请求)
Cookie是不能跨浏览器的!
- JavaWeb中使用Cookie
- 原始方式(了解):
使用response发送Set-Cookie响应头
使用request获取Cookie请求头 - 便捷方式(精通):
使用repsonse.addCookie()方法向浏览器保存Cookie
使用request.getCookies()方法获取浏览器归还的Cookie
Cookie第一例:
> 一个jsp保存cookie, a.jsp
> 另一个jsp获取浏览器归还的cookie! b.jsp
- Cookie详解
- Cookie不只有name和value两个属性
- Cookie的maxAge(掌握):Cookie的最大生命,即Cookie可保存的最大时长。以秒为单位,例如:cookie.setMaxAge(60)表示这个Cookie会被浏览器保存到硬盘上60秒
maxAge>0:浏览器会把Cookie保存到客户机硬盘上,有效时长为maxAge的值决定。
maxAge<0:Cookie只在浏览器内存中存在,当用户关闭浏览器时,浏览器进程结束,同时Cookie也就死亡了。
maxAge=0:浏览器会马上删除这个Cookie! - Cookie的path(理解):
Cookie的path并不是设置这个Cookie在客户端的保存路径!!!
Cookie的path由服务器创建Cookie时设置
当浏览器访问服务器某个路径时,需要归还哪些Cookie给服务器呢?这由Cookie的path决定。
浏览器访问服务器的路径,如果包含某个Cookie的路径,那么就会归还这个Cookie。
例如:
<> aCookie.path=/day11_1/; bCookie.path=/day11_1/jsps/; cCookie.path=/day11_1/jsps/cookie/;
<> 访问:/day11_1/index.jsp时,归还:aCookie
<> 访问:/day11_1/jsps/a.jsp时,归还:aCookie、bCookie
<> 访问:/day11_1/jsps/cookie/b.jsp时,归还:aCookie、bCookie、cCookie
Cookie的path默认值:当前访问路径的父路径。例如在访问/day11_1/jsps/a.jsp时,响应的cookie,那么这个cookie的默认path为/day11_1/jsps/ - Cookie的domain(了解)
domain用来指定Cookie的域名!当多个二级域中共享Cookie时才有用。
例如;www.baidu.com、zhidao.baidu.com、news.baidu.com、tieba.baidu.com之间共享Cookie时可以使用domain
设置domain为:cookie.setDomain(".baidu.com");
设置path为:cookie.setPath("/");
Cookie中不能存在中文!!!
// 保存
Cookie c = new Cookie("username", URLEncoder.encode("张三", "utf-8"));//出错!
response.addCookie(c);
// 获取
Cookie[] cs = request.getCookies();
if(cs != null) {
for(Cookie c : cs){
if("username".equals(c.getName())) {
String username = c.getValue();
username = URLDecoder.decode(username, "utf-8");
}
}
}