How to read and write Chinese in Servlet

Source: Internet
Author: User

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 ("}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.