• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Making Multiple Requests to HTTPServlet

 
Ranch Hand
Posts: 2119
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am pretty sure I asked to a long time ago but I cannot find the post.

Using jQuery

I have one HTTPServlet that I am making requests to from two different client sources.
If I make the request from source 1 first then a request from source 2 everything works.
Unless I try to make a request from source 1 again. When I do the data from source 2 appears again.
If I use source 2 first to make the request the requesting from source 1 will show source 2 data.
 
Sheriff
Posts: 67595
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Most likely scenario is using instance members in the servlet.
 
Bartender
Posts: 242
27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To expand on that, Java Servlets are actually a singleton. And therefore you should never use non-final class-level variables in a Servlet because they will share state with each other. This is especially dangerous considering that it can create a race condition which is extremely difficult to debug. It's also a security violation if you use it on data that needs to be secured.
 
Steve Dyke
Ranch Hand
Posts: 2119
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Zachary Griggs wrote:To expand on that, Java Servlets are actually a singleton. And therefore you should never use non-final class-level variables in a Servlet because they will share state with each other. This is especially dangerous considering that it can create a race condition which is extremely difficult to debug. It's also a security violation if you use it on data that needs to be secured.



Here is my code:

My servlet piece:


My two JSP's that make request:





My response JSP:



The servlet console output always shows the correct source. The hidden JSP field always shows the correct source.
However, the JSP alert only shows '0' when I call the source '0' code the first time(if I do not call the source '1' code first. Once the source '1' code is called the JSP alert always shows '1'.
I hope this helps.
 
Zachary Griggs
Bartender
Posts: 242
27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post your full servlet?
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If that hidden field has the value you expect then looking at the JSP is quite likely not relevant, at least until you figure out why the alert isn't working, so can you post the HTML page that is produced?
 
Steve Dyke
Ranch Hand
Posts: 2119
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Zachary Griggs wrote:Can you post your full servlet?



 
Steve Dyke
Ranch Hand
Posts: 2119
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:If that hidden field has the value you expect then looking at the JSP is quite likely not relevant, at least until you figure out why the alert isn't working, so can you post the HTML page that is produced?



I think I may have found the issue.
When source 1 is fired I load a div container with the response. This container will initialize several class definitions and assign values to them.

When source 2 is fired a different div container is loaded with the response. This container also initializes the same classes and assigns values to them.

I found out that it works if I call an empty method to the container just before I close the container.
 
Steve Dyke
Ranch Hand
Posts: 2119
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Most likely scenario is using instance members in the servlet.



I realize this is a very old post but I still have a question about it.

Does the following snippet form my servlet violate what you quoted?

 
Bear Bibeault
Sheriff
Posts: 67595
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Depends upon context. Where is this code located? If it's inside a method (which it must be given the references), then it's a local variable and not an issue.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic