Jsp, Iframes, and servlets

jamieg99
jamieg99 used Ask the Experts™
on
I have a jsp page with 2 iframes on the page which both call the same servlet with different parameters. When I load each Iframe individually, the servlet loads properly, but when the browser window is refreshed -- loading both Iframes simultaneously, Strings values which are set based on the servlet's input parameter, switch servlets. This switch happens when the servlet attempts to connect to the database.

For example, I pass String A to servlet 1, and String B to servlet 2. When both Iframes are loaded together, I println on Servlet 1 and the String A = String A initially. After a DB call is made in Servlet 1 String A = String B from Servlet 2. Wierd.

It's as though there's one single thread for both servlets and they're confusing the data. I can get around the problem by giving the servlets different names, but I am curious to know why this is happenning in the first place.


Thanks for any advice anyone can offer.


-Jamie

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mick BarryJava Developer
Top Expert 2010

Commented:
Can you post the code for your servlet.
Sounds like it may not be handling concurrent calls ok.
Does it include any static variables?

Author

Commented:
sure ....

the jsp page calling the servlet looks like this....

<div id="c" style="visibility:hidden;position:absolute;">
   <iframe id="iframe1" src="/atlast/servlet/alutil.AttributeForm?pageID=1&rn=<%=rand1%>"  scrolling="Yes" height="250" width="400">
   </iframe>
</div>
<div id="p" style="visibility:hidden;position:absolute;">
   <iframe id="iframe2" src="/atlast/servlet/alutil.AttributeForm?pageID=2&rn=<%=rand2%>" scrolling="Yes" height="250" width="400">
   </iframe>
</div>


and the servlet contains this:

//BEGIN
private String ptype = "";
private String pageID = "";

pageID = request.getParameter("pageID");
if(pageID.equals("1")) ptype = "cust";
else if(pageID.equals("2")) ptype="post";
else ptype="test";

out.print("A=" + ptype);

try{
     DBConnect dbc = new DBConnect();
     Connection con = dbc.openConn();
     Statement stm = con.createStatement();
     ResultSet rs = stm.executeQuery("select * from TBL_ATTRIBUTES_FORM where ATTR_TYPE=" + pageID + " ORDER BY ID");
     out.print("B=" + ptype);
}

//END


when iframe1 loads, A=cust and B=post. When iframe2 loads, A=post and B=post.


thanks!
Java Developer
Top Expert 2010
Commented:
The same instance of your servlet handles both requests.
As ptype is a member variable both requests are updating the same variable.

To fix it make ptype and pageID local variables instead of member variables.
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Mick BarryJava Developer
Top Expert 2010

Commented:
Member variables should be used with caution in servlets, and generally only to store servlet attributes, not request attributes.

Author

Commented:
excellent....that worked. Thanks very much objects.
Mick BarryJava Developer
Top Expert 2010

Commented:
No worries.
Thanks for the points :)

http://www.objects.com.au/staff/mick
Brainbench MVP for Java 1
http://www.brainbench.com

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial