jsp隐式对象_JSP隐式对象

jsp隐式对象

JSP Implicit Objects are very useful for developers. This is the second post in the series of JSP Tutorials, you might want to check out earlier post about JSP Tutorial for Beginners.

JSP隐式对象对开发人员非常有用。 这是JSP教程系列的第二篇文章,您可能想查看有关JSP初学者教程的早期文章。

JSP隐式对象 (JSP Implicit Objects)

JSP implicit objects are created by servlet container while translating JSP page to Servlet source to help developers. We can use implicit objects in JSP directly in scriptlets that goes in service method. However we can’t use jsp implicit objects in JSP declaration because that code will go at class level.

JSP隐式对象由Servlet容器创建,同时将JSP页面转换为Servlet源以帮助开发人员。 我们可以直接在服务方法中使用的scriptlet中使用JSP中的隐式对象。 但是,我们不能在JSP声明中使用jsp隐式对象,因为该代码将在类级别进行。

We have 9 jsp implicit objects that we can directly use in JSP page. Seven of them are declared as local variable at the start of _jspService() method whereas two of them are part of _jspService() method argument that we can use.

我们有9个jsp隐式对象,可以直接在JSP页面中使用。 其中有七个在_jspService()方法的开头声明为局部变量,而其中两个是我们可以使用的_jspService()方法参数的一部分。

  1. JSP Implicit Objects – out Object

    JSP隐式对象– out对象
  2. JSP Implicit Objects – request Object

    JSP隐式对象–请求对象
  3. JSP Implicit Objects – response Object

    JSP隐式对象–响应对象
  4. JSP Implicit Objects – config Object

    JSP隐式对象–配置对象
  5. JSP Implicit Objects – application Object

    JSP隐式对象–应用程序对象
  6. JSP Implicit Objects – session Object

    JSP隐式对象–会话对象
  7. JSP Implicit Objects – pageContext Object

    JSP隐式对象– pageContext对象
  8. JSP Implicit Objects – page Object

    JSP隐式对象–页面对象
  9. JSP Implicit Objects – exception Object

    JSP隐式对象–异常对象
  10. JSP Implicit Objects Example

    JSP隐式对象示例

JSP中的隐式对象 (Implicit Objects in JSP)

Let’s look into each of the implicit objects in JSP page one by one. Then we will go through JSP implicit objects example.

