Spring Semester 2002


Lab Fourteen: Focusing on the last assignment.

In lab today please discuss the posted code for this assignment.

Here's some help with the last homework assignment:

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

public class One extends HttpServlet {

    public void doGet(HttpServletRequest req, 
                      HttpServletResponse res) 

        throws ServletException, 
               IOException {
        
        res.setContentType("text/html"); 
        PrintWriter out = res.getWriter(); 

        String one, two; 

        String what = req.getParameter("picture"); 

        if (what == null) {
            what = ""; 
        }

        if (what.equals("one")) { one = "One"; } else {
            one = "<a href=\"/examples/servlet/One?picture=one\">One</a>"; 
        } 

        if (what.equals("two")) { two = "Two"; } else {
            two = "<a href=\"/examples/servlet/One?picture=two\">Two</a>"; 
        } 

        out.println("<html><head><title>Mamma mia!</title></head><body bgcolor=\"white\">" + 
                    "<table border><tr><td>" + 

                    one + 

                    "</td><td>" + 

                    two +

                    "</td><td>Three</td><td>Four</td></tr>" + 
                    "<tr><td colspan=4 align=center>Picture</td></tr>" + 
                    "<tr><td colspan=4 align=center>Caption</td></tr>" + 
                    "</table></body></html>"); 
        
        

    }
}
This was for the portfolio (as a servlet) and you need a JSP version of it, too.

Now help for the calculator, as JSP.

<html><head><title>My calculator</title></head><body>

<% Integer a = (Integer)session.getAttribute("acc");

   if (a == null) { a = new Integer(0); } 

   String argument = request.getParameter("arg"); 

   if (argument == null) { argument = "0"; }

   int conv; 

   try { 
     conv = Integer.parseInt(argument); 
   } catch(Exception e) { conv = 0; } 

   a = new Integer(a.intValue() + conv); 

   session.setAttribute("acc", a);

 %>

 <form>
 
   The accumulator is now: <%= a %> <p> 

   <input type="text" name="arg"> <p> 

 </form>

</body></html>
This is only the starting point, and you need a servlet version of it as well.


Last updated on Apr 18, 2002, by Adrian German for A348/A548