• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getParameter problrm

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We have a web program which carries a value from the first logon page to the next pages. We are using getParameter() to pass this value to the next page.
My problem is:
Let's say 2 users Log in simultaneously (almost at the same time) the parameter value of one is shown for the other session, but if the diffrence in time is litte more it works fine.
Plz help me with a solution.

Thanks in advance.
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by that?
I believe, both would be seperate requests and both would get seperate sessions, no matter if they request log in simultaneously. Your application server can handle this atleast.
 
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Piyali Mallick:
We have a web program which carries a value from the first logon page to the next pages. We are using getParameter() to pass this value to the next page.
My problem is:
Let's say 2 users Log in simultaneously (almost at the same time) the parameter value of one is shown for the other session, but if the diffrence in time is litte more it works fine.
Plz help me with a solution.

Thanks in advance.



You have a first page that take username and password from the user, when user clicks on the login button you submit the form to a second page, where you are fetch username and password using request.getParameter(). Am I Right?

Now when two users try to login from the different PC, sometime their username and password getting swapped. Am I right?


If I am right at first instance, I MUST wrong at second instance.
If I am right at both instances, something MUST be wrong in your code.
 
Confused
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya ! u r correct. the code accepts user name and password , a location as against the user name is forwarded to the second page...the location value is picked from the database ... this location value is getting overlapped when both user submit at almost same instance.

plz tell me what can be wrong in my code...
Im using request.getParameter to fatch the posted value...
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you post your code?
 
Confused
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya Sure..
here is my code

<%@ page language="java" import="java.sql.*" errorPage="error_page.jsp" %>
<%!
Connection con;
Statement stmt;
ResultSet rs;
String driver;
String db;
String user_id;
String password;
String location="";
int flag;
%>
<%
response.setHeader("Cache-Control","no-cache"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", 0); //prevents caching at the proxy server
%>



<%
flag=0;
user_id=request.getParameter("txtEmpId");
password=request.getParameter("txtPass");
out.println(password);
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc racle:thin:@Ip address:1521:SID","user", "password");
stmt=con.createStatement();
String sql ="";
sql ="SELECT * FROM table name WHERE user_ID ='"+user_id+"' AND EMPLOYEE_PASSWORD='"+password+"' ";
rs=stmt.executeQuery(sql);

out.println(sql);

}catch(Exception e)
{
out.print(e);
}
%>
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because your code is not thread-safe.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would suggest to practice MVC2 architecture. Never do DB stuff in your JSP. JSP is for presentation. DB code should be atleast somewhere in your normal java classes. Not in JSP, nor in servlet.
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Adeel Ansari:
Because your code is not thread-safe.



This is the problem, it should be solved by puting following line in your JSP file.

 
Confused
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ive used it like this ...but its not solving... the error is still coming..

<%@ page language="java" import="java.sql.*" isThreadSafe="true" errorPage="error_page.jsp" %>
 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Because your code is not thread-safe.



Good catch. Looks like your application has been poorly designed with all those database access code in jsp page.

Use MVC pattern.
 
Confused
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can u guide me how will i use MVC pattern...
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Piyali Mallick:
can u guide me how will i use MVC pattern...



Courtesy of Ben Souther
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


All your java code(including database access code) in seperate java file.

After getting input from user use servlet to call the java file that accesses the database and get the result and store it in most appropriate scope(application/session/request) and forward/include the request to output jsp page.

Use jsp page only for presentation purpose as stated earlier by our fellow ranchers.
 
Confused
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya I understand that JSP is good for presentation purpose... I will try to use MVC ... I never used it..

so for the time being can I use my current code without getting that error ?
Is there any way
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your problem is here:


By putting all of your declarations within a <%! ... %> block, you've created all of your variable as instance variables.
Since there will only be one instance of the generated servlet for this JSP, those variables will be shared among all requests (which is what Adeel means by 'not thread safe').

Declare your variables within a <% ... %> block and they will end up being declared within the service method of the generated servlet. They will then be thread safe.
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Ben Souther, Do you know that are great?
[ February 24, 2006: Message edited by: Chetan Parekh ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, the usage of isThreadSafe listed here is backwards.
If you know your page is not thread safe, you would let the container know this by declaring isThreadSafe="false".
You would only use isThreadSafe="true" if you know that your page is threadsafe.

Moreover, since SingleThreadedModel has been deprecated and, since this is the most common method that containers use to handle pages with an isThreadSafe="false" declaration, it is recommended that you NOT use this declaration.

From JSP.1.10.1 (link to thes spec can be found in my signature)

[ February 24, 2006: Message edited by: Ben Souther ]
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chetan Parekh:
<%@ page isThreadSafe="true" %>



you mean to say, bluff the container.
[ February 28, 2006: Message edited by: Adeel Ansari ]
 
Confused
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I make my JSP page threadSafe...Actually ..currently Im working with a old project ... which is made long time back... its a not a matter of one/two JSP page(s)... the package contains atleast 40-50 JSP pages...
.. It is working fine ... but the only probs is some times when a user logs in and sees others details.....But it occurs some times...
[ March 02, 2006: Message edited by: Piyali Mallick ]
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do as directed by Ben Souther in all JSP pages.
 
Confused
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ive testing like the way Ben Souther directed.. by declaring isThreadSafe="true" / isThreadSafe="false" in the top of the page.. ... but its not happing...still two persons are getting one location who one is pressing submit button first in the gap of 2-3 seconds.Apart from declaring the syntax on the top of the page , do i need to add anything else to my code... b'coz i want this much assurance that one user should not allow to see other details whether he/she is inputing the data or viewing the data...

Plz note : my probs is if they are login at same time other wise its working fine. but is pretty risky... I can not take chance

How I can get that assurence.. can u provide me a proper code where user is putting values..say three or four continous form and simitnouly viewing the data by choosing "Back" and "Forward" button.
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am talking about following post

Have you done that?

**************************************************************************
Your problem is here:

code:
--------------------------------------------------------------------------------

<%!Connection con;Statement stmt;ResultSet rs;String driver;String db;String user_id;String password;String location="";int flag;%>

--------------------------------------------------------------------------------



By putting all of your declarations within a <%! ... %> block, you've created all of your variable as instance variables.
Since there will only be one instance of the generated servlet for this JSP, those variables will be shared among all requests (which is what Adeel means by 'not thread safe').

Declare your variables within a <% ... %> block and they will end up being declared within the service method of the generated servlet. They will then be thread safe.
 
Confused
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya.. Ive removed that ! tag from <% -- %> when I am decalring the variables..
 
reply
    Bookmark Topic Watch Topic
  • New Topic