How to run JSP pages inside a SilverStream server.
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.
For this technique, you need the following files:
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.
Here's the source for the date.jsp sample page:
<html> <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> <ul> <li>Query string: <%= request.getQueryString() %> <li>Server name: <%= request.getServerName() %> <li>Server port: <%= request.getServerPort() %> <li>Remote address: <%= request.getRemoteAddr() %> </ul> <h1> Use a Bean to access date information</h1> <ul> <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"/> </ul> <h1> Call a function declared on the JSP page </h1> <%-- Function declaration --%> <%! public String getAString(String x) { return x + " was passed in"; } %> <ul> <li> call getAString: <%= getAString("Hello") %> </ul> <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"/> </html>
Here's the source for the include.jsp sample page:
<html> <b>This text is from include.jsp!</b> </html>
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(); calendar.setTime(trialTime); } public int getYear() { //System.out.println("JspCalendar.getYear()"); return calendar.get(Calendar.YEAR); } public String getMonth() { int m = getMonthInt(); String[] months = new String [] { "January", "February", "March","April","May","June","July","August", "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() + "/" + getYear(); } 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) { System.out.println(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); } }
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 = (javax.sql.DataSource)ctx.lookup(srcname); 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 (rs.next()) { 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; } }
Here's the source for the datejsp.xml file:
<obj_DeployJSP> <URLs type="StringArray"> <!-- The list of URLs --> <el>jsptests/url1</el> <!-- URL"http://localhost/Examples3_HTML/ jsptests/url1/date/date.jsp" --> </URLs> <JARs type="StringArray"> <!-- The list of dependency JARs --> </JARs> </obj_DeployJSP>
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, JspCalendar.java and JspSQL.java.
Here's the source for the build_jsp.bat file:
cd date \SilverStream35\bin\silvercmd JspCompiler -p date date.jsp include.jsp cd.. set classpath=c:\SilverStream35\jre\lib\rt.jar;c:\SilverStream35\lib\silverdesignerall.zip;c:\SilverStream35\lib\servlet.jar;c:\SilverStream35\Docs\help\books\TechCode\jsptech;c:\SilverStream35\jre\lib\ext\jndi.jar;c:\SilverStream35\lib\javax_sql.zip cd util javac JspCalendar.java javac JspSQL.java cd.. cd date javac date.java javac include.java cd.. 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
To deploy the JSP pages and JavaBeans components to SilverStream, you need to run the batch file. Here's what you need to:
cd SilverStream35\Docs\help\books\TechCode\jsptech
build_jsp.bat
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:
http://localhost/Examples3_HTML/jsptests/url1/date/date.jsp