1.page
页面级别,现在几乎不用
2.request
一次请求范围
即客户端重定向是获取不了的,而服务器转发是可以获得的 ,因为客户端重定向是发出了两次请求,地址栏是发生了变化的
//演示向request保存作用域保存数据
@WebServlet("/demo06")
public class Demo06ServletRequest extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.向request保存作用域保存数据
request.setAttribute("uname","lili");
//2.客户端重定向
//response.sendRedirect("demo07");
//3.服务器转发
request.getRequestDispatcher("demo07").forward(request,response);
}
}
//演示从request保存作用域获取数据
@WebServlet("/demo07")
public class Demo07ServletRequest extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取request保存作用域保存的数据,key为uname
Object unmObj=request.getAttribute("uname");
System.out.println("unameObj="+unmObj);
}
}
3.session
一次会话范围
无论是客户端重定向还是,服务器转发都是可以获得的
package com.example.demo2;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
//演示向session保存作用域保存数据
@WebServlet("/demo08")
public class Demo08ServletSession extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.向request保存作用域保存数据
request.getSession().setAttribute("uname","lili");
//2.客户端重定向
//response.sendRedirect("demo07");
//3.服务器转发
request.getRequestDispatcher("demo07").forward(request,response);
}
}
//演示从session保存作用域获取数据
@WebServlet("/demo09")
public class Demo09ServletSession extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取session保存作用域保存的数据,key为uname
Object unameObj=request.getSession().getAttribute("uname");
System.out.println("unameObj="+unameObj);
}
}
4.application
整个应用 范围
只要tomcat不关闭都是可以获得到的,用别的浏览器查看也是可以获得的
//演示向application保存作用域保存数据
@WebServlet("/demo10")
public class Demo10ServletApplication extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.向application保存作用域保存数据
//ServletContext:servlet上下文,即代表当前web应用
ServletContext application=request.getServletContext();
application.setAttribute("uname","lili");
//2.客户端重定向
//response.sendRedirect("demo07");
//3.服务器转发
request.getRequestDispatcher("demo11").forward(request,response);
}
}
//演示从applictation保存作用域获取数据
@WebServlet("/demo11")
public class Demo11ServletApplication extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取application保存作用域保存的数据,key为uname
ServletContext applictation=request.getServletContext();
Object unameObj=applictation.getAttribute("uname");
System.out.println("unameObj="+unameObj);
}
}
路径问题
…/ 表示的是推入上一级目录
绝对路径: http://localhost:8080/pro10/css/shopping.css
在head标签中有base标签:的作用
是:当前页面上的所有路径都以这个为基础