JSP笔记

作者: 海洋_5ad4 | 来源:发表于2019-01-02 20:07 被阅读0次

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);
    }
}

转发方式的总结

  1. 在Servlet中进行转发request.getRequestDispatcher("/Lesson3Servlet2").forward(request,response)
  2. <jsp:forward page=""></jsp:forward> jsp标签
  3. pageContext.forward("login.jsp"); 内置对象

相关文章

网友评论

      本文标题:JSP笔记

      本文链接:https://www.haomeiwen.com/subject/bxetrqtx.html