Example Time!
Now enough of theory and let us get along with some examples from whatever we have learned so far,
The example shows how a HttpServletRequest is used to analyse the incoming request.
Following is a code snippet of Info.html,
<HTML>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080">
<h4> <p> Enter Your User Data </p></h4>
<h5> <p> And Enter GO ! to know all about you and your session info </p> </h5>
<form action="../com/cit/servlet/Info" method="post">
<p> First Name: <input type="text" size="20" name="n1" > </p>
<p> Last Name: <Input type="Text" size="20" name="n2" > </p>
<p> Password: <Input type="password" size="20" name="pw" > </p>
<p><input type="submit" name="button" value="GO !"></p>
</form>
</BODY>
</HTML>
Info.html should look as shown above.
Servlet output will look something like this,
To run the servlet you should create the folders under webapps and copy the files as follows,
\ cit \ jsp\ info.html
\ cit \ WEB-INF \ web.xml
\ cit \ WEB-INF \ classes \ com \ cit \ servlet \ InfoServlet.class
Pay close attention to how the information in info.html was obtained by the servlet, by using the method,req.getParameter(?paramName?),and returned back to the client. Also observe how servlet gets important client information,like header,session information.
|