让我们逐一研究JSP页面中的每个隐式对象。 然后我们将通过JSP隐式对象示例。

  1. JSP隐式对象– out对象 (JSP Implicit Objects – out Object)

    JSP out implicit object is instance of javax.servlet.jsp.JspWriter implementation and it’s used to output content to be sent in client response. This is one of the most used JSP implicit object and thats why we have JSP Expression to easily invoke out.print() method.

    JSP输出隐式对象是javax.servlet.jsp.JspWriter实现的实例,它用于输出要在客户端响应中发送的内容。 这是最常用的JSP隐式对象之一,这就是我们拥有JSP Expression来轻松调用out.print()方法的原因。

  2. JSP隐式对象–请求对象 (JSP Implicit Objects – request Object)

    JSP request implicit object is instance of javax.servlet.http.HttpServletRequest implementation and it’s one of the argument of JSP service method. We can use request object to get the request parameters, cookies, request attributes, session, header information and other details about client request.

    JSP请求隐式对象是javax.servlet.http.HttpServletRequest实现的实例,它是JSP服务方法的参数之一。 我们可以使用请求对象来获取请求参数,cookie,请求属性,会话,标头信息以及有关客户端请求的其他详细信息。

  3. JSP隐式对象–响应对象 (JSP Implicit Objects – response Object)

    JSP response implicit object is instance of javax.servlet.http.HttpServletResponse implementation and comes as argument of service method. We can response object to set content type, character encoding, header information in response, adding cookies to response and redirecting the request to other resource.

    JSP响应隐式对象是javax.servlet.http.HttpServletResponse实现的实例,并且作为服务方法的参数来使用。 我们可以通过响应对象来设置内容类型,字符编码,响应中的标头信息,向响应中添加cookie以及将请求重定向到其他资源。

  4. JSP隐式对象–配置对象 (JSP Implicit Objects – config Object)

    JSP config implicit object is instance of javax.servlet.ServletConfig implementation and used to get the JSP init params configured in deployment descriptor.

    JSP config隐式对象是javax.servlet.ServletConfig实现的实例,用于获取在部署描述符中配置的JSP初始化参数。

  5. JSP隐式对象–应用程序对象 (JSP Implicit Objects – application Object)

    JSP application implicit object is instance of javax.servlet.ServletContext implementation and it’s used to get the context information and attributes in JSP. We can use it to get the RequestDispatcher object in JSP to forward the request to another resource or to include the response from another resource in the JSP.

    JSP应用程序隐式对象是javax.servlet.ServletContext实现的实例,它用于获取JSP中的上下文信息和属性。 我们可以使用它来获取JSP中的RequestDispatcher对象,以将请求转发到另一个资源或在JSP中包括来自另一个资源的响应。

  6. JSP隐式对象–会话对象 (JSP Implicit Objects – session Object)

    JSP session implicit object is instance of javax.servlet.http.HttpSession implementation. Whenever we request a JSP page, container automatically creates a session for the JSP in the service method.

    Since session management is heavy process, so if we don’t want session to be created for JSP, we can use page directive to not create the session for JSP using . This is very helpful when our login page or the index page is a JSP page and we don’t need any user session there.

    JSP会话隐式对象是javax.servlet.http.HttpSession实现的实例。 每当我们请求JSP页面时,容器都会在service方法中自动为JSP创建会话。

    由于会话管理是繁重的过程,因此,如果我们不希望为JSP创建会话,则可以使用page指令不使用以下命令为JSP创建会话: 。 当我们的登录页面或索引页面是JSP页面并且我们在那里不需要任何用户会话时,这将非常有用。

  7. JSP隐式对象– pageContext对象 (JSP Implicit Objects – pageContext Object)

    JSP pageContext implicit object is instance of javax.servlet.jsp.PageContext abstract class implementation. We can use pageContext to get and set attributes with different scopes and to forward request to other resources. pageContext object also hold reference to other implicit object.

    JSP pageContext隐式对象是javax.servlet.jsp.PageContext 抽象类实现的实例。 我们可以使用pageContext获取和设置具有不同作用域的属性,并将请求转发给其他资源。 pageContext对象还保存对其他隐式对象的引用。

  8. JSP隐式对象–页面对象 (JSP Implicit Objects – page Object)

    JSP page implicit object is instance of java.lang.Object class and represents the current JSP page. page object provide reference to the generated servlet class. This object is very rarely used.

    JSP页面隐式对象是java.lang.Object类的实例,并且代表当前的JSP页面。 页面对象提供对生成的servlet类的引用。 这个对象很少使用。

  9. JSP隐式对象–异常对象 (JSP Implicit Objects – exception Object)

    JSP exception implicit object is instance of java.lang.Throwable class and used to provide exception details in JSP error pages. We can’t use this object in normal JSP pages and it’s available only in JSP error pages.

    JSP异常隐式对象是java.lang.Throwable类的实例,用于在JSP错误页面中提供异常详细信息。 我们不能在普通的JSP页面中使用该对象,并且仅在JSP错误页面中可用。

JSP隐式对象示例 (JSP Implicit Objects Example)

Here is a simple JSP page that’s showing usage of JSP implicit objects. I am not providing exception object example, that will be covered in JSP Exception Handling post.

这是一个简单的JSP页面,显示JSP隐式对象的用法。 我没有提供异常对象示例,该示例将在JSP异常处理文章中介绍。

index.jsp

index.jsp

<%@ page language="java" contentType="text/html; charset=US-ASCII"
    pageEncoding="US-ASCII"%>
<%@ page import="java.util.Date" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Index JSP Page</title>
</head>
<body>
<%-- out object example --%>
<h4>Hi There</h4>
<strong>Current Time is</strong>: <% out.print(new Date()); %><br><br>

<%-- request object example --%>
<strong>Request User-Agent</strong>: <%=request.getHeader("User-Agent") %><br><br>

<%-- response object example --%>
<%response.addCookie(new Cookie("Test","Value")); %>

<%-- config object example --%>
<strong>User init param value</strong>:<%=config.getInitParameter("User") %><br><br>

<%-- application object example --%>
<strong>User context param value</strong>:<%=application.getInitParameter("User") %><br><br>

<%-- session object example --%>
<strong>User Session ID</strong>:<%=session.getId() %><br><br>

<%-- pageContext object example --%>
<% pageContext.setAttribute("Test", "Test Value"); %>
<strong>PageContext attribute</strong>: {Name="Test",Value="<%=pageContext.getAttribute("Test") %>"}<br><br>

