JSP Fast Manual

© Zhou Qingqing 2001
http://www.cs.toronto.edu/~zhouqq

Last modified: 2002-05-03


1. Install (win2k)

1. Go to http://jakarta.apache.org/tomcat/tomcat-4.0-doc/appdev/index.html to download your files;

2. Add a system variable to your w2k system JAVA_HOME=c:\jdk1.3.1 by right click "my computer" icon and choose "property" menu bar; 

3. Start up by execute "startup.bat" and shutdown by execute "shutdown.bat";

4. Visit http://localhost:8080/index.html to verify the success.

กก

2. Basics

1. How Java Servlet works

Example (from SDK's example "Hello world" - HelloWorldExample.java):

import java.io.*;
...


public class HelloWorld extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<body>");
    out.println("<head>");
...

We can see that:

(1) Servlet is like CGI, which should construct the whole HTML page;
(2) To refer to it, the url should be the .java file name; To make it run, only .java file is needed;

2. How JSP works

Example (from SDK's example "Guess number" - numguess.jsp):

<jsp:useBean id="numguess" class="num.NumberGuessBean" scope="session"/>
<jsp:setProperty name="numguess" property="*"/>

<html>
<head><title>Number Guess</title></head>
<body bgcolor="white">
<font size=4>

<% if (numguess.getSuccess()) { %>

Congratulations! You got it.
And after just <%= numguess.getNumGuesses() %> tries.<p>

<% numguess.reset(); %>
...

We can see that:

(1) JSP is like ASP, which can mixed with HTML and use Beans(first three sentences);
(2) If we want to execute the function in the bean, use <% ... %>;
(3) The beans are in another directory as a Java class; (in this example, in \WEB-INF\... directory);
(4) To refer to it, the file name is the .jsp file name; To make it run, need .jsp and .java (for the beans) files;
(5) ==> so JSP is a more advanced technology than Servlet.

กก

3. My First program ( not "Hello, world!" )

My first program just want to a simple thing - let the user input a string, pass it to the TomCat Server, then output what the user's input. 

showstring.jsp

-------------------------

<jsp:useBean id="objname" class="show.showstring" scope="session"/> 
<jsp:setProperty name="objname" property="*"/>

<html>
<head><title>Show String</title></head>
<body>

Welcome to the Show String.<p>


<form method=get>
Just input a string here: <input type=text name=myinput>
<input type=submit value="Submit">
</form>

What you've inputed is : "<%= objname.ShowMe() %>". <p>

</body>
</html>

-------------------------

show/showstring.java 

-------------------------

package show;

import java.util.*;

public class showstring{

  String content;

  public void setMyinput(String myinput) {
    content = myinput;
  }

  public String ShowMe(){
    return content;
  }
}

-------------------------

Key points should be noted here (Marked by bold/italic characters):

(1) Don't forget the first two sentences in .jsp. "id" is the name of your object, "class" is the name of your class ( full name with package ), "name" use the same name as "id";
(2) showstring.setMyinput(String myinput) is a strict name - the format should be setXXX(String XXX). And XXX is the same as .jsp's input form's input's name.
(3) The directory of the .jsp file and .java file (compile to .class) can be found by the server. In my program, I just put the .java "jakarta-tomcat-4.0.3\webapps\examples\WEB-INF\classes\show" and put the .jsp in "jakarta-tomcat-4.0.3\webapps\examples\jsp\showstring". These two folders are the same folders are as the same as the examples in TomCat package.
(4) You will find TomCat will create a new dir "jakarta-tomcat-4.0.3\work\localhost\examples\jsp\showstring" for you where your .class and .java will be copied. It is the TomCat's working directory. 

กก

4. Work with XML

Example (revised one based on http://61.144.28.245/hjc/web/doc/servlet-jsp/71.html by lhcyf)

-------------------------

<?xml version="1.0" encoding="ISO-8859-1"?>
<% java.util.Calendar cal = java.util.Calendar.getInstance(); %>
<Flights>
<AirCarrier>AA</AirCarrier>
<FlightNumber>700</FlightNumber>
<FlightSegment>
<DepartingInfo>
<City>DFW</City>
<Date>30Mar</Date>
<Scheduled>
<% out.print(cal.getTime().toString()); %>
</Scheduled>
<Status>ON TIME</Status>
<% cal.add(java.util.Calendar.MINUTE, 10);
out.print("<Time>" + cal.getTime().toString() +"</Time>"); %>

<Gate>C16</Gate>
<OffGateTime>0644A</OffGateTime>
<OffGroundTime>0632A</OffGroundTime>
</DepartingInfo>
</FlightSegment>
</Flights>

-------------------------

JSP can work with HTML, so it can work with XML, too. One thing should be noted is that the first JSP code should be the 2nd line since a well-formed XML requires that <?xml ...> at the first line.  

กก

Links

1. http://www.chinajavaworld.com/ - things about Java (in Chinese);

2. http://61.144.28.245/hjc/web/doc/servlet-jsp/servlet-jsp4.html - many good introductory articles about JSP and Servlet;

3. http://61.144.28.245/hjc/web/doc/servlet-jsp/h1.html , 57.html - what is JSP/Servlet, what's the difference;

4. http://groups.google.ca/groups?hl=en&lr=lang_zh-CN|lang_en&group=weblogic.developer.interest.jsp - JSP newsgroup;