Sample Application: ImplementCookies
This is an example of a full working JSP application using JSP and Servlet. This program demonstrates how to create a simple login form which stores username and password in the browser even after the web page is closed. Users are provided with the option to decide whether to remember their username and password using a check box. On checking the check box the inputs will be stored and on unchecking it will be removed from the browser.
This is an example application for the implementation of cookies. The main purpose of cookies is to identify users and possibly prepare customized Web pages for them. When you enter a Web site using cookies, you may be asked to fill out a form providing such information as your name. This information is packaged into a cookie and sent to your Web browser which stores it for later use. The next time you go to the same Web site, your browser will send the cookie to the Web server. The server can use this information to present you with custom Web pages.
You can download the source of this Sample Application here.
This is illustrated based on the assumption that you are you're already familiar with JSP, Servlet technologies. and equipped with a Servlet-capable Web server.
Many large sites require you to register in order to use their services, but it is inconvenient to remember the username and password. Cookies are a good alternative for this purpose.
Apache Tomcat is the servlet container that is used in the official Reference Implementation for the Java Servlet and JavaServer Pages technologies. You can download the latest version of Apache Tomcat from here
/webapps/
/webapps/ImplementCookies/
/webapps/ImplementCookies/web/
/webapps/ImplementCookies/web/index.jsp
/webapps/ImplementCookies/web/META-INF/context.xml
/webapps/ImplementCookies/web/WEB-INF/web.xml
/webapps/ImplementCookies/src/
/webapps/ImplementCookies/src/java/
/webapps/ImplementCookies/src/java/CookiesServlet.java
Lets go over each one:
/webapps/
The /webapps/ directory is our web server document root.
/webapps/ImplementCookies/
/ImplementCookies/ is the subdirectory where our application is accessed by the browser.
/webapps/ImplementCookies/web
/web/ is the subdirectory where our JSP files and xml files have been kept.
/webapps/ImplementCookies/web/index.jsp
index.jsp will be the entry point of our application. The web browser will be accessing this via http://yourdomain.com/ImplementCookies. This page contains three fields a username text box, a password box and a check box Values stored in request object are taken here , you can see the values which are stored in the cookies in your web page.
/webapps/ImplementCookies/web/META-INF/context.xml
context.xml will specify the context path to our project. In our case context path is "/ImplementCookies"
/webapps/ReportGeneration/web/WEB-INF/web.xml
web.xml acts as the deployment descriptor for our application. It is held in the application's WEB-INF directory. It defines a number of parameters that are used when the web application is deployed into the Tomcat Servlet/JSP container.
/webapps/ReportGeneration/src/
This folder holds all the source of our web application.
/webapps/ImplementCookies/src/java/
java/This folder holds all the java files necessary for our application. Servlets are also stored in this directory. You can also store the files in a package structure.
/webapps/ReportGeneration/src/java/CookiesServlet.java
CookiesServlet.java is the servlet which stores the required information to the browser. First it will check the user's wish to store the information to the browser. If he/she needs to store the inputted values new cookie will be created and the value will be stored. In the other case the values will not be stored in the browser and if any values already exists that will be removed from browser.