工程路径
相对路径
页面所有的相对路径,在默认情况下,都会参考当前浏览器地址栏的路径http://ip:port/工程名/+资源来进行跳转
项目名就是配置tomcat的application context,一般就是类名
<form action="ok"></form>
//等价于
<form action="http://localhost:8080/ok"></form>
相对路径带来的问题举例


在a.html跳转到b.html 相当于http://localhost:8080/项目名/d1/d2/b.html
在b.html跳转到a.html 相当于http://localhost:8080/项目名/a.html
需要写成../../a,html
Base标签
1. base 标签是HTML语言中的基准网址标记,它是一个单标签,位于网页头部文件的head 标签内 2. 一个页面最多只能使用一个base元素,用来提供一个指定的默认目标,是一种表达路 径和连接网址的标记
第一个/,会被解析成http://localhost:8080/
不带/,根据当前页面来定位资源
<head>
<!--
1. 如果没有<basehref="http://localhost:8080/webpath/">
2. 当点击 返回a.html 超链接,将会以当前浏览器的地址为路径来确定路径
3. 如果增加了<basehref="http://localhost:8080/webpath/">
4. 将以 base 指定的href的地址为路径,来确定 超链接的路径-->
<base href="http://localhost:8080/webpath/">
<!--简写形式(常用)
第一个/,会被解析成http://localhost:8080/-->
<base href="/webpath/">
</head>
<body>
<h1>这是b.html</h1>
<a href="a.html"><h1>返回a.html</h1></a>
</body>
<body>
<h1>这是a.html</h1>
<a href="d1/d2/b.html">跳转到/d1/d2/b.html</a><br/>
<!--实际开发中需要在服务端进行转发或者重定向来访问资源-->
<!-- path是xml中的pattern -->
<a href="path">转发到/d1/d2/b.html</a>
</body>
转发
在服务器端
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("进行转发...");
//解析第一个/时,会被解析成http://ip:port/项目名/
//在服务器端,所以前面的主机名默认就有,可以认为解析成 /项目名/
//没有/,默认http://ip:port/项目名/,建议写/
request.getRequestDispatcher("/d1/d2/b.html").forward(request,response);
}
最好带上** ****/工程路径/资源名**

注意

String realPath = getServletContext().getRealPath("/");
//实际的工作路径,在out目录下
String contextPath = getServletContext().genContextPath();
//是tomcat配置的application context

总结

重定向
全路径,浏览器解析/,当前路径

工程路径优化
在index.jsp修改



需要的地方进行修改

Cookie
Cookie(小甜饼)是客户端技术,服务器把每个用户的数据以cookie的形式写给用户各自的浏 览器。当用户使用浏览器再去访问服务器中的web资源时,就会带着各自的数据去。这样, web 资源处理的就是用户各自的数据了

**服务器端在需要的时候可以从客户端/浏览器读取(http协议) **
cookie数据是保存在浏览器的
常用方法
Cookie有点象一张表(K-V),分两列,一个是名字,一个是值,数据类型都是String
创建一个Cookie(在服务端创建的)
Cookiec=newCookie(Stringname,Stringval);
c.setMaxAge();//保存时间
将一个Cookie添加到客户端
response.addCookie(c);
读取cookie(在服务器端读取到cookie信息)
request.getCookies();
创建Cookie
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//创建Cookie对象
//username 该Cookie的名字,是唯一的,可以理解成key
//abc:是Cookie的值
//可以创建多个Cookie
Cookie cookie1 = new Cookie("username1", "abc1");
Cookie cookie2 = new Cookie("username2", "abc2");
//将Cookie发送给浏览器,让浏览器保存Cookie
response.addCookie(cookie1);
response.addCookie(cookie2);
}
读取Cookie
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//通过request对象读取Cookie信息
Cookie[] cookies = request.getCookies();
//遍历Cookie
if(cookies != null && cookies.length != 0) {
for(Cookie cookie : cookies) {
System.out.println(cookie.getName() + "=" + cookie.getValue());
}
}
}
不同会话,jsessionid 不同
修改Cookie的值
同名的Cookie相当于覆盖
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = "username";
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
if(name.equals(cookie.getName())) {
cookie.setValue("ccc");
//浏览器中本地的cookie没有修改,需要返回给浏览器
response.addCookie(cookie);
}
}
}
cookie 生命周期
**
浏览器在发http请求的时候不再携带Cookie信息,而不是删除
0才是删除,返回信息:**Set-Cookie: username=xxx; Expires=Thu, 01-Jan-1970 00:00:10 GMT
cookie有效路径

