Inserting scripts in JSP

Available

Scriptlets <%

What can be inserted into a JSP file?

First, Java code. If you want to insert Java code into your JSP, then the general format is given by the template:

 <%
	Java code
 %>

You can break this code into several parts:

 <%
   Beginning of Java Code
 %>
  HTML-code
<%
   End of Java Code
 %>

Example:


    <html> 
    <body> 
	<%
    	double num = Math.random();
    	if (num > 0.95) {
     %>
         <h2> You are lucky, user!</h2><p>(<%= num %>)</p>
 	<%
   	    } else {
     %> 
         <h2> Today is not your day, user!</h2><p>(<%= num %>)</p>
 	<%
   	    }
 	%>
  </body> 
   </html> 

Expression <%=

You can also insert any calculated expression into the JSP file. At the same time, the JSP parser itself will make sure that it is not only calculated, but also assigned where necessary. The expression inside the code is given by a template:

 <%= expression %>

Note that the semicolon is not needed here.

JSP servlet example with multiple expressions:

<p>root of 10 equals <%= Math.sqrt(10) %></p>
<h5><%= item[10] %></h5>
<p>current time: <%=  new java.util.Date() %></p>

This code will be converted to this Java code:

out.write("<p>");
out.write("The root of 10 is ");
out.print( Math.sqrt(10) );
out.write("</p>");
out.write("<h5>");
out.print( item[10] );
out.write("</h5>");
out.write("<p> Current time: ");
out.print( new java.util.Date()  );
out.write("</p>");

Important! In your Java code and expressions, you can use predefined variables such asrequest,response,session,outand so on.

Comments (2)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Thomas
Level 41 , Bayreuth, Germany
16 March, 09:34
What to do with these request, response, session etc objects (as listed in my previous comment)? Here some code examples so you may hopefully see what you can be done with that objects and how useful they are. out:
<%
    String greet = "Hello";
    out.println(greet);
%>
request and response:
<%
    String userName = request.getParameter("name");
    if (userName != null)
        out.println(userName);
%>
session:
<%
    String userName = request.getParameter("name");
    if (userName != null)
        session.setAttribute("userName", userName);
%>
pageContext: Little bit about scopes. There are application, session, request and page scopes. If a variable is in application scope, then it is valid over the applications lifetime. page scope just inside your jsp (similar to this). If you declare a variale in a wider scope, then it also is visiblie in a narrower one. Eg. session variables are visible in request scope. To uniform the access to all scopes the following has been introduced (doing the same as the snippet above)
<%
    String userName = request.getParameter("name");
    pageContext.setAttribute("name", userName, PageContext.SESSION_SCOPE);
%>
config: with config you can access init parameters of the servlet (defined in the deployment descriptor)
<%
    String dbConfig = config.getInitParameter("db-config");
%>
Thomas
Level 41 , Bayreuth, Germany
16 March, 09:41
Also not mentioned has been the possibility to declare class variables. They need to be thread safe as a servlet usually is invoked by several threads.
<%! AtomicLong accessCounter = new AtomicLong(0); %>