servlet 中缺少隐式对象
为什么 Java Server Pages (jsp) 有隐式对象,而 java servlet 没有?
Why do Java Server Pages (jsp) have implicit objects but java servlets do not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Servlet 代码通过
service
或doGet
、doPost
、do...
方法之一执行。这些方法采用 HttpServletRequest 和 HttpServletResponse 参数,因此在某种意义上,请求和响应是隐式对象。您可以通过HttpServletRequest
(即getSession()
)或通过Servlet上下文(getServletContext()
)访问JSP页面上可用的其他隐式对象servlet 中的方法)——这对应于 JSP 页面中的application
。缺少的一个隐式对象是page
,它在普通 servlet 中不可用。 JSP 页面自己提供了这一功能。Servlet code is executed via
service
or one ofdoGet
,doPost
,do...
methods. These methods takeHttpServletRequest
andHttpServletResponse
parameters, so in a sense request and response are implicit objects. You can access other implicit objects that are available on JSP page throughHttpServletRequest
(i.e.getSession()
), or through servlet context (getServletContext()
method in servlet) -- this corresponds toapplication
in JSP page. One missing implicit object ispage
, which is not available in plain servlets. JSP pages provide this one on their own.我们并没有获得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)
orrequest.getSession(false)
.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 方法中包含多个对象声明。
你看到有对象引用,比如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.
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