Application Techniques

Using JavaServer Pages with SilverStream

How to run JSP pages inside a SilverStream server.

About this technique



HTML Client Techniques> JavaServer Pages


You'll learn about:

Related reading

See the chapter on using JSP pages in the Programmer's Guide

SilverStream provides support for pages that conform to the JavaServer Pages 1.0 specification. JSP 1.0 pages that you've authored outside of SilverStream can run inside a SilverStream server. SilverStream provides utilities for translating JSP pages into Java source files and for uploading the compiled Java classes to a SilverStream server.

NOTE   Do not confuse JSP pages with SilverStream dynamic pages. Dynamic pages that you author in the SilverStream Page Designer use a very different architecture than JSP. At the present time, the SilverStream Page Designer does not provide tools for authoring JSP pages.

Setting up   Top of page

For this technique, you need the following files:




Sample page that demonstrates many of the features of JSP


Sample page that is included by date.jsp

JavaBeans component that performs date processing functions

JavaBeans component that accesses a database and returns the result as HTML


XML file that is used as input to the DeployJSP SilverCmd utility


Batch file that runs the JspCompiler SilverCmd utility, compiles the generated Java source files, creates the JAR file, and runs DeployJSP.

You can find these files in the Docs\help\books\TechCode\jsptech subdirectory of your SilverStream installation directory.

    Complete instructions for running this example are provided under Deploying and testing the JSP pages.

Coding the JSP pages   Top of page

Source for date.jsp   Top of page

Here's the source for the date.jsp sample page:

  <jsp:useBean id='clock' scope='page' class='util.JspCalendar' type="util.JspCalendar"/> 
  <jsp:useBean id='sql' scope='request' class='util.JspSQL'/> 
  <h1> Use the implicit Request object </h1> 
  <li>Query string: <%= request.getQueryString() %> 
  <li>Server name: <%= request.getServerName() %> 
  <li>Server port: <%= request.getServerPort() %> 
  <li>Remote address: <%= request.getRemoteAddr() %> 
  <h1> Use a Bean to access date information</h1> 
  <li>Day of month: is  <jsp:getProperty name="clock" property="dayOfMonth"/> 
  <li>Another form of Day of month: is  <%=clock.getDayOfMonth() %> 
  <li>Year: is  <jsp:getProperty name="clock" property="year"/> 
  <li>Month: is  <jsp:getProperty name="clock" property="month"/> 
  <li>Time: is  <jsp:getProperty name="clock" property="time"/> 
  <li>Date: is  <jsp:getProperty name="clock" property="date"/> 
  <h1> Call a function declared on the JSP page </h1> 
  <%-- Function declaration --%> 
     public String getAString(String x) 
     return x + " was passed in"; 
  <li>  call getAString: <%= getAString("Hello") %> 
  <h1> Use a Bean to access a database </h1> 
  <%= sql.getSQL(request, "Select employeeid as ID, lastname as Name from employees") %> 
  <h1> Execute a scriptlet that has embedded text </h1> 
  <% if (java.util.Calendar.getInstance().get(java.util.Calendar.AM_PM) == java.util.Calendar.AM) {%> 
  Good Morning 
  <% } else { %> 
  Good Afternoon 
  <% } %> 
  <h1>Include the output of another JSP</h1> 
  <jsp:include page="include.jsp"/> 

Source for include.jsp   Top of page

Here's the source for the include.jsp sample page:

  <b>This text is from include.jsp!</b> 

Coding the JavaBeans components   Top of page

Source for   Top of page

