jsp by zhangyun


• Many servers (like JBoss and Glassfish)
  are Tomcat servers.
• The file structures and format for
  deployment are the same.
• Where (in what server directory) files get
  deployed varies.
• These jsp examples were developed on
         Webapps with only jsp
• Jsp are compiled and run as if they were servlets by
  tomcat, but you write only script code and the container
  does the rest.
• Jsp, css and html files will go at the root of the webapp
  and WEB-INF will contain just an empty web.xml:
      <?xml version="1.0" encoding="ISO-8859-1"?>
      <!DOCTYPE web-app
       PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
      <web-app><!—notice this is an empty tag? -->
Here is my (jsp) samples directory
   Creating/Running a jsp for JBoss (or
Create a directory named MyGreeting
Within the MyGreeting directory, create a subdirectory named WEB-INF (capitalization
       matters, so be precise, and that's a dash, not an underscore)
| +-- HelloWorld.jsp (*)
| +-- WEB-INF
+-- web.xml (*)
 (*) denotes a file

Create a JSP file named MyGreeting/HelloWorld.jsp
Add the following text to the HelloWorld.jsp file:
<%@ page language="java" %> <HTML> <BODY> <% for (int counter = 1; counter <=
      10; counter++) { %> Hello World!<BR> <% } %> </BODY> </HTML>

Create the web.xml file in MyGreeting/WEB-INF/web.xml
Add the following text to web.xml: note – this xml doc is basically empty.
(from code/Chapter1/MyGreeting/WEB-INF/web.xml)

<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-
     //Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
     "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"> <web-app> </web-app>
Put the directory into Tomcat webapps or create the war file.
To build the war:
Drop to a command line and change to the MyGreeting directory
Use the command-line jar utility, jarring up any jsp files as well as the WEB-INF
    dir into a war called MyGreeting:
c:\j2ee\code\Chapter1\MyGreeting> jar cvf MyGreeting.war *.jsp WEB-INF
Note that to jar additional files like html or css you could use
jar cvf MyGreeting.war *.jsp *.html *.css WEB-INF
jar cvf MyGreeting.war *.* WEB-INF
Copy MyGreeting.war to the deploy folder in your JBoss installation (or to
    webapps in tomcat):
c:\j2ee\code\Chapter1\MyGreeting> copy MyGreeting.war
Start JBoss
Open a web browser and go to http://localhost:8080/MyGreeting/HelloWorld.jsp
A very simple JBoss example
   url for a similar JBoss example
                using jsp
• http://www.centerkey.com/jboss/
• 1) Create Work Folder
• Create a folder called "HelloWorld"
  separate from your JBoss installation. For
  example, the full path of your work folder
  could be "C:\Projects\HelloWorld".
• 2) Create Startup Script below (or just navigate to the
  bin directory and click the run batch file)
• In the "HelloWorld" folder create a file named
  "JBoss.bat" with the following content: JBoss.bat
@echo off
  set JAVA_HOME=\Program Files\Java\jdk1.6.0_11
  set JBossHome=\Apps\JBoss\jboss-5.0.0.GA
  set Path=%JAVA_HOME%\bin;%Path%
  cd "%JBossHome%\bin"
      Write JSP File hi.jsp in the "HelloWorld― dir

This JSP simply displays a greeting along with the current date and time.

<html><head><title>JSP Test</title>
  String msg = "Hello, World.";
  <h2><%= msg %></h2>
  <%= new java.util.Date() %>
    Create Deployment Descriptor

  In the "HelloWorld" folder, create a sub folder called
  "WEB-INF", and in that folder create a file named
  "web.xml" as: web.xml
• <web-app>
  <display-name>Hello World</display-name>

    The deployment descriptor provides information to JBoss
    about your web application.
    Create WAR Builder & Deployer
• In the "HelloWorld" folder, create a file called "Deploy.bat" as:
@echo off
  set JAVA_HOME=\Program Files\Java\jdk1.6.0_11
  set JBossHome=\Apps\JBoss\jboss-5.0.0.GA
  "%JAVA_HOME%\bin\jar.exe" -cvf helloworld.war *.jsp WEB-
  copy helloworld.war "%JBossHome%\server\default\deploy"

    This script uses Java's JAR utility to zip up the appropriate contents
    into a WAR file.
       Test Your Web Page
• Run the "Deploy.bat" file.

 In a browser, open
 "http://localhost:8080/helloworld/hi.jsp" to
 see your web application run.
A jsp with parameter
               The order jsp, xml…
•   <?xml version="1.0"                  •   <!DOCTYPE HTML PUBLIC "-
    encoding="ISO-8859-1"?>                  //W3C//DTD HTML 4.0
•   <!DOCTYPE web-app                    •   <HTML>
•    PUBLIC "-//Sun Microsystems,        •   <HEAD>
    Inc.//DTD Web Application            •   <TITLE>Order
    2.2//EN"                                 Confirmation</TITLE>
•                                        •   <LINK REL=STYLESHEET
    "http://java.sun.com/j2ee/dtds/web   •      HREF="JSP-Styles.css"
    -app_2_2.dtd">                       •      TYPE="text/css">
                                         •   </HEAD>
•   <web-app>                            •   <BODY>
•   </web-app>                           •   <H2>Order Confirmation</H2>
                                         •   Thanks for ordering <I><%=
                                         •   </BODY></HTML>
                               Css for this example
BODY { background-color: #FDF5E6 }
A:hover { color: red }                                      •   Remember to build directory
H1 { color: #440000;
   text-align: center;
                                                                structure with web-inf containing
   font-family: Arial Black, Arial, Helvetica, sans-serif       web.xml and jsp and css at top
H2 { color: #440000;
   font-family: Arial, Helvetica, sans-serif                •   Jar into a war file and deploy in
H3 { color: #440000;                                            JBoss default server.
   font-family: Arial, Helvetica, sans-serif
UL { margin-top: 0;
   border-top-width: 0;
   padding-top: 0
DT { font-weight: bold;
PRE { font-size: 105%;
CODE { font-size: 105%;
.TOC { font-size: 90%;
     font-weight: bold;
     font-family: Arial, Helvetica, sans-serif
TH.COLORED { background-color: #FFAD00
TR.COLORED { background-color: #FFAD00
TH.TITLE { background-color: #EF8429;
         font-size: 28px;
         font-family: Arial, Helvetica, sans-serif;
        A bunch of jsp from the
           coreservlets text
• Along with an empty web.xml file, the css file
  from above, I packaged a bunch of the simple
  jsp examples from coreservlets as a war file and
  put them in JBoss. An example shown below.

To top