Introduction to Servlet technology
Chapters
Working with Cookies
Cookies are text files stored in client computer which is used for tracking information. Java Servlets supports HTTP cookies.
Below are used for checking returning users:
- Server script send a set of cookies to browser. For example name etc.
- Browser stores below information in local machine for future reference.
- When browser sends any request to web server next time then it sends cookies information to server for identifying the user.
Servlet Cookies Methods
Below are the methods which is used to manipulate cookies:
Method & Description |
public void setDomain(String pattern)
|
public String getDomain()
|
public void setMaxAge(int expiry)
|
public int getMaxAge()
|
public String getName()
|
public void setValue(String newValue)
|
public String getValue()
|
public void setPath(String uri)
|
public String getPath()
|
public void setSecure(boolean flag)
|
public void setComment(String purpose)
|
public String getComment()
|
Setting Cookies with Servlet
Setting cookies with servlet involves following steps:
Creating Cookie object:
The Cookie constructor contain a cookie name and a cookie value which are strings.
Cookie cookie = new Cookie("key","value");
Setting the maximum age:
The setMaxAge specify how long (in seconds) cookie should be valid.
cookie.setMaxAge(60*60*24);
Sending Cookie into HTTP response headers:
The response.addCookie to add cookies in HTTP response header:
response.addCookie(cookie);
Below is the example of use of cookie in servlets:
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class CookieExample extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); // print out cookies Cookie[] cookies = request.getCookies(); for (int i = 0; i < cookies.length; i++) { Cookie c = cookies[i]; String name = c.getName(); String value = c.getValue(); out.println(name + " = " + value); } // set a cookie Cookie c = new Cookie("myCookie", "myCookieValue"); response.addCookie(c);
// delete a cookie
for (int i = 0; i < cookies.length; i++){ cookie = cookies[i]; if((cookie.getName( )).compareTo("myCookie") == 0 ){ cookie.setMaxAge(0); response.addCookie(cookie); out.print("Deleted cookie : " + cookie.getName( ) + "<br/>"); } out.print("Name : " + cookie.getName( ) + ", "); out.print("Value: " + cookie.getValue( )+" <br/>"); }
}
}
Description
This guide introduces Servlet technology and we will cover below topics
- What is a Servlet?
- Initial Setup
- Life Cycle
- Various Examples
- Client Request
- Server Response
- Http Status Codes
- Writing Filters
- Exception Handling
- Cookies Handling
- Session Tracking
- Database Access
- Packaging
- Internationalization
This is to the point introduction to the topic to get you started
Prerequisites
Working knowledge of Core Java is essential
Audience
Beginner to Java Servlet technology or students looking to brush up their skills quickly
Learning Objectives
Learn Java Servlets
Author: Subject Coach
Added on: 2nd May 2015
You must be logged in as Student to ask a Question.
None just yet!