Java Servlet and Cookie usage

Source: Internet
Author: User
Tags cookie names

This article describes the software and environment configurations required for Java Servlet programming. As long as you have a PC with Windows 95/98 installed, you can follow the steps in this article to develop the Servlet program. This article also provides methods and routines for writing and reading cookies to users' hard disks through Java Servlet. It can be used to implement common personalized Web pages, online shopping baskets, and password verification on the Internet.


Java Servlet is the Java code running on the Web server. It can accept user requests, process them accordingly, and provide feedback to users. Similar to CGI programs, it can achieve a lot of interactive effects on web pages, but it is more efficient than CGI programs. Cookies are a small piece of information written by the Web server into a specific directory of your computer when you access the Web server. Java Servlet provides a Cookie class that allows you to operate on Cookies. You can write cookies to your computer at specific times and use them again when necessary.

1. Software Download

PC, install Windows 95/98, with an IP address (if not available, you can configure it as needed ). There are two types of software: JDK1.3 and JSWDK1.0.1.

Go to the http://java.sun.com and click the "Products & APIs" link to download "JavaTM 2 SDK, Standard Edition, v 1.3 ". Install the downloaded file on your computer.

JSWDK1.0.1 only has 763,414 bytes, which can be downloaded from http://java.sun.com/products/jsp/download.html. Download the jswdk1_0_1-win.zip to a directory, such as: d: app, the directory will automatically create a sub-Directory: jswdk-1.0.1. Then JSWDK 1.0.1 has been installed in the d: appjswdk-1.0.1 directory.

2. Start JSWDK 1.0.1

Open a DOS window. For example, click the DOS icon in the upper left corner of the window, select the attribute menu, and click "Memory" in the window that appears ", change "Auto" to 2816 in the drop-down menu after "initial environment", click "exit" to exit the window, and then enter the DOS window again. This setting only needs to be done once.

Run the following doscommand in the DOS window to set environment variables:

Set CLASSPATH = c: jdk1.3bin ;.

Set path = c: jdk1.3bin; c: windows; c: windowscommand

If your JDK 1.3 and Windows operating systems are installed in other directories, modify the directory name in the doscommand.

Then run the CD command in the DOS window to enter the JSWDK 1.0.1 installation directory (such as d: appjswdk-1.0.1) and run startserver. bat.

Note:

For JDK1.3, that is, the environment described in this article, you need to modify startserver. bat

Start java com. sun. web. shell. Startup % 1% 2% 3% 4% 5% 6% 7% 9

Rem java com. sun. web. shell. Startup % 1% 2% 3% 4% 5% 6% 7% 9

Changed:

Rem start java com. sun. web. shell. Startup % 1% 2% 3% 4% 5% 6% 7% 9

Java com. sun. web. shell. Startup % 1% 2% 3% 4% 5% 6% 7% 8% 9

If the old version of JDK1.2 is used, you do not need to modify startserver. bat. Another DOS window will appear after execution.

A string is displayed after running, and endpoint created: localhost/127.0.0.1: 8080 is displayed. Indicates that the normal startup is completed. However, it does not support query of Chinese fields and Chinese values in the database.

Open a browser and enter http: // your machine IP Address: 8080. Then, the browser displays the vacant province page. on this page, you can view the Servlet example.
Java Servlet provides a Cookie class. Its constructor has two parameters, representing the Cookie name and value respectively. The Cookie class provides various methods to set the Cookie attributes. For example, you can use the setMaxAge () method to set the Cookie survival time. If the survival time is negative, the browser closes the Cookie and disappears. If the survival time is 0, it indicates that the Cookie is deleted. If the survival time is positive, it indicates how many seconds the Cookie exists.

At the same time, the HttpServletResponse class in Servlet provides the addCookie () method to write the created Cookie to the user's computer.

The following code writes two cookies to your computer. One is named my1, the value is hi1, the other is named my2, and the value is hi2.

Import javax. servlet .*;

Import javax. servlet. http .*;

Import java. io .*;

Public class xx extends HttpServlet

{

Public void service (HttpServletRequest rq, HttpServletResponse rp)

Throws ServletException, IOException

{Cookie c;

C = new Cookie ("my1", "hi1 ");

C. setMaxAge (100*60 );

Rp. addCookie (c );

C = new Cookie ("my2", "hi2 ");

C. setMaxAge (-1 );

Rp. addCookie (c );

}

}

In this program, the Cookie c = new Cookie ("my1", "hi1"); is used to create a Cookie, which is actually two pairs of strings. The first string represents the Cookie name, and the second string represents the Cookie value. Then, use rp. addCookie (c); write the cookie to your computer. Cookie my1 will survive for 100*60 seconds, that is, 100 minutes, while my2 will automatically disappear when the browser is closed.

In the preceding DOS window, enter the following command:

Set path = c: jdk1.3bin; c: windows; c: windowscommand

Set CLASSPATH = c: jdk1.3bin; d: appjswdk-1.0.1libservlet.jar

To set environment variables.

Javac xx. java compile Java program

Copy *. class D: appjswdk-1.0.1examplesWEB-INFservlets

Copy the compiled program to the Java Servlet running directory.

* If the old version of JDK is used, you need to modify a file to run the Java Servlet program:

D: appjswdk-1.0.1examplesWEB-INFservlets.properties

Open the file in a text editor and add the following lines at the end:

Xx. code = xx

Xx. initparams = foo

Enter http: // server IP Address: 8080/examples/servlet/xx in the browser to write cookies to your computer.

You can use the getCookies () method of the HttpServletRequest class to read the Cookie list from the corresponding user and store it in an array of Cookie types. The getName () and getValue () methods of each Cookie in the array can be used to obtain the names and values of each Cookie.

The following example shows a list of Cookie names and values written to your machine by the server where the Servlet is located. If the Cookie is named my1, the value is added with a character x. It is similar to a counter, which can be used to calculate the dictionary of the user's access during Cookie survival.

Import javax. servlet .*;

Import javax. servlet. http .*;

Import java. io .*;

Public class yy extends HttpServlet

{Cookie x [];

Public void service (HttpServletRequest rq, HttpServletResponse rp)

Throws ServletException, IOException

{Int have = 0;

Cookie c;

Related Article

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.