Fall Semester 2002


Lab Notes Eleven: Servlets and JSP for the last two homework assignments

You know now what homework assignments five and six are: The problems you need to solve in these assignments will ask you to develop servlets and Java server pages under Tomcat. In the process we will need to understand the corresponding APIs very well. These documents are available with each installation of Tomcat, and the links that I provided above are essentially from my server.

Now let's get started.

Remember this?

I sure hope you do.

Let's implement this with servlets and Java server pages so we have something to refer to.

Let's do it (as always) in stages.

Stage One.

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*; 

public class Number extends HttpServlet {
    public void doGet(HttpServletRequest req, 
		      HttpServletResponse res) 
	throws ServletException, 
	       IOException {

	res.setContentType("text/html"); 
	PrintWriter out = res.getWriter(); 
	out.println("Hola, how are ya (today)?"); 

    }
}
Stage Two

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*; 

public class Number extends HttpServlet {
    public void doGet(HttpServletRequest req, 
		      HttpServletResponse res) 
	throws ServletException, 
	       IOException {

	res.setContentType("text/html"); 

	HttpSession who = req.getSession(); 

	PrintWriter out = res.getWriter(); 

	out.println("Session ID: " + who.getId() + 
		    "\n<br>Creation Time: " + who.getCreationTime() + 
		    "\n<br>Last Accessed: " + who.getLastAccessedTime()); 



    }
}
Stage Three

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*; 

public class Number extends HttpServlet {
    public void doGet(HttpServletRequest req, 
		      HttpServletResponse res) 
	throws ServletException, 
	       IOException {

	res.setContentType("text/html"); 

	HttpSession who = req.getSession(); 

        Integer count = (Integer)who.getAttribute("count"); 

        if (count == null) {
	    who.setAttribute("count", new Integer(1)); 
	} else { 
            int aux = count.intValue(); 
            aux += 1; 
            who.setAttribute("count", new Integer(aux)); 
	} 

	PrintWriter out = res.getWriter(); 

	out.println("Session ID: " + who.getId() + 
		    "\n<br>Creation Time: " + who.getCreationTime() + 
		    "\n<br>Last Accessed: " + who.getLastAccessedTime() + 
		    "\n<br>Count: " + count); 



    }
}
Stage Four

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*; 

public class Number extends HttpServlet {
    public void doGet(HttpServletRequest req, 
		      HttpServletResponse res) 
	throws ServletException, 
	       IOException {

	res.setContentType("text/html"); 

	HttpSession who = req.getSession(); 

        Integer count = (Integer)who.getAttribute("count"); 

        if (count == null) {
	    who.setAttribute("count", new Integer(1)); 
	} else { 
            int aux = count.intValue(); 
            aux += 1; 
            who.setAttribute("count", new Integer(aux)); 
	} 

        String one = req.getParameter("one"), 
  	       two = req.getParameter("two"); 


	PrintWriter out = res.getWriter(); 

	out.println("Session ID: " + who.getId() + 
		    "\n<br>Creation Time: " + who.getCreationTime() + 
		    "\n<br>Last Accessed: " + who.getLastAccessedTime() + 
		    "\n<br>Count: " + count + 
		    "\n<br>One: " + one + 
		    "\n<br>Two: " + two); 



    }
}
That's basically it as far as servlets go.

Now let's do the same with Java server pages.

Stage One

<html><head><title>JSP Stage One</title></head>
<body bgcolor="white">

  <%= new java.util.Date() %>

</body>
</html>
How do you access this?

What if you have errors?

What if you need to make changes?

Stage Two

<html><head><title>JSP Stage One</title></head>
<body bgcolor="white">

  <%= new java.util.Date() %>

  JSP has access to a number of predefined variables. <p> 

  Here are some: <ol>

    <li> <code>session</code> 

    <li> <code>request</code> 

    <li> <code>response</code>

  </ol> 

  <p> Let's use them. <p> 

  Here's your session ID: <%= session.getId() %> <p> 

  Here's the value of parameter named <code>two</code>: 

    <%= request.getParameter("two") %> <p> 

  Here's an <%! int count = 0; %>

  instance variable (<code>count</code>) 

  that's incremented every time:  <%= ++count %> <p> 

</body>
</html>
For the count variable we need a servlet equivalent (easy).

Everything else has been covered before.

Stage Three

<html><head><title>JSP Stage One</title></head>
<body bgcolor="white">

First we run the scriptlet. <p> 

<% Integer count = (Integer)session.getAttribute("count"); 

   if (count == null) {
     session.setAttribute("count", new Integer(1)); 
   } else { 
     int aux = count.intValue(); 
     aux += 1; 
     session.setAttribute("count", new Integer(aux)); 
   } 

 %>

<p> Which you can't see. <p> 

Then we print <%= count %> 


</body>
</html>
There's an issue of scoping here.

Let me clarify it with this example:

<html><head><title>JSP Stage One</title></head>
<body bgcolor="white">

A declaration: <%! int count = 6; %> (invisible) <p> 

A scriptlet: <% int count = 3; %> (invisible) <p> 

An expression: <%= count %> (prints 3, doesn't it?) <p> 

Another expression: <%= this.count %> (should print 6) <p> 

A scriptlet printing them both: <%
    out.println(count + " + " + this.count + " = three + six = 9 (nine)"); 
  %> 

</body>
</html>
We can easily describe this in plain Java.

Now the question is: hoes does this apply to the homework assignment?

Well, the assignment is asking you to:

  1. implement the calculator and portfolio using a Java servlet in Tomcat
  2. implement the calculator and portfolio using a Java server page in Tomcat

You should essentially find yourself in a familiar environment.

Servlets are like CGI with the added option of session management.

JSP are like PHP.

PHP and CGI are somewhat related.

JSP and Java servlet technology are even more related.

So essentially JSP is like PHP, where you provide the servlet in pieces.

These pieces are of three kinds:

  1. expressions
  2. declarations
  3. scriptlets

And they are being provided in the context of a larger, desired output.

Finally here's your:

A348/A548 LAB ASSIGNMENT ELEVEN

UNDERGRADUATES
Assume you are a graduate student (see below).

GRADUATES
Your task is the same as for the undergraduates:

  1. Solve all the problems/exercises from Lecture Notes Twenty
  2. Install the web chat application
  3. Look forward to the next lab assignment

Last updated: Oct 18, 2002 by Adrian German for A348/A548