![]() |
![]() Spring Semester 2004 |
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.
Stage Twoimport 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 Threeimport 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 Fourimport 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); } }
That's basically it as far as servlets go.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); } }
Now let's do the same with Java server pages.
Stage One
How do you access this?<html><head><title>JSP Stage One</title></head> <body bgcolor="white"> <%= new java.util.Date() %> </body> </html>
What if you have errors?
What if you need to make changes?
Stage Two
For the<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>
count
variable we need a servlet equivalent (easy). Everything else has been covered before.
Stage Three
There's an issue of scoping here.<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>
Let me clarify it with this example:
We can easily describe this in plain Java.<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>
Now the question is: hoes does this apply to the homework assignment?
Well, the assignment is asking you to:
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:
And they are being provided in the context of a larger, desired output.
A348/A548 LAB ASSIGNMENT ELEVEN