This week's giveaways are in the MongoDB and Jobs Discussion forums.
We're giving away four copies of Mongo DB Applied Patterns and 4 resume reviews from Five Year Itch and have the authors/reps on-line!
See this thread and this one for details.
The moose likes Tomcat and the fly likes HttpSession problem when client on same server Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Products » Tomcat
Reply Bookmark "HttpSession problem when client on same server" Watch "HttpSession problem when client on same server" New topic
Author

HttpSession problem when client on same server

Rodger Donaphon
Greenhorn

Joined: Jan 23, 2009
Posts: 4
In my application, a java client program is accessing a servlet. Debugging the servlet I discovered that HttpSession.isNew() always returns TRUE when the client is on the same host as the servlet. The servlet returns XML data. The client is treating the servlet as an InputStream.

It works OK when the client is on a different machine, like my desktop PC. I access the servlet using a fully qualified domain name, but it is locally resolved. The final domain name is TBD and not yet on a DNS server.

I started using Tomcat5. Upgraded to Tomcat 6. No improvement.

According to my research, my problem can occur if the client refuses to accept the session. Not sure what that means. I tried using the HttpClient library, but it did not help.

For debugging, this is a BIG problem since all develop and unit test are on the same server.

To isolate the problem I am using the following simple test. processRequest simply returns the value of 'count'. Normally, it should increment each time the servlet is accessed. But when the client is on the same machine as Tomcat, it is stuck at 1 because httpSession.isNew() is always returning TRUE.



Any input would be appreciated.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56150
    
  13

Why do you care whether the session is new or not? Sounds like you might be trying to use the session lifecycle in an inappropriate manner.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Rodger Donaphon
Greenhorn

Joined: Jan 23, 2009
Posts: 4
The real application needs the servlet to maintain session variables. The example code with the 'count' session variable is just for troubleshooting purposes.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12265
    
    1
Exactly how is this "client program" accessing the servlet? What does the client program do to preserve cookies?


Bill

Java Resources at www.wbrogden.com
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56150
    
  13

But you really haven't answered the question. Why do you care if the session is new or not?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 14456
    
    7

Bear Bibeault wrote:But you really haven't answered the question. Why do you care if the session is new or not?


Huh? Because if the session is new, the session variables set in earlier requests aren't going to be there. It will be a blank session.

It sounds like one of 2 possibilities, neither of which actually depends on whether or not the client and server are on the same machine:

1. The failing client has cookies disabled and there's no URL rewriting in effect, so there's no place to pass the session ID back and forth between client and server. Since all HTTP requests are "one-off" with no enduring connection, there's no other way to keep the session identity in sync.

2. The client is doing low-level "brute force" HTTP. The HttpURLConnection can automatically handle cookies, but straight do-it-yourself network read/write logic would only handle the cookies with the session ID in them if the client implemented the low-level cookie logic explicitly.


Customer surveys are for companies who didn't pay proper attention to begin with.
Rodger Donaphon
Greenhorn

Joined: Jan 23, 2009
Posts: 4
Thanks for your input.

The problem is that I did not understand how cookies are used to maintain session.

Once I added code to save the JSESSION cookie and put it in subsequent requests, the problem went away.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: HttpSession problem when client on same server
 
Similar Threads
Kindly clarify "HttpSession.isNew()"
Accessing a Remote EJB in Glassfish from Tomcat
A query about HttpSession.isNew
404 Not Found - Servlet runs locally (GlassFish), but Not Found on web site
The body of the request is lost when you send the POST method