servlet 中缺少隐式对象

发布于 2024-10-02 01:46:20 字数 59 浏览 0 评论 0原文

为什么 Java Server Pages (jsp) 有隐式对象,而 java servlet 没有?

Why do Java Server Pages (jsp) have implicit objects but java servlets do not?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓬勃野心 2024-10-09 01:46:20

Servlet 代码通过 servicedoGetdoPostdo... 方法之一执行。这些方法采用 HttpServletRequest 和 HttpServletResponse 参数,因此在某种意义上,请求和响应是隐式对象。您可以通过HttpServletRequest(即getSession())或通过Servlet上下文(getServletContext())访问JSP页面上可用的其他隐式对象servlet 中的方法)——这对应于 JSP 页面中的 application。缺少的一个隐式对象是 page,它在普通 servlet 中不可用。 JSP 页面自己提供了这一功能。

Servlet code is executed via service or one of doGet, doPost, do... methods. These methods take HttpServletRequest and HttpServletResponse parameters, so in a sense request and response are implicit objects. You can access other implicit objects that are available on JSP page through HttpServletRequest (i.e. getSession()), or through servlet context (getServletContext() method in servlet) -- this corresponds to application in JSP page. One missing implicit object is page, which is not available in plain servlets. JSP pages provide this one on their own.

奢欲 2024-10-09 01:46:20

我们并没有获得servlet中的所有隐式变量,因为有些隐式变量是存在的,例如页面和页面上下文,这些变量只属于jsp。默认情况下,我们可以获得一些隐式变量,如请求、响应、会话、输出、配置、应用程序和异常。

为了获取 servlet 中的所有这些隐式变量,我们使用请求和响应隐式变量。例如,如果您想获取会话,我们使用方法 request.getSession(true)request.getSession(false)

We don't get all the implicit variables in servlets, because some implicit variables are there such as page and page context, which only belong to jsp. We can get some implicit variables by default like request, response, session, out, config, application, and exception.

To get the all these implicit variables in a servlet we are using request and response implicit variables. for ex if you want to get session we are using a method request.getSession(true) or request.getSession(false).

我不在是我 2024-10-09 01:46:20

JSP隐式对象是JSP容器在每个页面中提供的Java对象,我们可以直接调用它们而无需显式声明。 JSP 隐式对象也称为预定义变量。

Jsp 中的示例
1)请求类似于HttpServletRequest
2)响应类似于HttpServletResponse
3)应用程序类似于ServletContext
4)配置类似于ServletResponse
5)out类似于JspWriter对象

在 servlet 中,我们可以通过 doGet(HttpServlet request,HttpServletResponse response) 方法或 doPost 方法访问请求、响应,因为这些可以作为容器的参数。

servlet中的config可以通过servlet中的init(ServletConfig config)方法获取。
应用程序或 servlet 中的 ServletContext 对象可以从请求中检索,例如 request.getServletContext();

实际上所有jsp在内部都会转换为servlet。生成的 servlet 代码产生隐式对象。
检查生成的 JSP servlet 源代码后,您知道该代码在其 _ jspService 方法中包含多个对象声明。

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

 JspFactory _jspxFactory = null; 
 PageContext pageContext = null; 
 HttpSession session = null; 
 ServletContext application = null; 
 ServletConfig config = null; 
 JspWriter out = null; 
 Object page = this; 
 String _value = null; 
 try {
 . 
 . 
 . 
 _jspxFactory = JspFactory.getDefaultFactory(); 
 response.setContentType("text/html;charset=ISO-8859-1"); 
 pageContext = jspxFactory.getPageContext(this, 
 request, response, "", true, 8192, true); 
 application =pageContext.getServletContext(); 
 config = pageContext.getServletConfig(); pageContext.getServletConte
 session = pageContext.getSession(); 
 out = pageContext.getOut(); 
 . 
 . 
 . 
}

你看到有对象引用,比如pageContext、session、application、config、out等等。无论这些对象引用是从页面内部使用的,都会创建它们。它们会自动供 JSP 页面作者使用!这些对象称为隐式对象

JSP Implicit Objects are the Java objects that the JSP Container makes available in each page and we can call them directly without being explicitly declared. JSP Implicit Objects are also called pre-defined variables.

Example in Jsp
1)request resembles HttpServletRequest
2)response resembles HttpServletResponse
3)application resembles ServletContext
4)config resembles ServletResponse
5)out resembles JspWriter object
etc

But in servlets we can access request,response through doGet(HttpServlet request,HttpServletResponse response) method or doPost method, because these are available as arguments by the container.

config in servlets can be got through the init(ServletConfig config) method in servlets.
application or the ServletContext object in servlets can be retrieved from request, like request.getServletContext();

Actually all jsps internally get converted to servlets. The generated servlet code gives rise to the implicit objects.
Having examined the generated JSP servlet source code, you know that the code contains several object declarations in its _ jspService method.

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

 JspFactory _jspxFactory = null; 
 PageContext pageContext = null; 
 HttpSession session = null; 
 ServletContext application = null; 
 ServletConfig config = null; 
 JspWriter out = null; 
 Object page = this; 
 String _value = null; 
 try {
 . 
 . 
 . 
 _jspxFactory = JspFactory.getDefaultFactory(); 
 response.setContentType("text/html;charset=ISO-8859-1"); 
 pageContext = jspxFactory.getPageContext(this, 
 request, response, "", true, 8192, true); 
 application =pageContext.getServletContext(); 
 config = pageContext.getServletConfig(); pageContext.getServletConte
 session = pageContext.getSession(); 
 out = pageContext.getOut(); 
 . 
 . 
 . 
}

You see that there are object references, such as pageContext, session, application, config, out, and so on. These object references are created whether they are used from inside the page. They are automatically available for the JSP page author to use! These objects are called implicit objects

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文