• Post Reply Bookmark Topic Watch Topic
  • New Topic

Printing in methods other than doGet()

 
vijayaraghava mutharaju
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to send back a "result" string to the client side browser. Now, the problem is that it should be sent back from a method different from doGet(). Iam doing something like this................

public class A extends HttpServlet
{
PrintWriter out;
public void doGet() throws......................
{
out = response.getWriter();
..........................
}
public void someOtherMethod()
{
out.println(result);
}
}

The call in someOtherMethod() is not working. What should be done for this ?

Thank you.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not working ? What is not working ? Send us the Exception.

What I can say is:
1. Are you sending data via the GET method ? If not, doGet will not be called, and out will be null
2. You should not keep the PrintWriter as a class member. Pass it to your new function.
 
vijayaraghava mutharaju
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for the reply. Iam not getting any exceptions. Neither Iam getting the desired result. Nothing is getting printed in the client side browser.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've declared printWriter as an instance variable.
Don't do this.

Keep it local to your service method.
Then pass a reference to your other method.
 
Abhishek Asthana
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
Ben is right. Either you can pass the PrintWriter object to your otherMethod()



Even better will be if you pass HttpResponse object to otherMethod() and take the PrintWriter in otherMethod() only.

See if this works.Enjoy!

Abhishek.
 
vijayaraghava mutharaju
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thank you for the reply. I have tried it. Iam using a Timer class. Iam getting problems with that class. I can't call run() method directly.So I have tried this one but not working. Have a look at the code Iam using.......


public class event extends HttpServlet
{

private Timer t;
private static int count = 1;

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{

String eve;
String res;

eve=request.getParameter("opt");

EventAgent de=new EventAgent();
res=de.sendEventToEventAgent(eve);
PrintWriter out = response.getWriter();
out.println(res);
t = new Timer();
t.schedule(new GenerateRegNo(request,response),2,12000);
}



class GenerateRegNo extends TimerTask
{
private Random r;
private int no;
private String result;
private PrintWriter out;


public GenerateRegNo(HttpServletRequest req,HttpServletResponse
res) throws IOException
{
r = new Random();
out = res.getWriter();
}

public void run()
{
if(count <= 20)
{
DispReg();
count++;
}
else
t.cancel();

}

public void DispReg()
{
no = r.nextInt(3);

if(no == 0)
{
no = no + 11;
}
else
no = no + 10;

EventAgent de=new EventAgent();
result=de.sendEventToEventAgent(String.valueOf(no));
out.println(result);
}
}
}


Still not getting the desired result..........

Thank you once again.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!