Basic JSP Scripting Elements

JavaServer Pages (JPSs) offer a number of ways to inject Java code into the Servlet code generated when a JSP is compiled.


Scriptlet code elements put the included Java code into the _jspService method. The included code needs to be complete java code (Remember the trailing semicolons.):

exciting HTML code
<% String important = new String("Very important programmer"); %>
more HTML code

Expressions are evaluated and placed into out.print() methods to be written to the output stream. The code included is an expression, not a complete Java statement, so leave off the trailing semicolon:

HTML tags
<%= important %>
HTML tags
<%= new java.util.Date() %>

Declaration elements insert the enclosed code into the body of the servlet code allowing the declaration of variables or methods:

HTML stuff
<%! private int counter = 0; %>

 

About Quinn McHenry

Quinn was one of the original co-founders of Tech-Recipes. He is currently crafting iOS applications as a senior developer at Small Planet Digital in Brooklyn, New York.
View more articles by Quinn McHenry

The Conversation

Follow the reactions below and share your own thoughts.

Leave a Reply

You may also like-

Tomcat: How to Set the JAVA_HOME Enviroment VariableTomcat: How to Set the JAVA_HOME Enviroment VariableModify the catalina.bat file so that the servlet engine can work with Java in Tomcat. Catalina is Tomcat's servlet container that implements specifications for ... How to Manually Install Tomcat on Windows 7How to Manually Install Tomcat on Windows 7Apache Tomcat is an open source web server and servlet container created by the Apache Software Foundation. It's used to execute to Java servlets ...