< Zurück | Inhalt | Weiter >

19.3.3 Expression

This delimiter is a shorthand for getting output from a very small bit of Java into the output stream. It’s not a complete Java statement, only an expression that evaluates into a String. Here’s an example:

<h4>As of <%= new java.util.Date() %></h4>

which will create a Java Date object (initialized, by default, with the current date/time) and then call the toString() method on that object. This yields a date/time stamp as part of an <h4> heading.

Any methods and variables defined inside the previously described delimiters are OK to use with this expression shorthand.

There are also a few predefined servlet variables.

We’ve described how the JSP is converted into a servlet—the HTML statements become println() calls. This all happens inside of an HttpServlet-like class, just like our BudgetProServlet extends HttpServlet in the previous chapter. In such a class, the method called when a request arrives from a browser looks very much like our doBoth() method:

doBoth(HttpServletRequest request, HttpServletResponse response)


If you want to see the source for the servlet that gets generated when a JSP is converted, and if you’re using NetBeans, right-click on the filename (in the Explorer view) and, from this menu, choose Compile. Then do it again and you’ll notice that the second choice on the menu is View Servlet (Figure 19.1). If you are using Apache Tomcat as your Web server, just look in the work subdirectory in the directory where Tomcat is installed. In the appropriate subdi- rectory you will find both the .java and .class files for your converted JSP with the .jsp suffix converted to $jsp.java and $jsp.class respectively.

For example, BPAcct.jsp becomes BPAcct$jsp.java and is compiled

into BPAcct$jsp.class.



Figure 19.1 Viewing the converted JSP in NetBeans

The point here is that a request object and a response object are defined by the way the servlet is generated. They are called, oddly enough, request and response. In addition to these, a session is defined and initialized, just like we did in our servlet example. (What were we thinking?)

There are a few other variables that the converted servlet has created that we can use. We’ll summarize them in Table 19.1. To read more about how to use them, look up the Javadoc page for their class definition.

Remember that these can be used not only by the <%= %> expressions, but also by the <% %> snippets of code.