my dog learned polymorphism*
The moose likes Servlets and the fly likes Serlvet Execution problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Serlvet Execution problem" Watch "Serlvet Execution problem" New topic
Author

Serlvet Execution problem

Ashwin Kumar
Ranch Hand

Joined: Apr 07, 2005
Posts: 78
I am calling a servlet from my swing application. The scenario is when ever the new the user login I have to retrive the user infomation from database and show it to the user.
So for the first time If User1 logs in and the servlet retrieves the information correctly. But say User2 logs in and the servlet shows same old data. I tried printing the user id in servlet, and thats perfectly right and also the query prints perfectly but somehow when I say ReusltSet.getRow it returns value 1, actually the value should be zero, because there is no data in the database for that user. So this means its picking up the old result set, so thats why all the values are old (i mean previous userids). I am wondering how can this haappen when the query is new and the result set is old. I am using WebSphere as server and If i redepoly and log in as user2 it retrieves the correct data.
Please give suggestions.


Preparing SCEA..<br />SCBCD 5.0<br />SCWCD 1.4<br />SCJP 1.4
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Sounds like you're storing something in an instance variable.
Post your servlet code.


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

Joined: Apr 07, 2005
Posts: 78
Here is the code:

public class RiskNeedsAdultFlagsStatusServlet extends HttpServlet {
String mlNum1 = null;
String mlNum2 = null;
int splitML = 1;
int splitGang = 2;
Vector adultStatusYesNoRowLoc = null;
Vector adultOffTypeOfWeapons = null;
String currentDate = null;
String value = "NO";
String code1 = null;
String code2 = null;

//*******STATUS VARIABLES*********//
ResultSet rsAdultStautsSql1 = null;
ResultSet rsAdultStautsSql2 = null;
ResultSet rsAdultStautsSql3 = null;
ResultSet rsAdultStautsSql4 = null;
ResultSet rsAdultStautsSql5 = null;
ResultSet rsAdultStautsSql6 = null;
ResultSet rsAdultStautsSql7 = null;
ResultSet rsAdultStautsSql8 = null;
ResultSet rsAdultStautsSqlLoc = null;
ResultSet selectAll = null;

Boolean adultStatusYesNoRow1 = null;
String adultStatusYesNoRow2 = null;
Boolean adultStatusYesNoRow3 = null;
String adultStatusYesNoRow4 = null;
Boolean adultStatusYesNoRow5 = null;
Boolean adultStatusYesNoRow6 = null;
Boolean adultStatusYesNoRow7 = null;

String resultStatusValue1 = null;
String adultStautsSqlLoc = null;
String gangNameCode = null;
String gangAssosType = null;
String gangName = null;


public Vector populateData(String mlKey, String mode) {
if(mlKey !=null){
if(mlKey.startsWith("A")){
mlNum1 = "A";
mlNum2 = mlKey.substring(splitML);
}
}
Vector result = new Vector();
if (mode.equalsIgnoreCase("POPULATE")){
rsAdultStautsSql1 =
RiskNeedsSQLReader.execSQLCommand(
"SELECT CUSFLG FROM TMADTA.GSCLNT WHERE MLNUM1='"
+ mlNum1
+ "' and MLNUM2 ="
+ mlNum2
+ " ");

try{
while(rsAdultStautsSql1.next() && rsAdultStautsSql1 != null){
resultStatusValue1 = rsAdultStautsSql1.getString(1);
}

if(resultStatusValue1 != null && resultStatusValue1.equals(value)){
adultStatusYesNoRow1 = new Boolean(false);
}
else {
adultStatusYesNoRow1 = new Boolean(true);
}

}
catch(SQLException se){
se.printStackTrace();
}

rsAdultStautsSql2 =
RiskNeedsSQLReader.execSQLCommand(
"SELECT SENDAT FROM TMADTA.CMMAST WHERE MLNUM1='"
+ mlNum1
+ "' and MLNUM2 ="
+ mlNum2
+ " ");

try{
while(rsAdultStautsSql2.next() && rsAdultStautsSql2 != null){
adultStatusYesNoRow2 = rsAdultStautsSql2.getString(1);
}
}
catch(SQLException se){
se.printStackTrace();
}

//Location pull down-- Statuses
adultStautsSqlLoc = "SELECT LCNAME FROM TMADTA.RNLUSF" ;
adultStatusYesNoRowLoc = RiskNeedsSQLReader.execSQLVector(adultStautsSqlLoc);

rsAdultStautsSql3 =
RiskNeedsSQLReader.execSQLCommand(
"SELECT MAX(DATUPD) FROM CMADTA.CMFLAG WHERE MLNUM1='"
+ mlNum1
+ "' and MLNUM2 ="
+ mlNum2
+ " and DESC = 'PC1210' ");
try{
int resultStatusValue3 = rsAdultStautsSql3.getRow();
if( resultStatusValue3 == 0){
adultStatusYesNoRow3 = new Boolean(false);
}
else {
adultStatusYesNoRow3 = new Boolean(true);
}

while(rsAdultStautsSql3.next() && rsAdultStautsSql3 !=null){
adultStatusYesNoRow4 = rsAdultStautsSql3.getString(1);
}
}

catch(SQLException se){
se.printStackTrace();
}

rsAdultStautsSql5 =
RiskNeedsSQLReader.execSQLCommand(
"SELECT GANGTC FROM TMADTA.GSCLNT WHERE MLNUM1='"
+ mlNum1
+ "' and MLNUM2 ="
+ mlNum2
+ " and GANGTC = 'Y' ");
try{
int resultStatusValue5 = rsAdultStautsSql5.getRow();
if( resultStatusValue5 == 0){
adultStatusYesNoRow5 = new Boolean(false);
}
else {
adultStatusYesNoRow5 = new Boolean(true);
}
}
catch(SQLException se){
se.printStackTrace();
}

rsAdultStautsSql6 =
RiskNeedsSQLReader.execSQLCommand(
"SELECT RELTYPE,GANGCD,MAX(DATUPD) FROM CMADTA.CMGNGI WHERE MLNUM1='"
+ mlNum1
+ "' AND MLNUM2 ="
+ mlNum2
+ " AND RELTYPE IN ('MEMBER' ,'ASSOC') GROUP BY GANGCD,RELTYPE ");
System.out.println("SELECT RELTYPE,GANGCD,MAX(DATUPD) FROM CMADTA.CMGNGI WHERE MLNUM1='"
+ mlNum1
+ "' AND MLNUM2 ="
+ mlNum2
+ " AND RELTYPE IN ('MEMBER' ,'ASSOC') GROUP BY GANGCD,RELTYPE");

try{
while(rsAdultStautsSql6.next() && rsAdultStautsSql6 !=null){
gangAssosType = rsAdultStautsSql6.getString(1);
gangNameCode = rsAdultStautsSql6.getString(2);
}
if(gangNameCode !=null){
code1 = gangNameCode.substring(0,2);
code2 = gangNameCode.substring(splitGang);
}
//GANG ASSO.TYPE
rsAdultStautsSql7 =
RiskNeedsSQLReader.execSQLCommand(
"SELECT GNGNAM FROM CMBALL.CMGANG WHERE GNGCTY='"
+ code1
+ "' and GNGCOD ='"
+ code2
+ "' ");
while(rsAdultStautsSql7.next() && rsAdultStautsSql7 != null){
gangName = rsAdultStautsSql7.getString(1).trim();
}
}
catch(SQLException se){
se.printStackTrace();
}

//Statuses
result.add(adultStatusYesNoRow1);
result.add(adultStatusYesNoRow2);
result.add(adultStatusYesNoRow3);
result.add(adultStatusYesNoRow4);
result.add(adultStatusYesNoRow5);
result.add(gangName);
result.add(gangAssosType);

}
return result;
}
/**
*
*/
public void doPost(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
try {
response.setContentType("application/x-java-serialized-object");
InputStream in = request.getInputStream();
ObjectInputStream inputFromApplet = new ObjectInputStream(in);

String mlKey = (String) inputFromApplet.readObject();
String mode = (String) inputFromApplet.readObject();

//System.out.println("The mlkey from servlet : " + mlKey);

displayFlagsStatusData(response, mlKey, mode);

}

} catch (Exception e) {
e.printStackTrace();
System.out.println("Exception Occured in the doPost Method" + e);
}
}

