Webpage:
<HTML>
<Body bgcolor = "pink">
<Center>
<Form method = "Post" Action = http: // 127.0.0.1: 8000/gift>
<Table>
<Tr>
<TD> enter Login Name: </TD>
<TD> <input type = "text" name = "loginid"> </TD>
</Tr>
<Tr>
<TD> enter password: </TD>
<TD> <input type = "password" name = "passwd"> </TD>
</Tr>
</Table>
<Input type = "Submit" value = "Submit">
</Form>
</Center>
</Body>
</Html>
Servlet section:
Import javax. servlet .*;
Import javax. servlet. http .*;
Import java. Io .*;
Public class giftservlet extends httpservlet
{
Public void Service (httpservletrequest req, httpservletresponse res) throws ioexception
{
Boolean cookiefound = false;
Cookie mycookie = NULL;
Cookie [] cookieset = Req. getcookies ();
Res. setcontenttype ("text/html; charset = gb2312 ");
Printwriter PW = res. getwriter ();
PW. println ("<HTML> ");
PW. println ("<body> ");
PW. println ("this is Hebei software Vocational and Technical College ");
PW. println ("<br> ");
Req. setcharacterencoding ("gb2312 ");
PW. println ("Hello:" + Req. getparameter ("loginid "));
If (cookieset! = NULL)
{
For (INT I = 0; I <cookieset. length; I ++)
{
If (cookieset [I]. getname (). Equals ("logincount "))
{
Mycookie = cookieset [I];
Cookiefound = true;
}
}
}
If (cookiefound)
{
Int temp = integer. parseint (mycookie. getvalue ());
Temp ++;
PW. println ("Congratulations !!!!!!!!, A gift is waiting you ");
PW. println ("the number of times you have logged in is" + String. valueof (temp ));
Mycookie. setvalue (string. valueof (temp ));
Int age = 60*60*24*30;
Mycookie. setmaxage (AGE );
Res. addcookie (mycookie );
Cookiefound = false;
}
Else
{
Int temp = 1;
PW. println ("this is the first time you have logged on to the server ");
Mycookie = new cookie ("logincount", String. valueof (temp ));
Int age = 60*60*24;
Mycookie. setmaxage (AGE );
Res. addcookie (mycookie );
}
PW. println ("</body> ");
PW. println ("}
}