Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why jsp create two reference of PageContext and JspWriter while coneverting Jsp to Servlet

Tags:

java

jsp

servlets

I have created a basic Jsp example, when i am going through the Servlet file created automaticaly by Jsp. I have seen two reference of PageContext and JspWriter in _jspService method. Why there are two refences of PageContext and JspWriter

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;

`

like image 335
Varun Chawla Avatar asked Feb 09 '16 06:02

Varun Chawla


1 Answers

This question involved the implemention of JSP custom tag library and tag files. The non-final variable PageContext and JspWriter may changed during run time. Here is a sample JSP

<html><body><%@ taglib uri="http://tomcat.apache.org/example-taglib" prefix="eg"%>
<eg:log>It's <%="TEST"%></eg:log></body></html>

Generated java code of Tomcat 7

1 pageContext = _jspxFactory.getPageContext(this, request, response, null, true, 8192, true);
2 _jspx_page_context = pageContext;
3 application = pageContext.getServletContext();
4 config = pageContext.getServletConfig();
5 session = pageContext.getSession();
6 out = pageContext.getOut();
7 _jspx_out = out;
8 out.write("<html><body>\r\n");
9 //  eg:log
10 examples.LogTag _jspx_th_eg_005flog_005f0 = (examples.LogTag) _005fjspx_005ftagPool_005feg_005flog.get(examples.LogTag.class);
11 _jspx_th_eg_005flog_005f0.setPageContext(_jspx_page_context);
12 _jspx_th_eg_005flog_005f0.setParent(null);
13 int _jspx_eval_eg_005flog_005f0 = _jspx_th_eg_005flog_005f0.doStartTag();
14 if (_jspx_eval_eg_005flog_005f0 != javax.servlet.jsp.tagext.Tag.SKIP_BODY) {
15  if (_jspx_eval_eg_005flog_005f0 != javax.servlet.jsp.tagext.Tag.EVAL_BODY_INCLUDE) {
16    out = _jspx_page_context.pushBody();
17    _jspx_th_eg_005flog_005f0.setBodyContent((javax.servlet.jsp.tagext.BodyContent) out);
18    _jspx_th_eg_005flog_005f0.doInitBody();
19  }
20  do {
21    out.write("It's ");
22    out.print("TEST");
23    int evalDoAfterBody = _jspx_th_eg_005flog_005f0.doAfterBody();
24    if (evalDoAfterBody != javax.servlet.jsp.tagext.BodyTag.EVAL_BODY_AGAIN)
25      break;
26  } while (true);
27  if (_jspx_eval_eg_005flog_005f0 != javax.servlet.jsp.tagext.Tag.EVAL_BODY_INCLUDE) {
28    out = _jspx_page_context.popBody();
29  }
30 }

In line 16, once the program execute _jspx_page_context.pushBody(). A new JSPWriter is returned. It's used to capture the output of body, so data is not write to response outputStream directly. The LogTag.doAfterBody() can get captured ouput and handle it.

like image 96
Beck Yang Avatar answered Oct 01 '22 05:10

Beck Yang