/**
*
* @param response
* @param mlKey
* @param mode
*/
protected void displayFlagsStatusData(
HttpServletResponse response,
String mlKey,
String mode) {
Vector result = populateData(mlKey, mode);
sendFlagsStatusData(response, result);
}


/**
*
* @param response
* @param result
*/
protected void sendFlagsStatusData(
HttpServletResponse response,
Vector result) {
ObjectOutputStream outputToApplet;

try {
outputToApplet = new ObjectOutputStream(response.getOutputStream());
outputToApplet.writeObject(result);
outputToApplet.writeObject(adultStatusYesNoRowLoc);
outputToApplet.flush();
outputToApplet.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Yes, you're using instance variables for everything.
Don't do that.

Servlet containers only instanciate one Servlet instance.
Each request is handled in a separate thread but they all have access to the same instance variables.

Make sure you declare all of your variables inside one of the service methods (doPost or doGet).


Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Another Tip:

If you are going to post more than a couple lines of code, nest them inside a set of UBB Code tags. This preserves your indenting and makes it much easier for someone to read it.
Ashwin Kumar
Ranch Hand

Joined: Apr 07, 2005
Posts: 78
Thanks Ben it works fine now....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Serlvet Execution problem
 
Similar Threads
Inspiration and help needed
Deleting duplicate rows using sql query
HttpSessions getting mixed up
READ_TIMEOUT error in Apache 2.0
Union