Use of java servlet and cookie


This paper introduces the software and environment configuration required for java servlet programming. As long as you have a PC with Windows 95 / 98 installed, you can develop the servlet program according to the steps in this article. This paper also gives the method and routine of writing and reading cookies to the user‘s hard disk through java servlet. It can realize the common personalized web pages, online shopping baskets, password verification and so on.

Java servlet is java code running on the web server. It can accept user requests, process them accordingly, and provide feedback to users. Its function is similar to CGI program. It can achieve many interactive effects in web pages, but it is more efficient than CGI program. Cookies are a small piece of information written into the specific directory of the user’s computer by the web server when the user accesses the web server. Cookie classes are provided in java servlet to operate cookies. The cookie is written to the user’s computer at a specific time and can be taken out for use when needed.

   1. Software download

PC, installed with Windows 95 / 98, with IP address (if not, you can configure any one). There are two development software: jdk1 3 and jswdk1 0.1。

To , click the “products & APIs” link to download “JavaTM 2 SDK, standard edition, V 1.3”. Install the downloaded files on your computer.

   JSWDK1. 0.1 only 763414 bytes, available from Download. Downloaded jswdk1_ 0_ 1-win. Zip use winzip software to decompress to a directory, such as D: app, under which a subdirectory will be automatically established: jswdk-1.0.1. At this time, 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, as shown in the figure below, click the DOS icon in the upper left corner of the window, select the attribute menu, click “memory” in the next window, change “automatic” to 2816 in the drop-down menu after “initial environment”, click “exit” to exit the window, and then re-enter the DOS window. This setting only needs to be done once.

Run the following DOS commands in this DOS window to set the environment variables:

   set CLASSPATH=c:jdk1.3bin;.

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

If your JDK 1.3 and windows operating system are installed in other directories, the directory name in DOS command should be modified accordingly.

Then use 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.


For jdk1 3, that is, the environment described in this article, needs to be modified Bat, the

   start java %1 %2 %3 %4 %5 %6 %7 %8 %9

   rem java %1 %2 %3 %4 %5 %6 %7 %8 %9

Replace with:

   rem start java %1 %2 %3 %4 %5 %6 %7 %8 %9

   java %1 %2 %3 %4 %5 %6 %7 %8 %9

If you are using an old version of jdk1 2. There is no need to modify startserver Bat, another DOS window will appear after execution.

After running, a string of strings will be displayed, and finally endpoint created: localhost / will be displayed. Indicates that normal startup is completed. However, it does not support the query of Chinese fields and Chinese values in the database.

Open a browser and enter: http: / / your machine IP address: 8080. At this time, the browser displays the default page, where you can view the servlet example.
A cookie class is provided in java servlet. Its constructor has two parameters, which represent the name and value of the cookie respectively. Various methods are provided in the cookie class to set the properties of the cookie. For example, the lifetime of the cookie can be set through the setmaxage() method. If the lifetime is negative, it means that the browser closes and the cookie disappears. If the lifetime is 0, it means that the cookie is deleted. If the lifetime is positive, it means how many seconds the cookie exists.

At the same time, the httpservletresponse class in the servlet provides the addcookie () method, which can write the created cookie to the user’s computer.

For example, the following code can write two cookies to the user’s computer, one with the name my1 and the value hi1, and the other with the name my2 and the value Hi2

   import javax.servlet.*;

   import javax.servlet.http.*;


   public class xx extends HttpServlet


   public void service(HttpServletRequest rq, HttpServletResponse rp)

   throws ServletException,IOException

   { Cookie c;

   c=new Cookie(“my1″,”hi1”);



   c=new Cookie(“my2″,”hi2”);





In this program, use cookie C = new cookie (“my1”, “hi1”); A cookie is created, which is actually two paired strings. The first string represents the name of the cookie, and the second string represents the value of the cookie. Then use rp.addcookie (c); Write it to the user’s computer. The lifetime of cookie my1 is 100 * 60 seconds, i.e. 100 minutes, while my2 disappears automatically when the browser is closed.

Enter the command in the previous DOS window:

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

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

To set the environment variables.

   javac xx. Java compiler

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

Copy the compiled program to the java servlet running directory.

* if using the old version of jdk1 2. In order to run the java servlet program, you also need to modify a file:

Open the file with a text editor and add a few lines at the end:



Enter the IP address of http: / / machine in the browser: 8080 / examples / servlet / XX to write cookies to the user’s computer.

The getcookies () method of HttpServletRequest class can read the cookie list from the corresponding user and put it in the cookie type array. The name and value of each cookie can be obtained through the getname () and getValue () methods of each cookie in the array.

The following example can print out the list of cookie names and values written to the user’s machine by the server where the servlet is located. If there is a cookie named my1, add a character x to its value. It is similar to a counter, which can simply count the user’s access dictionary during the lifetime of the cookie.

   import javax.servlet.*;

   import javax.servlet.http.*;


   public class yy extends HttpServlet

   { Cookie x[];

   public void service(HttpServletRequest rq, HttpServletResponse rp)

   throws ServletException,IOException

   { int have=0;

   Cookie c;  

Recommended Today

Go custom error handling method

Go’s error is flexible However, the error processing mechanism is not easy to use. We can customize the error output:As long as all objects that implement the error () method are OK, here is a relatively simple demo, and then sort out an optimized package of error: package main import ( “fmt” ) type NameEmtpyError […]