*
The moose likes JSP and the fly likes getParameter problrm Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "getParameter problrm" Watch "getParameter problrm" New topic
Author

getParameter problrm

Confused
Ranch Hand

Joined: Jan 24, 2006
Posts: 54
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.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
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.
Chetan Parekh
Ranch Hand

Joined: Sep 16, 2004
Posts: 3636
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.


My blood is tested +ve for Java.
Confused
Ranch Hand

Joined: Jan 24, 2006
Posts: 54
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

Joined: Sep 16, 2004
Posts: 3636
can you post your code?
Confused
Ranch Hand

Joined: Jan 24, 2006
Posts: 54
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

Joined: Aug 15, 2004
Posts: 2874
Because your code is not thread-safe.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
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

Joined: Sep 16, 2004
Posts: 3636
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

Joined: Jan 24, 2006
Posts: 54
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" %>
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

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.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Confused
Ranch Hand

Joined: Jan 24, 2006
Posts: 54
can u guide me how will i use MVC pattern...
Chetan Parekh
Ranch Hand

Joined: Sep 16, 2004
Posts: 3636
Originally posted by Piyali Mallick:
can u guide me how will i use MVC pattern...


Courtesy of Ben Souther
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026


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

Joined: Jan 24, 2006
Posts: 54
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
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Chetan Parekh
Ranch Hand

Joined: Sep 16, 2004
Posts: 3636
Hey Ben Souther, Do you know that are great?
[ February 24, 2006: Message edited by: Chetan Parekh ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Aug 15, 2004
Posts: 2874
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

Joined: Jan 24, 2006
Posts: 54
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

Joined: Sep 16, 2004
Posts: 3636
Do as directed by Ben Souther in all JSP pages.
Confused
Ranch Hand

Joined: Jan 24, 2006
Posts: 54
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

Joined: Sep 16, 2004
Posts: 3636
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

Joined: Jan 24, 2006
Posts: 54
Ya.. Ive removed that ! tag from <% -- %> when I am decalring the variables..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getParameter problrm
 
Similar Threads
Difference between getAttribute(string) and getParameter(String)
In Weblogic, service() being called twice
accessing int value from previous page
how to get value in drop down while doing update
how to retrive a value from a check box on one page to another page containing a com