Lezione 6: JSP

Esempio: hello.jsp

Consideriamo una semplice jsp che stampa una stringa: hello.jsp


<%-- hello.jsp stampa il classico saluto --%>

<%! static private String str = "world!";%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
	  "http://www.w3.org/TR/REC-html40/loose.dtd">

<html>
 <head>
   <title>Hello!</title>
 </head>

 <body>
   <h1>Hello world!</h1>
   <strong>Hello,<%= str.toUpperCase() %></strong>
 </body>
</html>

Supponiamo di memorizzare questa JSP nel context ROOT/.
La prima volta che viene questa JSP invocata tramite l'URL http://localhost:8080/hello.jsp, Tomcat invoca il compilatore Jasper che produce la servlet equivalente ~tomcat/work/localhost/_/hello$jsp.java:


package org.apache.jsp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import org.apache.jasper.runtime.*;

public class hello$jsp extends HttpJspBase {

    static private String str = "world!"; 

    public hello$jsp( ) { }

    private static boolean _jspx_inited = false;

    public final void _jspx_init() 
        throws org.apache.jasper.runtime.JspException { }

    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 {
            if (_jspx_inited == false) {
                synchronized (this) {
                    if (_jspx_inited == false) {
                        _jspx_init();
                        _jspx_inited = true;
                    }
                }
            }
            _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();
            session = pageContext.getSession();
            out = pageContext.getOut();

            out.write("\r\n\r\n<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\r\n\t  \"http://www.w3.org/TR/REC-html40/loose.dtd\">\r\n\r\n<html>\r\n<head>\r\n  <title>\r\n    Esempio\r\n  </title>\r\n</head>\r\n\r\n<body>\r\n  <h1>Hello world!</h1>\r\n  <strong>Hello, ");
            out.print( str.toUpperCase() );
            out.write("</strong>\r\n</body>\r\n</html>\r\n");
        } catch (Throwable t) {
            if (out != null && out.getBufferSize() != 0)
                out.clearBuffer();
            if (pageContext != null) pageContext.handlePageException(t);
        } finally {
            if (_jspxFactory != null) _jspxFactory.releasePageContext(pageContext);
        }
    }
}

La servlet viene quindi eseguita dal servlet engine (Tomcat). Il risultato dell'esecuzione è il seguente file html:


<!DOCTYPE HTML
    PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/REC-html40/loose.dtd">

<html>
 <head>
   <title>Hello!</title>
 </head>

 <body>
   <h1>Hello world!</h1>
   <strong>Hello, WORLD!</strong>
 </body>
</html>

Valida il documento
Copyright © 2002 by Roberto Posenato