Here's the source for the JspCalendar component:

  package util; 
  import java.text.DateFormat; 
  import java.util.*; 
  public class JspCalendar { 
     Calendar  calendar = null; 
     public JspCalendar() {     
     calendar = Calendar.getInstance(); 
     Date trialTime = new Date(); 
     public int getYear() { 
     return calendar.get(Calendar.YEAR); 
     public String getMonth() { 
     int m = getMonthInt(); 
     String[] months = new String [] { "January", "February", 
     "September","October", "November", "December" }; 
     if (m > 12) 
        return "Unknown to Man"; 
  //System.out.println("JspCalendar.getMonth()=" + months[m - 1]); 
     return months[m - 1]; 
     public String getDay() { 
     int x = getDayOfWeek(); 
     String[] days = new String[] {"Sunday", "Monday",  
     "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; 
     if (x > 7) 
        return "Unknown to Man"; 
  //System.out.println("JspCalendar.getDay()=" + days[x - 1]); 
     return days[x - 1]; 
     public int getMonthInt() { 
     return 1 + calendar.get(Calendar.MONTH); 
     public String getDate() { 
     return getMonthInt() + "/" + getDayOfMonth() + "/" + 
     public String getTime() { 
     return getHour() + ":" + getMinute() + ":" + getSecond(); 
     public int getDayOfMonth() { 
     return calendar.get(Calendar.DAY_OF_MONTH); 
     public int getDayOfYear() { 
     return calendar.get(Calendar.DAY_OF_YEAR); 
     public int getWeekOfYear() { 
     return calendar.get(Calendar.WEEK_OF_YEAR); 
     public int getWeekOfMonth() { 
     return calendar.get(Calendar.WEEK_OF_MONTH); 
     public int getDayOfWeek() { 
     return calendar.get(Calendar.DAY_OF_WEEK); 
     public int getHour() { 
     return calendar.get(Calendar.HOUR_OF_DAY); 
     public int getMinute() { 
     return calendar.get(Calendar.MINUTE); 
     public int getSecond() { 
     return calendar.get(Calendar.SECOND); 
     public static void main(String args[]) { 
     JspCalendar db = new JspCalendar(); 
     p("date: " + db.getDayOfMonth()); 
     p("year: " + db.getYear()); 
     p("month: " + db.getMonth()); 
     p("time: " + db.getTime()); 
     p("date: " + db.getDate()); 
     p("Day: " + db.getDay()); 
     p("DayOfYear: " + db.getDayOfYear()); 
     p("WeekOfYear: " + db.getWeekOfYear()); 
     p("era: " + db.getEra()); 
     p("ampm: " + db.getAMPM()); 
     p("DST: " + db.getDSTOffset()); 
     p("ZONE Offset: " + db.getZoneOffset()); 
     p("TIMEZONE: " + db.getUSTimeZone()); 
     private static void p(String x) { 
     public int getEra() { 
     return calendar.get(Calendar.ERA); 
     public String getUSTimeZone() { 
     String[] zones = new String[] {"Hawaii", "Alaskan", 
     "Pacific","Mountain", "Central", "Eastern"}; 
     return zones[10 + getZoneOffset()]; 
     public int getZoneOffset() { 
     return calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000); 
     public int getDSTOffset() { 
     return calendar.get(Calendar.DST_OFFSET)/(60*60*1000); 
     public int getAMPM() { 
     return calendar.get(Calendar.AM_PM); 

Source for   Top of page

Here's the source for the JspSQL component:

  package util; 
  import java.util.*; 
  import com.sssw.shr.http.*; 
  import javax.servlet.http.*; 
  import javax.naming.*; 
  import java.sql.*; 
  public class JspSQL { 
     public String getSQL( 
        javax.servlet.http.HttpServletRequest req, String sql)  
        String result; 
        String srcname; 
        AgiHttpServletRequest agReq =  
           (AgiHttpServletRequest) req; 
        String name = agReq.getDatabaseURL().toString(); 
        int index = name.lastIndexOf("/"); 
        name = name.substring(index+1); 
        srcname = "Databases/" + name + "/DataSource"; 
        try { 
           InitialContext ctx = new InitialContext(); 
  //System.out.println("Looking up: "+srcname); 
           javax.sql.DataSource ds = 
           java.sql.Connection con = ds.getConnection(); 
  //System.out.println("jspSQL connection=" + con); 
           java.sql.Statement stmt = con.createStatement(); 
           java.sql.ResultSet rs = stmt.executeQuery(sql); 
           result =  
              "<TABLE BORDER=2 BORDERCOLOR=\"#808080\"  
              CELLPADDING=0 CELLSPACING=1> "; 
           int col; 
           // Output the column headers 
           ResultSetMetaData md =  rs.getMetaData(); 
           int numCols= md.getColumnCount(); 
           result += "<TR>";  
           for (col=1; col <= numCols; col++) 
              result += "<TD VALIGN=TOP><FONT COLOR=RED>"  
              + md.getColumnName(col) + "</FONT></TD>"; 
           result += "</FONT></TR>"; 
           // Loop thru all the rows 
           while ( 
              col = 1; 
              // Output the column data 
              result += "<TR>";  
              for (col=1; col <= numCols; col++) 
                result += "<TD VALIGN=TOP>" + rs.getString(col)  
                + "</TD>"; 
              result += "</TR>"; 
           result += "</TABLE><br>"; 
        catch (Exception e) 
           result = srcname + "<br>" + sql + "<br>" +  
              "Exception " + e + "<br>"; 
       return result; 

Coding the deployment XML file   Top of page

Here's the source for the datejsp.xml file:

     <URLs type="StringArray">  <!-- The list of URLs --> 
           <!-- URL"http://localhost/Examples3_HTML/ 
              jsptests/url1/date/date.jsp" --> 
     <JARs type="StringArray">  
           <!-- The list of dependency JARs --> 

Creating the batch file to deploy the JAR   Top of page

You can use a batch file to deploy a JSP JAR file to the SilverStream Server. In fact, to simplify the development process, you can perform all steps required to process your JSP page(s) in the same batch file. Then, any time you make a change to any JSP page or other resource in the JAR, you can simply execute the batch file to update the server.

The source for a sample batch file called build_jsp.bat is shown below. The file runs JspCompiler, compiles the generated Java source files, creates the JAR file, and runs DeployJSP, which deploys the JAR to the SilverStream server. It processes two JSP files, date.jsp and include.jsp, and two JavaBeans components, and

Here's the source for the build_jsp.bat file:

  cd date 
  \SilverStream35\bin\silvercmd JspCompiler -p date date.jsp include.jsp  
  set classpath=c:\SilverStream35\jre\lib\rt.jar;c:\SilverStream35\lib\;c:\SilverStream35\lib\servlet.jar;c:\SilverStream35\Docs\help\books\TechCode\jsptech;c:\SilverStream35\jre\lib\ext\jndi.jar;c:\SilverStream35\lib\ 
  cd util 
  cd date 
  del datetest.jar 
  jar cvf datetest.jar date/*.class util/*.class 
  jar tvf datetest.jar 
  \SilverStream35\bin\silvercmd deployjsp -f date\datejsp.xml -o localhost Examples3_HTML datetest.jar 

Deploying and testing the JSP pages   Top of page

To deploy the JSP pages and JavaBeans components to SilverStream, you need to run the batch file. Here's what you need to:

  1. Start the SilverStream server.

  2. Add the Examples3_HTML database to the server, if it has not been added already.

  3. At an MS-DOS command prompt, change to the jsptech directory:

      cd SilverStream35\Docs\help\books\TechCode\jsptech 
  4. Run build_jsp.bat from the command prompt.


NOTE   The build_jsp.bat file assumes that the SilverStream installation directory is c:\SilverStream35. If you installed SilverStream on a different drive or in a different directory, you need to modify the batch file to specify the correct location for the SilverStream installation.

Once the batch file has completed processing, you can test the JSP pages. To do this, you need to:

  1. Start a Web browser.

  2. Type the following URL:


Copyright © 2000, SilverStream Software, Inc. All rights reserved.