• 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

what is the scope of JSP objects ???

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all ,
i again need help regarding scope of JSP's inbuild objects like out,response etc.
I have declared one recursive method in my jsp file in <%! %> tags and in that method i want to show some links using html anchor tag.for that i have to use out object. while using out object , it is giving error " undefined variable".
I don't know whether my approach is correct or not.
What could be best way for defining some recursive processing ?
please help
thanx in advance,
Deepika
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The thing to bear in mind when writing JSP is that a JSP is first converted to a regular Java class, then compiled. code in <%! %> is placed in the class body, and code in <% %> is placed in the service routine. Variables like out are only available in the service routine.
To use out in your methods, you have to pass it in:
 
Deepika Wadhwa
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Frank for your valuable help.
-Deepika
 
Deepika Wadhwa
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have one more query...
actually in my code i am writing the method as run() method for MyThread class. and I am passing three parameters for thread class constructor like
public MyThread(Connection con,Writer out,int no){
this.con=con;
this.out=out;
this.no=no;
}
what i believe is , in java the parameters are passed by reference, if they are object. so if I am calling this constructor from a separate jsp page. then it should show me all html data , which i have written in out.write() in the run() method.
but it is not showing anything thrown from this class .......
please help
thanx and regds
Deepika
--------------------------------------
| here is entire code for MyThread.java |
--------------------------------------
package chetana;
import java.sql.*;
import java.io.*;

public class MyThread extends Thread {
public Connection con;
public Writer out;
public int no;
public ResultSet rs;
public ResultSet rscount;
public Statement stmt1;
public Statement stmt2;

public MyThread(Connection con,Writer out,int no){
this.con=con;
this.out=out;
this.no=no;
}
public void run(){
int msgno;
int replycount;
try{
stmt1=con.createStatement();
rs = stmt1.executeQuery("select * from message where parentmessage="+no);
if(rs != null){
while(rs.next())
{
msgno =Integer.parseInt(rs.getString(1));
String subpass=rs.getString(2);
out.write("<a href=\"/chetana-testing/msg.jsp?no="+msgno+ "\">");
out.write(subpass+"</a>");
out.write(rs.getString(3));
stmt2= con.createStatement();
rscount= stmt2.executeQuery("select count(messageno) from message where parentmessage="+msgno);
rscount.next();
replycount=Integer.parseInt(rscount.getString(1));
out.write("("+replycount+")<br>");
// if this message is having child threads - ie replies
if (replycount >0) {
out.write(" --");
//recursive call to MyThread class object : to show the reply message as main message
(new MyThread(con,out,msgno)).start();
}
rscount.close();
stmt2.close();
}
}
rs.close();
stmt1.close();
}catch(Exception e){}
}
}
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Goodness that's a dangerous thing to do.
If you are writing output in a different thread you have no guarantee or control of the status of the Writer. I strongly suggest that you create some temporary place to put the output from your thread, and only ever write to the JSP Writer from the main thread of the JSP/Servlet.
For example, create a StringWriter in your JSP, pass it to your thread. Then in your JSP service routine, examine the contents of the StringWriter using toString() or getBuffer(), and write that to the JSP Writer.
 
Deepika Wadhwa
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks frank for your suggestion...
i have tried it using StringWriter as well as StringBuffer.
still its not showing any responses from different threads to browser.
what could be the reason?
please help
thanx n regds
Deepika
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure that your threads have actually run and produced any output by the time you ask the JSP for the data? Thread scheduling is a very complex topic, and the only assumption you can eaily make is that you can't make any assumptions.
 
Deepika Wadhwa
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i don't think i am assuming anything. If i replace my out.write() to System.out.println() . then it shows all expected results.which means that run method is written fine.
i am also getting confused.....

 
eat bricks! HA! And here's another one! And a tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic