7:57 pm - Thursday April 25, 2019

Cookie example program using getSecure()method to Check whether the browser send files only throw a secure 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

GetSecure.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 getsecure
*/
public class GetSecure extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public GetSecure() {
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 c = new Cookie("key", str);
boolean s = c.getSecure();
PrintWriter out = res.getWriter();
res.setContentType("text/html");
HttpSession ses = req.getSession(true);
ses.setAttribute("s1", s);
ses.setAttribute("s2", str);
res.sendRedirect("getsecure.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>GetSecure</display-name>
<servlet>
<description>
</description>
<display-name>GetSecure</display-name>
<servlet-name>GetSecure</servlet-name>
<servlet-class>com.candidjava.GetSecure</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GetSecure</servlet-name>
<url-pattern>/GetSecure</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>Home.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

Home.html

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

getsecure.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>display page</title>
</head>
<body bgcolor="pink">
your cookie is not sent as a secure one<br></br>
<%= session.getAttribute("s1")%><br></br>
Your cookie is set to
<%=session.getAttribute("s2")%><br></br>

</body>
</html>

output

output 1

download+GetName

Filed in: Z-A All Java Codes

Comments are closed.