美文网首页
JSP与servlet之间的参数传递

JSP与servlet之间的参数传递

作者: pujess | 来源:发表于2019-08-14 17:50 被阅读0次

四大作用域

要先知道四大作用域是什么,怎么运作的。
什么是四大作用域?(这部分知识在jsp基础里面有提到过,jsp里面有九个内置对象,其中就有四个域对象)
JSP中的四大作用域

page作用域
request作用域
session作用域
application作用域

这四个域对象就是在jsp和servlet类文件之间保存数据、获取数据、传递数据的对象
这四个域对象的作用范围不同,因此称作四个作用域。

四个域对象详解

想记清楚作用域,需要明白域对象的原理。而想要弄清楚域对象,可以通过看jsp实际在服务器上编译成的java文件。

Tips:如何找到jsp的java文件?
在部署在服务器上的项目目录下,例如我的就在我的tomcat服务器下:
D:\Tomacat\apache-tomcat-8.5.43\work\Catalina\localhost\http_req_resp\org\apache\jsp\

首先先看我的test.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
request.setAttribute("3", 3);
%>
<%request.getRequestDispatcher("d1").forward(request, response); %>
</body>
</html>

它的java文件代码是这样的:

/*
 * Generated by the Jasper component of Apache Tomcat
 * Version: Apache Tomcat/8.5.43
 * Generated at: 2019-08-14 08:05:37 UTC
 * Note: The last modified time of this file was set to
 *       the last modified time of the source file after
 *       generation to assist with modification tracking.
 */
package org.apache.jsp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;

public final class testArea_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent,
                 org.apache.jasper.runtime.JspSourceImports {

  private static final javax.servlet.jsp.JspFactory _jspxFactory =
          javax.servlet.jsp.JspFactory.getDefaultFactory();

  private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;

  private static final java.util.Set<java.lang.String> _jspx_imports_packages;

  private static final java.util.Set<java.lang.String> _jspx_imports_classes;

  static {
    _jspx_imports_packages = new java.util.HashSet<>();
    _jspx_imports_packages.add("javax.servlet");
    _jspx_imports_packages.add("javax.servlet.http");
    _jspx_imports_packages.add("javax.servlet.jsp");
    _jspx_imports_classes = null;
  }

  private volatile javax.el.ExpressionFactory _el_expressionfactory;
  private volatile org.apache.tomcat.InstanceManager _jsp_instancemanager;

  public java.util.Map<java.lang.String,java.lang.Long> getDependants() {
    return _jspx_dependants;
  }

  public java.util.Set<java.lang.String> getPackageImports() {
    return _jspx_imports_packages;
  }

  public java.util.Set<java.lang.String> getClassImports() {
    return _jspx_imports_classes;
  }

  public javax.el.ExpressionFactory _jsp_getExpressionFactory() {
    if (_el_expressionfactory == null) {
      synchronized (this) {
        if (_el_expressionfactory == null) {
          _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
        }
      }
    }
    return _el_expressionfactory;
  }

  public org.apache.tomcat.InstanceManager _jsp_getInstanceManager() {
    if (_jsp_instancemanager == null) {
      synchronized (this) {
        if (_jsp_instancemanager == null) {
          _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
        }
      }
    }
    return _jsp_instancemanager;
  }

  public void _jspInit() {
  }

  public void _jspDestroy() {
  }

  public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
      throws java.io.IOException, javax.servlet.ServletException {

    final java.lang.String _jspx_method = request.getMethod();
    if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method) && !javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) {
      response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSPs only permit GET POST or HEAD");
      return;
    }

    final javax.servlet.jsp.PageContext pageContext;//pageContext是一个Servlet包中的pageContext的实例
    javax.servlet.http.HttpSession session = null;//session是一个HttpSession实例
    final javax.servlet.ServletContext application;//application是一个ServletContext实例
    final javax.servlet.ServletConfig config;
    javax.servlet.jsp.JspWriter out = null;
    final java.lang.Object page = this;
    javax.servlet.jsp.JspWriter _jspx_out = null;
    javax.servlet.jsp.PageContext _jspx_page_context = null;//这个是真正的page域对象


    try {
      response.setContentType("text/html; charset=ISO-8859-1");
      pageContext = _jspxFactory.getPageContext(this, request, response,
                null, true, 8192, true);//把页面的信息用pageContext来引用
      _jspx_page_context = pageContext;//真正的page域对象!!
      application = pageContext.getServletContext();//把原本服务器中的servletContext实例用application来引用
      config = pageContext.getServletConfig();
      session = pageContext.getSession();//把会话中new的session对象用session来引用
      out = pageContext.getOut();
      _jspx_out = out;

      out.write("\r\n");
      out.write("<!DOCTYPE html>\r\n");
      out.write("<html>\r\n");
      out.write("<head>\r\n");
      out.write("<meta charset=\"ISO-8859-1\">\r\n");
      out.write("<title>Insert title here</title>\r\n");
      out.write("</head>\r\n");
      out.write("<body>\r\n");
      out.write("<h1>ddddd</h1>\r\n");

request.setAttribute("3", 3);

      out.write('\r');
      out.write('\n');
request.getRequestDispatcher("d1").forward(request, response); 
      out.write("\r\n");
      out.write("\r\n");
      out.write("</body>\r\n");
      out.write("</html>");
    } catch (java.lang.Throwable t) {
      if (!(t instanceof javax.servlet.jsp.SkipPageException)){
        out = _jspx_out;
        if (out != null && out.getBufferSize() != 0)
          try {
            if (response.isCommitted()) {
              out.flush();
            } else {
              out.clearBuffer();
            }
          } catch (java.io.IOException e) {}
        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
        else throw new ServletException(t);
      }
    } finally {
      _jspxFactory.releasePageContext(_jspx_page_context);//释放掉page域对象。
    }
  }
}

可从上看出:

  • application只是单纯的引用了系统中已经存在的servletContext对象;session只是单纯的引用了每次会话中都只创建一次的session对象;page域对象也只是一个单纯的引用,但是因为它最后被释放了;因此他们的作用范围显而易见
  • pageContext对象是jsp的上下文,可以获取到servletContext、session等已经存在的对象,这样就能实现他们的作用范围
  • request对象和response对象是用户的请求和应答,可以被转发,转发还是那个对象;但是如果是重定向了,那就是传回客户端再次请求的过程,就不是同一个request了。

常说的jsp往servlet三种传值方式

  1. Form表单传值,
  2. url传值
    3.session传值
    实际上application也就是servletContext也可以作为传值,但是不太好;而request域对象如果要传值可以是可以,但是当你在jsp页面上转发到servlet的时候这个jsp界面所有东西都会一下子闪过,内容不会停留在浏览器上,这样这个jsp就没有存在的必要。

servlet给jsp传值

  1. 重定向
    这种方式要传值出去的话,只能在url中带parameter或者放在session中,无法使用request.setAttribute来传递。

  2. 转发
    使用这种方式跳转,传值可以使用三种方法:使用url中带parameter或使用session或使用request.setAttribute

总结

  • 其实通过四个域对象传参的原理很简单:
    同一个对象数据就不会丢,不同的对象就不能传参啦!
  • 平常开发就使用常用的几种传值方式就好

相关文章

网友评论

      本文标题:JSP与servlet之间的参数传递

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