Java servlet and Cookie use _jsp programming

Source: Internet
Author: User
Tags cookie names first string
This article describes the software and environment configuration 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 a servlet program. This article also gives the method and routines for writing and reading cookies to the user's hard disk through the Java servlet. It can realize the personalized Web page, online shopping basket, password authentication and so on on the internet.


The Java servlet is Java code that runs on a Web server that accepts user requests, processes them, and provides feedback to users. Its function is similar to CGI program, can realize many interactive effect in webpage, but more efficient than CGI program. Cookies are a small piece of information that a Web server writes to a specific directory of a user's computer when a user accesses a Web server, and a cookie class is available in the Java servlet to manipulate cookies. Writes a cookie to the user's computer at a specific time and can be used again when needed.

1. Software download

PC, install Windows 95/98, with an IP address (if no one can be randomly matched). There are two software developers: JDK1.3 and JSWDK1.0.1.

To Http://java.sun.com, click on the "Products & APIs" link and click to download "JavaTM 2 SDK, Standard Edition, v 1.3". The downloaded files are installed on your computer.

JSWDK1.0.1 only 763,414 bytes can be downloaded from the http://java.sun.com/products/jsp/download.html. Downloaded Jswdk1_0_1-win.zip with WinZip software to a directory, such as: D:app, this directory will automatically create a subdirectory: 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, the following figure click on the upper left corner of the window dos icon, select the Properties menu, in the next window to click on the "Memory" in the "Initial environment" Drop-down menu will be "automatic" to 2816, click the "Exit" button to exit the window, and then re-enter the DOS window. This setting only needs to be done once.

In this DOS window, run the following DOS command to set the environment variable:

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, the directory names in the DOS commands 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) to run Startserver.bat.

Attention:

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

Start Java com.sun.web.shell.Startup%1%2%d%5%6%7%8%9

rem Java com.sun.web.shell.Startup%1%2%d%5%6%7%8%9

To

REM Start Java com.sun.web.shell.Startup%1%2%d%5%6%7%8%9

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

If you are using an older version of JDK1.2, you do not need to modify the Startserver.bat, and another DOS window will appear after execution.

Displays a string of strings after running, and finally displays endpoint created:localhost/127.0.0.1:8080. Indicates that the normal boot is complete. However, it does not support queries for text segments and Chinese values in the database.

Open a browser, enter: http://your machine IP address: 8080, when the browser displays the page of the vacant province, the page can view the servlet example.
A cookie class is provided in the Java servlet with two parameters representing the name and value of the cookie, respectively. The cookie class provides various methods for setting the cookie's properties, such as the Setmaxage () method to set the cookie's lifetime. If the lifetime is negative, it disappears on behalf of the browser shutdown cookie. The lifetime is 0, representing a deletion cookie, a positive lifetime, and how many seconds the cookie exists.

The HttpServletResponse class in the servlet also provides a Addcookie () method that writes the created cookie to the user's computer.

For example, the following code writes two cookies to the user's computer, one named My1, the value is Hi1, the other is 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 Cookies ("My1", "hi1");

C.setmaxage (100*60);

Rp.addcookie (c);

C=new Cookies ("My2", "Hi2");

C.setmaxage (-1);

Rp.addcookie (c);

}

}

In the program, use cookies c=new cookies ("My1", "Hi1"), and create a cookie that is actually two pairs of 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), and write it to the user's computer. The Cookie my1 lifetime is 100*60 seconds, or 100 minutes, while my2 automatically disappears when the browser is closed.

In the previous DOS window, enter the command:

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

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

To set the environment variable.

Javac Xx.java Compiler Java Program

Copy *.class D:appjswdk-1.0.1examplesweb-infservlets

Copies the compiled program to the Java servlet Runtime directory.

* If you use the old version of JDK1.2, you will need to modify a file to run the Java servlet program:

D:appjswdk-1.0.1examplesweb-infservlets.properties

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

Xx.code=xx

Xx.initparams=foo

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

The GetCookies () method of the HttpServletRequest class allows you to read the cookie list from the appropriate user and place it in an array of cookie types. The names and values of individual cookies can be obtained by the GetName () and GetValue () methods of each cookie in the array.

The following example prints a list of cookie names and values written to the user's machine by the server where the servlet resides. If there is a cookie with the name my1, the value is added to a character X. It is similar to a counter that can simply count the user's access dictionaries during the lifetime of the cookie.

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;

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.