?
Solved

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

Posted on 2010-09-07
6
Medium Priority
?
678 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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month15 days, 7 hours left to enroll

839 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