开发者

Lack of implicit objects in servlet

开发者 https://www.devze.com 2023-01-25 11:58 出处:网络
Why do Java Server Pages (jsp) have implicit objects but java servlets 开发者_运维技巧do not?Servlet code is executed via service or one of doGet, doPost, do... methods. These methods take HttpServlet

Why do Java Server Pages (jsp) have implicit objects but java servlets 开发者_运维技巧do not?


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.


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).


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

0

精彩评论

暂无评论...
验证码 换一张
取 消