Setting the cookies in jsp

Hi

Iam trying to add the cookie into local machine.Problems is i written cookie program in cookietest.jsp then i incude this jsp into index.jsp.When iam accessing the index.jsp cookies are not setting But when iam acessing cookietest.jsp cookies are adding.why index.jsp accessing time not adding?please help me how to achive this one.

below the code snippet

**********************cookietest.jsp***********************

<%@ page import="java.util.Date"%>

<%@ page import="java.net.*"%>

<%

Date now = new Date();

String timestamp = now.toString();

Cookie cookie = new Cookie ("Venkateswarlu", "ravi");

cookie.setMaxAge(365 * 24 * 60 * 60);

response.addCookie(cookie);

%>

<HTML>

<HEAD>

</HEAD>

<BODY>

</BODY>

</HTML>

**************************end*****************************

***********************index.jsp***************************

<jsp:include page="/cookietest.jsp"/>

Thanks for advace

Venkatesearlu Ravi

[1114 byte] By [-1805693921433274168a] at [2007-11-26 14:52:58]
# 1

According to the definition of <jsp:include

Includes a static file or the result from another web component. from:

http://java.sun.com/products/jsp/syntax/2.0/syntaxref2020.html#8828

your code should work. (I think).

However, you can also try JSTL tag ><c:import , instead of ><jsp:include>

appy77a at 2007-7-8 8:41:15 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

> According to the definition of <jsp:include

> Includes a static file or the result from another web

> component. from:

> http://java.sun.com/products/jsp/syntax/2.0/syntaxref2

> 020.html#8828

>

> your code should work. (I think).

>

No it shouldnt. If you would read that definition closely once again, note the words 'Includes a static file or the result from another web

component'

When one jsp includes another using the <jsp:include> tag, the jsp container executes the included jsp and includes the 'html output' of the

included jsp in the response stream.

Now the included jsp in question sets a cookie, which is a response header. But this header is valid and recognized by the browser iof and only if no content has yet been flushed. The web server may have flushed some response body from the original jsp before including the contents of the included jsp. And even if the original jsp doesnt have any content as in the example of the OP, you never know with jsps because remember the jsp gets converted to a servlet, compiled and then executed for html output.

ram.

Madathil_Prasada at 2007-7-8 8:41:15 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

So you mean the cookie jsp page will include starting of page?\

Actually this is requirment

I have to add the that cookie code snippet to every page.SO what i did is i placed the cookie code snippet into footer.jsp.This footer jsp will include every page..Here cookie is not adding.

Is there any solution.

-1805693921433274168a at 2007-7-8 8:41:15 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

> Actually this is requirment

>

> I have to add the that cookie code snippet to every

> page.SO what i did is i placed the cookie code

> snippet into footer.jsp.This footer jsp will include

> every page..Here cookie is not adding.

>

> Is there any solution.

I also tried this with JSTL c:import, since the JSTL1.1 spec says that c:import covers many of the shortcomings of jsp:include.

/p/cookies/include.jsp

<%@ page import="java.util.Date"%>

<%@ page import="java.net.*"%>

<%

Date now = new Date();

String timestamp = now.toString();

Cookie cookie = new Cookie ("Venkateswarlu", "ravi");

cookie.setMaxAge(365 * 24 * 60 * 60);

response.addCookie(cookie);

%>

/p/cookies/page1.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<c:import url="/p/cookies/include.jsp"/>

<html>

<head><title>Page 1</title></head>

<body>

<%request.getCookies();%>

</body>

</html>

/p/cookies/page2.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<c:import url="/p/cookies/include.jsp"/>

<html>

<head><title>Page 2</title></head>

<body>

<%request.getCookies();%>

</body>

</html>

But the result is the same as jsp:include as far as setting HTTP Headers (Cookies) is concerned.

So the conclusion is that you can't really set HTTP headers in include files.

Then, you can try it with Servlets instead of JSPs. But then, you'll need to call the servlet each time, before every JSP is invoked.

I don' know how that's done. Probably through Filters

(a wild guess)

Message was edited by:

appy77

appy77a at 2007-7-8 8:41:15 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...