<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--
jsp中有9大对象
--%>
<%
/*四大域对象*/
request.setAttribute("user", "gavin");
session.setAttribute("pwd", 12356);
application.setAttribute("userId", "1001");
pageContext.setAttribute("userPhone", 10086);
/*响应*/
response.sendRedirect("https://www.baidu.com");
/*输出流对象*/
out.print("IO流");
%>
<%--当<%@page isErrorPage="true" %>设置为true时才可使用exception对象--%>
<%@page isErrorPage="true" %>
<%--异常对象--%>
<%exception.getCause();%>
<%--page对象---即当前jsp对象--%>
<% page.getClass();
/*config对象*/
config.getServletName();
%>
</body>
</html>
那这九大对象在那里体现呢?
打开转译之后的java代码—可以看到在jspservice方法中用到了这九大对象
@WebServlet(urlPatterns = "/PutDemo2.do")
public class PutDemo2 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//向servlet三大域中放信息
Person per= new Person("张三",22,"男");
Person per1= new Person("李四",23,"男");
Person per2= new Person("王五",24,"男");
Person per3= new Person("赵六",25,"男");
req.setAttribute("request域",per);
HttpSession session = req.getSession();
session.setAttribute("session域",per1);
ServletContext application = req.getServletContext();
application.setAttribute("application域",per2);
RequestDispatcher requestDispatcher = req.getRequestDispatcher("ReadDemo.jsp");
requestDispatcher.forward(req,resp);
}
}
jsp文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<%@page import="PutDemo.Person" %>
</head>
<body>
<%=request.getAttribute("request域")%>
<br>
<%=session.getAttribute("session域")%>
<br>
<%=application.getAttribute("application域")%>
<%=page.getClass()%>
<%pageContext.setAttribute("user",new Person("钱八",19,"男"));%>
<br>
<%=pageContext.getAttribute("user")%>
</body>
</html>