<%-- page object example --%>
<strong>Generated Servlet Name</strong>:<%=page.getClass().getName() %>

</body>
</html>

Deployment descriptor for the dynamic web project is:

动态Web项目的部署描述符为:

web.xml

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns="https://java.sun.com/xml/ns/javaee" xsi:schemaLocation="https://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>JSPImplicitObjects</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <context-param>
  <param-name>User</param-name>
  <param-value>Admin</param-value>
  </context-param>
  
  <servlet>
  <servlet-name>home</servlet-name>
  <jsp-file>/index.jsp</jsp-file>
  <init-param>
  	<param-name>User</param-name>
  	<param-value>Pankaj</param-value>
  </init-param>
  </servlet>
  <servlet-mapping>
  <servlet-name>home</servlet-name>
  <url-pattern>/home.do</url-pattern>
  <url-pattern>/home.jsp</url-pattern>
  </servlet-mapping>
</web-app>

When I access above JSP in browser, I get response like below image.

当我在浏览器中访问以上JSP时,得到如下图所示的响应。

Let’s look at the generated servlet class code for better understanding about these objects.

让我们看一下生成的Servlet类代码,以更好地了解这些对象。

index_jsp.java

index_jsp.java

/*
 * Generated by the Jasper component of Apache Tomcat
 * Version: Apache Tomcat/7.0.32
 * Generated at: 2013-08-22 00:42:24 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.*;
import java.util.Date;

public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent {

  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 javax.el.ExpressionFactory _el_expressionfactory;
  private org.apache.tomcat.InstanceManager _jsp_instancemanager;

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

  public void _jspInit() {
    _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
    _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
  }

  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 javax.servlet.jsp.PageContext pageContext;
    javax.servlet.http.HttpSession session = null;
    final javax.servlet.ServletContext application;
    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;


    try {
      response.setContentType("text/html; charset=US-ASCII");
      pageContext = _jspxFactory.getPageContext(this, request, response,
      			null, true, 8192, true);
      _jspx_page_context = pageContext;
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      session = pageContext.getSession();
      out = pageContext.getOut();
      _jspx_out = out;

      out.write("\n");
      out.write("\n");
      out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"https://www.w3.org/TR/html4/loose.dtd\">\n");
      out.write("<html>\n");
      out.write("<head>\n");
      out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=US-ASCII\">\n");
      out.write("<title>Index JSP Page</title>\n");
      out.write("</head>\n");
      out.write("<body>\n");
      out.write("\n");
      out.write("<h4>Hi There</h4>\n");
      out.write("<strong>Current Time is</strong>: ");
 out.print(new Date()); 
      out.write("<br><br>\n");
      out.write("\n");
      out.write("\n");
      out.write("<strong>Request User-Agent</strong>: ");
      out.print(request.getHeader("User-Agent") );
      out.write("<br><br>\n");
      out.write("\n");
      out.write('\n');
response.addCookie(new Cookie("Test","Value")); 
      out.write('\n');
      out.write('\n');
      out.write("\n");
      out.write("<strong>User init param value</strong>:");
      out.print(config.getInitParameter("User") );
      out.write("<br><br>\n");
      out.write("\n");
      out.write("\n");
      out.write("<strong>User context param value</strong>:");
      out.print(application.getInitParameter("User") );
      out.write("<br><br>\n");
      out.write("\n");
      out.write("\n");
      out.write("<strong>User Session ID</strong>:");
      out.print(session.getId() );
      out.write("<br><br>\n");
      out.write("\n");
      out.write('\n');
 pageContext.setAttribute("Test", "Test Value"); 
      out.write("\n");
      out.write("<strong>PageContext attribute</strong>: {Name=\"Test\",Value=\"");
      out.print(pageContext.getAttribute("Test") );
      out.write("\"}<br><br>\n");
      out.write("\n");
      out.write("\n");
      out.write("<strong>Generated Servlet Name</strong>:");
      out.print(page.getClass().getName() );
      out.write("\n");
      out.write("\n");
      out.write("\n");
      out.write("</body>\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 { 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);
    }
  }
}

Thats all for JSP implicit objects, in future posts you will see a lot of examples where they will be used.

多数民众赞成在JSP隐式对象中,在以后的文章中,您将看到很多使用它们的示例。

翻译自: https://www.journaldev.com/2038/jsp-implicit-objects

jsp隐式对象

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值