1.pageContext (存东西)
2.request (请求,存东西)
3.session (会话,存东西)
4.application (相当于 ServletContext,存东西)
5.config (相当于ServletConfig)
6.out (向页面输出东西)
7.response (响应)
8.exception (java异常错误)
9.page (几乎不用)
1.存东西(保存数据)
<% pageContext.setAttribute("name1","肖聪1");//保存的数据只在一个页面中有效 request.setAttribute("name2","肖聪2");//保存的数据只在一次请求中有效,请求转发同样有效 session.setAttribute("name3","肖聪3");//保存的数据在一次会话中有效 application.setAttribute("name4","肖聪4");//保存的数据在服务器中有效 %>
2.同一个页面找数据
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <% pageContext.setAttribute("name1","肖聪1");//保存的数据只在一个页面中有效 request.setAttribute("name2","肖聪2");//保存的数据只在一次请求中有效,请求转发同样有效 session.setAttribute("name3","肖聪3");//保存的数据在一次会话中有效 application.setAttribute("name4","肖聪4");//保存的数据在服务器中有效 %> <% // pageContext.getAttribute("name1"); String name1 =(String) pageContext.findAttribute("name1"); String name2 =(String) pageContext.findAttribute("name2"); String name3 =(String) pageContext.findAttribute("name3"); String name4 =(String) pageContext.findAttribute("name4"); String name5 =(String) pageContext.findAttribute("name5"); %> <h1>取出的值为</h1> <h3>${name1}</h3> <h3>${name2}</h3> <h3>${name3}</h3> <h3>${name4}</h3> <h3>${name5}</h3> <h3><%=name5%></h3> </body> </html> (pageContext.jsp)
3.不用页面找数据
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <% String name1 =(String) pageContext.findAttribute("name1"); String name2 =(String) pageContext.findAttribute("name2"); String name3 =(String) pageContext.findAttribute("name3"); String name4 =(String) pageContext.findAttribute("name4"); String name5 =(String) pageContext.findAttribute("name5"); %> <h1>取出的值为</h1> <h3>${name1}</h3> <h3>${name2}</h3> <h3>${name3}</h3> <h3>${name4}</h3> <h3>${name5}</h3> <h3><%=name5%></h3> </body> </html> (pageDemo1.jsp)
4.其他方式存东西
<% pageContext.setAttribute("hello1","hello1",pageContext.SESSION_SCOPE); //相当于session.setAttribute("hello1","hello1") //可以用这种方法做,但是不建议这样做 %>
5.pageContext 请求转发
<% pageContext.forward("/index1.jsp");//请求转发 //这样相当于request.getRequestDispatcher("/index1.jsp").forward(request,response) %>