how to set a cookie and return an image in the same request

I have a landing page on another domain - i.e. www.domain1.com

in a page on that domain - there is the following:

<img src="http://www.domain2.com/g1.jsp">

(an image src that points to a jsp on another domain.)
 What the g1.jsp is supposed to do is to set the cookie on the client's machine and then redirect to a blank.gif image. The problem is that I cannot set a cookie on the client's machine and return an image without 1) doing something with the content header i believe. or 2) breaking up into two steps because a cookie is added to the reponse object and there can only be one response for each request.
rbrett2003Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
kennethxuCommented:
two options come to my mind:

option 1: (I didn't try this one myself, so cannot be 100% sure if it will work)
in the jsp page, you added to cookie to the response and then use response.sendRedirect() to the blank.gif image.

option 2: (I'm 100% sure it will work)
for this option to work. you must use serlvet, if you must use g1.jsp, then let it simply forward to the servlet. the idea is let the servlet set the cookie and then read the blank.gif file from file system and return it back as the content. I'm gettiing you a sample servlet code, you can easily customized for your need.

let me know if you have further enquires.
0
 
rbrett2003Author Commented:
i think i did this in principle but it didn't seem to work. If you have a simplified example, that would help.
0
 
cheekycjCommented:
can you post your code?

CJ
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
cheekycjCommented:
I have done this before (in a servlet)

CJ
0
 
kennethxuCommented:
sample code:

CookieImage.java
======================
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public final class CookieImage extends HttpServlet {

     public void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, java.io.IOException
     {
           InputStream in = null;
           OutputStream out = null;
           try {
                // use one of those below.
                // in = getServletContext().getResourceAsStream("/resource/path/to/image/file");
                in = new FileInputStream( "/path/to/image/file" );
                if( in != null ) {
                    out = new BufferedOutputStream( response.getOutputStream() );
                    in = new BufferedInputStream( in );
                    response.setContentType( "image/gif" );
                    int c; while( ( c=in.read() ) != -1 ) out.write( c );
                    return;
                }
           } finally {
                if( in != null ) try { in.close(); } catch( Exception e ) {}
                if( out != null ) try { out.close(); } catch( Exception e ) {}
           }
     }
}
0
 
kennethxuCommented:
forgot to mention that you need to add cookie somewhere before response.getOutputStream() call.
0
 
rbrett2003Author Commented:
I was doing the following:

in g1.jsp:

<%
  System.out.println("setting cookie");
Cookie cookie1 = new Cookie("abc", "confirm123");
  cookie1.setMaxAge(24 * 60 * 60);
  response.addCookie(cookie1);

  response.sendRedirect("blank.gif");
%>

when it returns, it returns the image, but didn't set the cookie.
Where in the above code do you set the cookie?

0
 
cheekycjCommented:
you don't want to use a redirect when you set a cookie.  the cookie will not get set.

use kennethxu's code and add the cookie code in it like so:

CookieImage.java
======================
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public final class CookieImage extends HttpServlet {

     public void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, java.io.IOException
     {
           InputStream in = null;
           OutputStream out = null;
           try {
               Cookie cookie1 = new Cookie("abc", "confirm123");
               cookie1.setMaxAge(24 * 60 * 60);
               response.addCookie(cookie1);

                // use one of those below.
                // in = getServletContext().getResourceAsStream("/resource/path/to/image/file");
                in = new FileInputStream( "/path/to/image/file" );
                if( in != null ) {
                    out = new BufferedOutputStream( response.getOutputStream() );
                    in = new BufferedInputStream( in );
                    response.setContentType( "image/gif" );
                    int c; while( ( c=in.read() ) != -1 ) out.write( c );
                    return;
                }
           } finally {
                if( in != null ) try { in.close(); } catch( Exception e ) {}
                if( out != null ) try { out.close(); } catch( Exception e ) {}
           }
     }
}

Delete your g1.jsp
add a servlet url mapping your web.xml to map g1.jsp to the CookieImage Servlet

CJ
0
 
rbrett2003Author Commented:
This doesn't work either. If I call CookieImage Servlet then it sets the cookie, but i cannot set the cookie this way:


if it's coming from the image src.

<html>
<head>
</head>
<body>
<img src="http://www.domain.com/path/to/CookieImage">
</body>
</html>

the servlet works fine, but it cannot seemed to be called from an image tag in a plain old html file.

0
 
rbrett2003Author Commented:
As i said before the image returns fine in the servlet above - but it will not set the cookie.

e.g.


File1                                               ->                       cookieImage servlet

(contains img src tag)                                                 sets cookie returns image, (doesn't work)

user sees image.
0
 
cheekycjCommented:
Map your servlet to a jsp in your web.xml

that will allow you to use.  I have done this before.  http://www.domain.com/path/to/CookieImage.jsp

in my web.xml on domain.com I map CookieImage.jsp to CookieImage Servlet.

CJ
0
 
rbrett2003Author Commented:
I think you misunderstand me. The above code i compile as a servlet (CookieImage.java). What difference does it make if I make it a servlet or a jsp? so the img src goes directly to a servlet.
0
 
cheekycjCommented:
Your Servlet can return content type image/gif or image/jpeg and output an image without a redirect.

A JSP returns content type text/html so you cannot output an image , you have to use a redirect.  When you do a redirect you lose your cookie that was set.

CJ
0
 
rbrett2003Author Commented:
But i'm not doing a redirect.

The html file with the image src calls the servlet directly -
<html>
<head>
</head>
<body>
<img src="http://www.domain.com/path/to/CookieImageServlet">
</body>
</html>

then, the CookieImageServlet sets the cookie and returns the image. Only the image returns, and the cookie isn't set. There is no jsp involved here. Am i missing something?
0
 
kennethxuCommented:
1. How do you example if the cookie is set? I mean how do you know it is not set?
2. Did you tried simple type the servlet url on browser's address bar? does it set cookie?

If you need to make your cookie available to other pages, you need to use cookie1.setPath("/");

Let us know.
0
 
rbrett2003Author Commented:
Well, I look into the cache and I can see the cookie there. You can try this yourself :

go to:

http://rjbcomputers.dyndns.org:8080/examples/servlet/TestServlet

and you should see an image, then check your cache and you will see the cookie.

Now, create a web page whose img src points to that servlet and when you delete the cookie,
and then load up the web page, you will see that the cookie is not set.
0
 
kennethxuCommented:
check the privacy setting of internet options, try set it to accept all cookies.
if you show the IE status bar, you'll see a Privacy Report icon when a cookies is rejected, double click to see the detail.
let us know.
0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
FarlanderCommented:
What was the resolution here ? I'm running into this same issue.

I've set up the sample servlet code from above. So, if I use IE, and open http://myserver/testservlet, image is returned and cookies are set. Now, make html page, with an image tag with src=http://myserver/testservlet, image comes back, but cookies are not set - at least, I can't see them under document.cookies in javascript.

Privacy settings are to allow all cookies - but nothing shows up. Any insight would be greatly appreciated.
0
 
rbrett2003Author Commented:
the CookieImage.java seemed to work for me. Just needed to make sure that the browser wasn't caching the cookies.
0
 
phil_powellCommented:
Setting a cookie and redirecting is standard for page redirection in all scripting languages.  What you might consider doing is doing an HTTP page cache flush:

<meta http-equiv=Pragma content="no-cache">

Use the JSP-equivalent perhaps using response.setHeader() before you load HTML content and that will flush the cache before full page load and thus render your cookie.

Phil
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.