Solved

Interaction between Applet, Cookie e ASP.net doesn't works in Firefox

Posted on 2010-09-07
6
671 Views
Last Modified: 2012-05-10
Hi!

I have an Applet that gets the username through a cookie set by asp.net page (the page that contains the applet).

Java Code to get the cookie
------------------------------

    private void getUserNameFromCookie() {
        try {
            // get the cookies that are applicable for this applet's parent web page
            URL docBaseUrl = this.getDocumentBase();

            CookieHandler cookieHandler = CookieHandler.getDefault();
            java.util.Map<String, List<String>> headers =
                    cookieHandler.get(docBaseUrl.toURI(),
                    new HashMap<String, List<String>>());

            if (headers.isEmpty()) {
                System.out.println("No cookies found!");
            } else {
                getUserNameFromHeader(headers);
            }
        } catch (Exception e) {
            System.out.println("Unable to get cookie using CookieHandler");
            e.printStackTrace();
        }
    }

    private void getUserNameFromHeader(java.util.Map<String, List<String>> headers) {

        for (String key : headers.keySet()) {
            for (String value : headers.get(key)) {
                if (key.equals("Cookie") && value.startsWith("userName")) {
                    userName = value.split("=")[1];
                }
            }
        }
    }

Open in new window


ASP.net C# code to create the cookie (on Page_Load)
----------------------

            HttpCookie appCookie = new HttpCookie("userName");
            appCookie.Value = Session["user"].ToString();

            appCookie.Expires = DateTime.Now.AddDays(1);
            appCookie.HttpOnly = false;
            Response.Cookies.Add(appCookie);

Open in new window


In IE everything works nice, but in Firefox the operations that requires the username from cookie are not launched.

Any idea on how to resolve this?

Thanks in advance!
0
Comment
Question by:calypsoworld
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 14

Expert Comment

by:Daniel Junges
ID: 33624876
0
 
LVL 14

Expert Comment

by:Daniel Junges
ID: 33624908
0
 

Author Comment

by:calypsoworld
ID: 33631850
Thank you for reply.

Yes, cookies are enabled.
It's weird this works in IE and don't in Firefox....

Java console shows this information after applet's loading (and a lot of other things)

network: Connecting http://localhost:55000/page.aspx with cookie "__utma_a2a=6852338610.1305743161.1274186235.1274560427.1274878003.51; userName=testuser@mail.com"

Apparently there are two cookies in this page (I don't know where the first cookie come from).
Considering that are two cookies in page, am I getting it in the right way in Java?

Thank you.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:calypsoworld
ID: 33632030
I think that is the problem:

Information from Java console...

In IE
network: Connecting http://localhost:55000/page.aspx with cookie "userName=testuser@mail.com"

In Firefox
network: Connecting http://localhost:55000/page.aspx with cookie "__utma_a2a=6852338610.1305743161.1274186235.1274560427.1274878003.51; userName=testuser@mail.com"

Maybe I'm getting the wrong index in Java...
How should script the Java code to get the value in userName field, both in IE and Firefox?
Thanks in advance!
0
 

Accepted Solution

by:
calypsoworld earned 0 total points
ID: 33632651
Problem has been solved.

Thank you junges for your attention!

The problem is actually the what I said in previous post: Firefox contains an 'extra' cookie.
So, the method in Java to get the userName value, both to Firefox (2 cookies) or IE (1 cookie) is the same from the question, but with a simple adjustment.

    private void getUserNameFromHeader(java.util.Map<String, List<String>> headers) {

        for (String key : headers.keySet()) {
            for (String value : headers.get(key)) {
                if (key.equals("Cookie") ) {
                            userName = value.substring(value.indexOf("userName")+9);
                }
            }
        }
0
 
LVL 14

Expert Comment

by:Daniel Junges
ID: 33635103
very good.

regards junges
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
history of pages 7 50
What is GIS method of Geometry data type? 6 33
Issue in displaying table columns on selection of dropdown 2 27
JVM error from eclipse 1 25
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question