Let’s take a look at the very basics of Java Server Pages.
Serving JSP Pages
JSP pages should be bundled in a Java web application. They’re placed in the same location as regular HTML pages - basically, anywhere other than the WEB-INF/
folder. Furthermore, you won’t have to configure anything in the web.xml
file, and your web server will automatically compile and render the pagefor you (provided you are using a Java application server, like we have been thoughout these tutorials).
The example code for this post can be found in our tutorial Github repository under the jsp_part_1
branch. A ZIP file containing the example can be found here. Everything should behave the same as in the previous Servlet examples.
If you take a look at the code, you’ll find that there isn’t much in there. There’s no web.xml
file, nor are there any Servlets. All that is there is a file named index.jsp
in the src/main/webapp
folder. And here are its contents:
<html>
<body>
Here are some random numbers <%= new java.util.Random().nextInt(10) %>
</body>
</html>
Most of it looks like normal HTML. Except that stuff on line 3. There’s some Java code sandwiched between <%= and %>. This is the JSP part of the page. To see just what it will do, let’s load the page and see what it looks like. In our source code, the file is located at source/main/webapp/index.jsp
, which means it will be located at the root of the webapp when it is deployed to the server. So I loaded up localhost:8080/index.jsp
, and this is what came up:
The Java code we saw in the source file is nowhere to be found. Instead in its place is a number. Remember that the Java code in the source file was new java.util.Random().nextInt(10)
, which will generate a random number. From this we can intuit that the code itself was executed, and then replaced in the page by the number it generated. If you need further proof, try refreshing the page. You should see a different number every time.
What’s going on here? How is the Java code executed, and how is it rendered on the page? In our previous post I mentioned that JSP pages get converted into Servlets, which can then be compiled and executed. And as luck would have it, the build tools we are using will actually generate not just the compiled class file, but the source file as well. This is what I found in java_servlet_tutorial/build/serverBaseDir_jetty9/webapps-exploded/jsp/org/apache/jsp/index_jsp.java
:
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {
private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();
private static java.util.List<String> _jspx_dependants;
private org.glassfish.jsp.api.ResourceInjector _jspx_resourceInjector;
public java.util.List<String> getDependants() {
return _jspx_dependants;
}
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException {
PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;
try {
response.setContentType("text/html");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
_jspx_resourceInjector = (org.glassfish.jsp.api.ResourceInjector) application.getAttribute("com.sun.appserv.jsp.resource.injector");
out.write("<html>\n");
out.write(" <body>\n");
out.write(" Here are some random numbers ");
out.print( new java.util.Random().nextInt(10) );
out.write("\n");
out.write(" </body>\n");
out.write("</html>\n");
} catch (Throwable t) {
if (!(t instanceof SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
out.clearBuffer();
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
else throw new ServletException(t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
}
Since this is generated code, it is hardly clean and concise. But you can find the answers to those questions in the long sequence of out.write()
statements. The HTML is written out as Strings, while the Java code is executed and output as well. It isn’t a quick and pretty solution, but it works.
General Comments
I showed you the generated Servlet code for educational purposes. In practice, you should never have to look at or deal with it.
What we saw in this post is perhaps the dead simplest example of what you can do in a JSP page. It isn’t, however, the only thing we can do, but we’ll save the juicy details for another post.