Java Web Learning Chapter 1 Servlet and cookie

Source: Internet
Author: User

1. What is Cookie?

Cookie is actually a text file written by the Web server on the user's hard disk when the user accesses the website through a browser.

It contains some user information, which is stored in the form of key-value pairs

2. Typical applications using cookies

The following uses Cookie technology, which we often see.


3. Cookie Programming

Setpath is used to set the storage location.


The cookie constructor has two parameters, both of which are of the string type. They are preceded by a "key" and followed by a "value"

Setmaxage: Set the validity period. getmaxage gets the validity period.

Send Response. Add (...), that is, save the cookie object

Read Request. getcookies ()

Cookie. setvalue ()

4. Cookie Defects

5. programming example

Foreground cookieinput.htm

<HTML> 

  

Background

String processing class

Package webbook. util; public class stringutil {/*** determines whether the input string parameter is null. * @ Param ARGs the input string * @ return true/false */public static Boolean validatenull (string ARGs) {If (ARGs = NULL | args. length () = 0) {return true;} else {return false;}/*** determines whether the input string parameter is null or a "null" character, if yes, the target parameter is returned. If not, the source parameter is returned. */Public static string chanagenull (string source, string target) {If (Source = NULL | source. length () = 0 | source. equalsignorecase ("null") {return target;} else {return source ;}/ ***** filter <,>, \ n characters. * @ Param input the character to be filtered * @ return the string to be filtered */public static string filterhtml (string input) {If (input = NULL) {return NULL ;} if (input. length () = 0) {return input;} input = input. replaceall ("&", "&"); input = input. replaceall ("<", "<"); input = input. replaceall (">", ">"); input = input. replaceall ("", ""); input = input. replaceall ("'", "'"); input = input. replaceall ("\" "," "); Return input. replaceall ("\ n", "<br> ");}}

  

Servlet for Cookie setting

Package webbook. chapter6; import Java. io. ioexception; import Java. io. printwriter; import Java. text. simpledateformat; import Java. util. date; import webbook. util. stringutil; import javax. servlet. servletexception; import javax. servlet. HTTP. cookie; import javax. servlet. HTTP. httpservlet; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; public class setcookiesservlet exte NDS httpservlet {Private Static final long serialversionuid = response; Public void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {dopost (request, response );} public void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {string output = NULL; string username = request. getparameter (" Username "); If (! Stringutil. validatenull (username) {cookie cookie1 = new cookie ("username", stringutil. filterhtml (username); // The cookie is valid for one month cookie1.setmaxage (24*60*60*30 ); simpledateformat SDF = new simpledateformat ("yyyy-mm-dd"); cookie cookie2 = new cookie ("lasttime", SDF. format (new date (); cookie2.setmaxage (24*60*60*30); response. addcookie (cookie1); response. addcookie (cookie2); Output = "the current Logon Time and user name have been written To the cookie. <Br> <a href = \ "/webproject3/servlet/getcookies \"> View cookies </a> ";} The else {output =" username is blank. Please enter it again. <Br> <a herf = \ "/webproject3/cookieinput.htm \"> enter the user name </a> ";} response. setcontenttype ("text/html; charset = UTF-8"); printwriter out = response. getwriter (); out. println ("<HTML>"); out. println ("

  

Servlet for obtaining cookie

Package webbook. chapter6; import Java. io. ioexception; import Java. io. printwriter; import javax. servlet. servletexception; import javax. servlet. HTTP. cookie; import javax. servlet. HTTP. httpservlet; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; public class getcookiesservlet extends httpservlet {Private Static final long serialversionuid = response; Public void doget (httpservletrequest request, response) throws servletexception, ioexception {dopost (request, response );} public void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {response. setcontenttype ("text/html; charset = UTF-8"); printwriter out = response. getwriter (); out. println ("<HTML>"); out. println ("

  

Test results:

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.