wood burning stoves 2.0*
The moose likes Servlets and the fly likes Initialization in doPost Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Initialization in doPost" Watch "Initialization in doPost" New topic
Author

Initialization in doPost

roopa sudhi
Greenhorn

Joined: Oct 31, 2001
Posts: 16
Hi i am trying to initialize parameters in doPost() method but the variables does'nt seem to get initialized.can anybody please help me???
My code looks like this
package Bidder.Agent;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import AuctionEngine.*;
public class agentFactory extends HttpServlet implements Runnable{
public String AuctionID=null;
public String Quantity=null;
public String User=null;
public String MPrice=null;
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
response.setContentType("text/html");
PrintWriter out=response.getWriter();AuctionID=request.getParameter("ActID");
Quantity= request.getParameter("Qty");
User= request.getParameter("user");
MPrice= request.getParameter("Maxprice");
agentFactory aF=new agentFactory();
Thread t=new Thread(aF);
t.start();
out.println("AgentRunning");
}
public void run()
{
System.out.println(AuctionID);
SetAgent ag=new SetAgent();
ag.initialize(AuctionID,Quantity,User,MPrice);
}

}
Michael Pearson
Ranch Hand

Joined: Mar 11, 2001
Posts: 351
Is your current output:
AgentRunning
null
roopa sudhi
Greenhorn

Joined: Oct 31, 2001
Posts: 16
Yes. Browser displays "Agent Running" but the agent throws null pointer exception as Auction ID is null. I am unable get the initialized values in run() method.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12809
    
    5
Your approach is fraught with difficulties that will prevent this design from working with more than a single user at a time. All sorts of bizarre things will happen when more than one person sends a request to the servlet. You need to study basic servlet design principles.
As far as your immediate problem goes, every time you get a parameter from the request, you should check it for a null value. I bet there is some difference between the names of the parameters your form creates and the names you are asking for.
Bill
roopa sudhi
Greenhorn

Joined: Oct 31, 2001
Posts: 16
Thanks for the advice i'll work on the basics.I still have a doubt.
This is what i was trying to accomplish
when a bidder sets an agent, request should be sent to invoke the agent and he should be able to continue browsing.The parameters names are correct because if i print the value of AuctionID in doPost() my browser shows the correct answer but if i use the same variable in the run method which gets initialized in the doPost method it shows null.
Michael Pearson
Ranch Hand

Joined: Mar 11, 2001
Posts: 351
You should review the Factory Pattern.
Factory
Let's try to simplify this..
Why not create a Bid object that contains the information entered by a unique user for a unique auction item?
You may want to store this Bid in a collection for the Auction the user is bidding on. Somehow this data needs to persist.
roopa sudhi
Greenhorn

Joined: Oct 31, 2001
Posts: 16
Thanks a lot for your kind reply michael
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Initialization in doPost