If we declare a variable, say int i=3 in jsp file, then in what method does this variable reside? Is it within service() or the init() method? If this resides in service(), how can I make to be in init() and vice versa?

Luigi Viggiano

When declaring a variable inside <% %> it is contained in the service; you can define jspInit() instead of servlet's method init() and declare your variables there.

I would like to expose some variable declaration in a JSP file and corresponding generated servlet to try to give you an answer.

This is my JSP:

<html>
<body>
<h1>Declaration sample</h1>
<%!
    int i = 10;

    public void jspInit() {
        int j = 20;
        System.out.println("jspInit(): j = " + j);
    }

    public void jspDestroy() {
        int k = 30;
        System.out.println("jspDestroy(): k = " + k);
    }

%>
<%
    int m = 40;
    System.out.println("jspService m = " + m);
%>

</body>
</html>
Generated servlet (by tomcat) follows:
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.Vector;
import org.apache.jasper.runtime.*;
import java.beans.*;
import org.apache.jasper.JasperException;

public class _0002fExample_0002ejspExample_jsp_2 extends HttpJspBase {
    int i = 10;

    public void jspInit() {
        int j = 20;
        System.out.println("jspInit(): j = " + j);
    } 

    public void jspDestroy() {
        int k = 30;
        System.out.println("jspDestroy(): k = " + k);
    } 

    private static boolean _jspx_inited = false;

    public void _jspService(HttpServletRequest request, HttpServletResponse response) throws 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 {
            if (_jspx_inited == false) {
                _jspx_init();
                _jspx_inited = true;
            } 

            _jspxFactory = JspFactory.getDefaultFactory();
            response.setContentType("text/html;charset=8859_1");
            pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
            application = pageContext.getServletContext();
            config = pageContext.getServletConfig();
            session = pageContext.getSession();
            out = pageContext.getOut();

            out.write("<html>
<body>
<h1>Declaration sample</h1>

");

            int m = 40;
            System.out.println("jspService m = " + m);
            
            out.write("

</body>
</html>
");
        } catch (Exception ex) {
            if (out.getBufferSize() != 0)
                out.clearBuffer();

            pageContext.handlePageException(ex);
        } 
        finally {
            out.flush();
            _jspxFactory.releasePageContext(pageContext);
        } 
    } 
}
Note: I just cleaned up some un-necessary code to make the code easier to be read.
_jspx_init(); will call our jspInit method, and you can use also jspDestroy to release resources.
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact