Lecture Notes Twenty-Seven: Approaching the Take-Home Final

The exam will contain a few problems. Two of the problems 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. The documents that describe them are indexed visibly in everyone's Tomcat main page. I am indexing my Tomcat's main page here for your reference. Now let's get started, of course.

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 final?

Likely for the final you may be asked 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.

The last part on the final will be about XML.

That's the topic for next week.


Last updated: Nov 29, 2001 by Adrian German for A348/A548