More on Servlets

Required Reading


Passing parameters on the URL

Our first example shows how to pass values to a servlet by encoding them into the web address of the servlet. The idea is that one passes the values "a,b,c,..." for the parameters "A,B,..." to the servlet "prog.sxml", by using the extended webaddress: prog.sxml?A=a&B=b&C=c&...&Z=z For example, the following html generates an answer page depending on which link the user clicks. The answer page is customized for the question by passing along some information as described above.

Test

  1. What is the capital of Panama?
The HTML for this code is:
<h1>Test</h1> <ol> <li> What is the capital of Panama? <ul> <li> <a href="answer.sxml?correct=no&answer=PanamaCity"> HatCity</a></li> <li> <a href="answer.sxml?correct=yes&answer=PanamaCity"> Panama City</a></li> </ul> </li> </ol>
The answer.sxml servlet reads the two parameters and uses them to generate a webpage.
(let ( (correct (.getParameter request "correct")) (answer (.getParameter request "answer")) ) <xml> <html><head><title>AnswerPage</title></head> <body bgcolor= <scheme>(if (equal? correct "yes") "blue" "red")</scheme> > <scheme> (if (equal? correct "yes") <xml> <h1>Good Job!</h1> The answer was indeed <scheme>answer</scheme> </xml> <xml> <h1>Nope!</h1> The answer was <scheme>answer</scheme> </xml> ) </scheme> </body> </html> </xml> )

(let (
  (correct (.getParameter request "correct"))
  (answer  (.getParameter request "answer"))
     )

  <xml>
    <html><head><title>AnswerPage</title></head>
    <body bgcolor= 
      <scheme>(if (equal? correct "yes") "blue" "red")</scheme>
      >
      <scheme>
       (if (equal? correct "yes")

         <xml> <h1>Good Job!</h1>
            The answer was indeed <scheme>answer</scheme>
         </xml>

         <xml> <h1>Nope!</h1>
            The answer was <scheme>answer</scheme>
         </xml>
       )
      </scheme>
     </body>
    </html>
  </xml>
)


Generating the Form and the Response with one Servlet

Here we give an example of a tip calculation servlet, tipcalc.sxml, that both generates the form to get the data from the user, and processes that data.
(let ( (bill (.getParameter request "bill")) ) (if (equal? bill #null) <xml> <html> <head><title>Tip Calc</title></head> <body bgcolor=lightgreen> <h1> Tip Calculator</h1> <form method=get action="tipcalc.sxml"> Total Restaurant Bill: <input type=text name="bill"> <br> <input type=submit value="Calculate Tip"> </form> </body></html></xml> <xml> <html><head><title>Tip Results</title></head> <body bgcolor=yellow> <h1> Your tip on a bill of amount <scheme>bill</scheme><br> is <scheme> (* (Double. bill) 0.15) </scheme> </h1> </body></html></xml> ) ; close the if );;;; this is a comment, close the let


Storing and Reading Data on the Serve4r

Tomorrow we will discuss in detail the following example test1.sxml, which maintains a counter and generates a log of all visitors to the site.
(begin (load "schemelib/forms.silk") (let ( (s (param request "status")) (data (read-string-from-file httpservlet request "log" "no log yet")) (count (read-from-file httpservlet request "count" 0))) (append-to-file httpservlet request "log" (list s (Date.) (.getRemoteHost request) )) (write-to-file httpservlet request "count" (+ 1 count)) <xml> <html> <head><title>Test</title></head> <body bgcolor=lightgreen> This is a test <xmp><scheme>data</scheme>
The counter has value count ))