8:59 pm - Thursday October 18, 2018

Cookie example program using getVersion()/setVersion() methods to Get/Set the version of current protocol

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

GetVersion.java


package com.candidjava;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
* Servlet implementation class getversion
*/
public class GetVersion extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public GetVersion() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
*      response)
*/
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
// TODO Auto-generated method stub
String str = req.getParameter("Name");
System.out.println(str);
// Cookie(key,value);
Cookie ck = new Cookie("key", str);
PrintWriter out = res.getWriter();
res.setContentType("text/html");
// HttpServletResponse.addCookie(Cookie);
res.addCookie(ck);
ck.setVersion(1);
int t = ck.getVersion();
HttpSession ses = req.getSession(true);
ses.setAttribute("k1", str);
ses.setAttribute("k2", t);
res.sendRedirect("version.jsp");
out.close();
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
*      response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

}

web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Version</display-name>
<servlet>
<description>
</description>
<display-name>GetVersion</display-name>
<servlet-name>GetVersion</servlet-name>
<servlet-class>com.candidjava.GetVersion</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GetVersion</servlet-name>
<url-pattern>/GetVersion</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>version.html</welcome-file>
</welcome-file-list>
</web-app>

version.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body bgcolor="pink">
Your cookie's name
<%=session.getAttribute("k1") %><br></br>
your cookie's version is
<%=session.getAttribute("k2") %><br></br>
</body>
</html>

version.html


<html>
<body bgcolor="green">
<form action="GetVersion" method=get>
<p> Enter Your Name
<input type=text name="Name">
<input type=submit value="press">
</form>
</body>
</html>

output

output 1

output 2

download +Version

Filed in: Z-A All Java Codes

Comments are closed.