Jsp, Iframes, and servlets

jamieg99 used Ask the Experts™
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.


Watch Question

Do more with

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

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


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">
<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">

and the servlet contains this:

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);

     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);


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

Java Developer
Top Expert 2010
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

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


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

No worries.
Thanks for the points :)

Brainbench MVP for Java 1

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