请求地址长的包含短的,默认是/工程路径
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie cookie1 = new Cookie("address", "bj");
Cookie cookie2 = new Cookie("salary", "2000");
cookie1.setPath(request.getContextPath());
cookie2.setPath(request.getContextPath() + "/aaa");
response.addCookie(cookie1);
response.addCookie(cookie2);
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
writer.print("<h1>设置成功</h1>");
writer.flush();
writer.close();
}
用户登录保留用户名
将html代码放入servlet,在登入接收信息,保存cookie,再找到cookie的名称,设置用户名value
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
public class UserUI extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = "";
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
if("username".equals(cookie.getName())) {
username = cookie.getValue();
System.out.println(username);
}
}
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
writer.print("<!DOCTYPE html>\n" +
"<html lang=\"en\">\n" +
"<head>\n" +
" <meta charset=\"UTF-8\">\n" +
" <title>登录页面</title>\n" +
"</head>\n" +
"<body>\n" +
" <h1>登录页面</h1>\n" +
" <form action=\"login\" method=\"post\">\n" +
//账号 字符串拼接
" 账号:<input type=\"text\" value=\"" +username+ "\" name=\"username\"></br>\n" +
" 密码:<input type=\"password\" name=\"pwd\"></br>\n" +
" <input type=\"submit\" value=\"登录\">\n" +
" </form>\n" +
"\n" +
"</body>\n" +
"</html>");
writer.flush();
writer.close();
}
}
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
public class Login extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String pwd = request.getParameter("pwd");
System.out.println(username);
System.out.println(pwd);
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
if("hspedu".equals(username) && "123456".equals(pwd)) {
writer.print("<h1>登录OK</h1>");
Cookie cookie1 = new Cookie("username", username);
Cookie cookie2 = new Cookie("pwd", pwd);
cookie1.setMaxAge(3*24*60*60);
cookie2.setMaxAge(3*24*60*60);
response.addCookie(cookie1);
response.addCookie(cookie2);
} else {
writer.print("<h1>登录失败</h1>");
}
writer.flush();
writer.close();
}
}
Cookie注意事项和细节

URL编码和解码解决中文Cookie
/将 name 保存到cookie中
String urlName = URLEncoder.encode("韩顺平教育", "utf-8");
Cookie nameCookie = new Cookie("edu", urlName);
response.addCookie(nameCookie)
//解码
String val = URLDecoder.decode(cookie.getValue(),"utf-8")
Session
介绍


常用方法

Session底层机制分析
创建Session
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
System.out.println(session.getId());
session.setAttribute("email","[email protected]");
}
读取Session
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
System.out.println(session.getId());
Object email = session.getAttribute("email");
System.out.println((String)email);
}
Session生命周期

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
System.out.println(session.getId());
session.setMaxInactiveInterval(30);
session.setAttribute("u","jack");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
System.out.println(session.getId());
Object u = session.getAttribute("u");
if(u != null){
System.out.println("session未销毁");
} else {
System.out.println("session被销毁");
}
}

关闭浏览器,不会影响session的生命周期 ,是由服务器来维护和管理的
版权归原作者 CYX_cheng 所有, 如有侵权,请联系我们删除。
