Cookies in JSP
Introduction
Cookies are text files stored on the client computer and they are kept for various information tracking purposes. JSP transparently supports HTTP cookies using underlying servlet technology.
There are three steps involved in identifying and returning users −
Servlet Cookies Methods
Name | Description |
public void setDomain(String pattern) | This method sets the domain to which the cookie applies; for example, Google.com. |
public String getDomain() | This method gets the domain to which the cookie applies; for example, Google.com. |
public void setMaxAge(int expiry) | This method sets how much time (in seconds) should elapse before the cookie expires. If you don't set this, the cookie will last only for the current session. |
public int getMaxAge() | This method returns the maximum age of the cookie, specified in seconds, By default, -1 indicating the cookie will persist until the browser shutdown. |
public String getName() | This method returns the name of the cookie. The name cannot be changed after the creation. |
public void setValue(String newValue) | This method sets the value associated with the cookie. |
public String getValue() | This method gets the value associated with the cookie. |
Setting Cookies with JSP
Step 1: Creating a Cookie object
Cookie cookie = new Cookie("key","value");
Step 2: Setting the maximum age
cookie.setMaxAge(60*60*24);
Step 3: Sending the Cookie into the HTTP response headers
response.addCookie(cookie);
<%
Cookie firstName = new Cookie("first_name", request.getParameter("first_name"));
Cookie lastName = new Cookie("last_name", request.getParameter("last_name"));
firstName.setMaxAge(60*60*24);
lastName.setMaxAge(60*60*24);
response.addCookie( firstName );
response.addCookie( lastName );
%>
<html>
<head>
<title>Setting Cookies</title>
</head>
<body>
<center>
<h1>Setting Cookies</h1>
</center>
<ul> <li><p><b>First Name:</b> <%= request.getParameter("first_name")%> </p></li>
<li><p><b>Last Name:</b> <%= request.getParameter("last_name")%> </p></li> </ul>
</body>
</html>
<html>
<body>
<form action = "main.jsp" method = "GET">
First Name: <input type = "text" name = "first_name"> <br />
Last Name: <input type = "text" name = "last_name" />
<input type = "submit" value = "Submit" /> </form> </body>
</html>
Sessions in JSP
This interface provides a way to identify a user across.
Session Tracking in JSP
Session Tracking Techniques
There are four techniques used in Session tracking:
URL Rewriting :
�Hidden Form Fields
The session Object :
Connecting to Database using JSP
Step 1:Open a Command Prompt and change to the installation directory as follows
C:\> C:\>cd Program Files\MySQL\bin
C:\Program Files\MySQL\bin>
Step 2:Login to the database as follows
C:\Program Files\MySQL\bin>mysql -u root -p
Enter password: ******** mysql>
mysql> use TEST;
mysql> create table Employees
( id int not null, age int not null, first varchar (255), last varchar (255) );
Query OK, 0 rows affected (0.08 sec)
mysql>
Step4: Create Data Records: Let us now create a few records in the Employee table as follows
mysql> INSERT INTO Employees VALUES (100, 18, 'Zara', 'Ali'); Query OK, 1 row affected (0.05 sec)
mysql> INSERT INTO Employees VALUES (101, 25, 'Mahnaz', 'Fatma');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO Employees VALUES (102, 30, 'Zaid', 'Khan');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO Employees VALUES (103, 28, 'Sumit', 'Mittal'); Query OK, 1 row affected (0.00 sec)
mysql>
SELECT Operation using JSP
<%@ page import = "java.io.*,java.util.*,java.sql.*"%>
<%@ page import = "javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix = "c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix = "sql"%>
<html>
<head>
<title>SELECT Operation</title>
</head>
<body> <sql:setDataSource var = "snapshot" driver = "com.mysql.jdbc.Driver" url = "jdbc:mysql://localhost/TEST" user = "root" password = "pass123"/>
<sql:query dataSource = "${snapshot}" var = "result">
SELECT * from Employees;
</sql:query>
<table border = "1" width = "100%"> <tr> <th>Emp ID</th> <th>First Name</th> <th>Last Name</th> <th>Age</th> </tr>
<c:forEach var = "row" items = "${result.rows}"> <tr> <td><c:out value = "${row.id}"/></td> <td><c:out value = "${row.first}"/></td> <td><c:out value = "${row.last}"/></td> <td><c:out value = "${row.age}"/></td> </tr> </c:forEach> </table> </body> </html>
Output