JSP的9大内置对象
内置对象是指在JSP的<%=%> 和<% %>中可以直接使用的对象
什么是内置对象,就是说不用new,直接拿来就能用
表格见word
pageContext
package com.gyf.web.servlet.lesson03;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/Lesson3Servlet1")
public class Lesson3Servlet1 extends HttpServlet{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*
* 数据的存储方式
* 1.ServletContext【application】 在整个应用时共享,范围最大
* 2.Session 在同一个浏览器同一个Session才有效
* 3.request 在同一个请求对象中有效【转发】
*/
//this.getServletContext().setAttribute("name", "gyf");
//request.getSession().setAttribute("name", "gyf");
request.setAttribute("name", "gyf");
request.getRequestDispatcher("/Lesson3Servlet2").forward(request, response);
}
}
package com.gyf.web.servlet.lesson03;
import java.io.IOException;
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 org.apache.catalina.connector.Response;
@WebServlet("/Lesson3Servlet2")
public class Lesson3Servlet2 extends HttpServlet{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//String name = (String) this.getServletContext().getAttribute("name");
//String name = (String) request.getSession().getAttribute("name");
String name = (String) request.getAttribute("name");
response.getWriter().write(name);
}
}
转发方式的总结
- 在Servlet中进行转发request.getRequestDispatcher("/Lesson3Servlet2").forward(request,response)
- <jsp:forward page=""></jsp:forward> jsp标签
- pageContext.forward("login.jsp"); 内置对象
网友评论