Invoking a JSP from an external (non-Servlet) Java class.
5 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Sasha_Haghani
Posted On:   Monday, April 23, 2001 12:43 PM

Hi, I have a question about how I might call/invoke a JSP from another Java class (which is not itself a Servlet) using the Servlet/JSP container. For instance, I'd like to use a JSP to automatically generate the body of an e-mail message on a scheduled basis. In this situation, there wouldn't be corresponding Request or Session objects. Are there any methods that can be called on the Servlet/JSP container which would (a) trigger the compilation of the JSP page into a servlet (if needed) (b) return the response stream of the JSP (i.e.: service method). Alternatively, is there anyway to use the getRequestDispatcher or getNamedDispatcher methods to get a handle to a JSP. While, I'd like to use a standard container-neutral    More>>

Hi, I have a question about how I might call/invoke a JSP from another Java class (which is not itself a Servlet) using the Servlet/JSP container.



For instance, I'd like to use a JSP to automatically generate the body of an e-mail message on a scheduled basis. In this situation, there wouldn't be corresponding Request or Session objects.



Are there any methods that can be called on the Servlet/JSP container which would (a) trigger the compilation of the JSP page into a servlet (if needed) (b) return the response stream of the JSP (i.e.: service method).



Alternatively, is there anyway to use the getRequestDispatcher or getNamedDispatcher methods to get a handle to a JSP.



While, I'd like to use a standard container-neutral facility for doing this, I'd also like to hear about container-specific alternatives which might only work on Tomcat, BEA WebLogic, etc.



Any help is much appreciated.

   <<Less

Re: Invoking a JSP from an external (non-Servlet) Java class.

Posted By:   Tomas_Zeman  
Posted On:   Tuesday, October 1, 2002 02:49 PM

You can use URL class to get JSP response via http protocol


public class GetURL
{
public static void main(String[] args) throws Exception
{
URL httpUrl = new URL("http://some_url/index.jsp");

InputStream istream = httpUrl.openStream();

InputStreamReader ir = new InputStreamReader(istream);
BufferedReader reader = new BufferedReader( ir );

StringBuffer buf = new StringBuffer();
int nextChar;
while( (nextChar = reader.read()) != -1 )
{
buf.append( (char)nextChar );
}

reader.close();

System.out.println( buf );
// in buf, you have what JSP page generated

}
}


Re: Invoking a JSP from an external (non-Servlet) Java class.

Posted By:   sachin_walia  
Posted On:   Wednesday, February 27, 2002 09:52 PM

A good solution is to use a java.util.TimerTask.
What you can do is create a class which extends java.util.TimerTask and call the class from the servlet or jsp( which loads on startup).the sample code below shows how



//TimerTask code

import java.util.*;

public class MailTask{
Timer t=null;
SendMail sm = null;//ur bean for sending mail
public MailTask(long delay){
t = new Timer();
t.schedule(new SendMailTask(),10, delayInSecs);
}

class SendMailTask extends TimerTask{
public void run(){
try{
//do ur mail sending task here using SendMail component
}catch(Exception e){
}
}
}
}

//servlet code

import javax.servlet.*;
import javax.servlet.http.*;

public class SendMailServlet extends HttpServlet {

private MailTask mt;

public void init(ServletConfig config) throws ServletException {
super.init(config);
long delay = 86400*1000; //once a day
MailTask mt=new MailTask(delay);
}

public void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, java.io.IOException{
}
}


Your SendMail can have all the setters and getters for setting the addresses and subject and can be configured from servlet called on timely basis.

--
hope it helps

sachin

Re: Invoking a JSP from an external (non-Servlet) Java class.

Posted By:   Viswanath_Ramamurthy  
Posted On:   Tuesday, September 11, 2001 01:12 AM

HI
Do u have the idea of concept called as tunneling ..
I think it is the right solution for ur Problem!
For further query please contact me at vichu6@hotmail.com

Re: Invoking a JSP from an external (non-Servlet) Java class.

Posted By:   AlessandroA_Garbagnati  
Posted On:   Monday, May 7, 2001 01:52 AM

Hi,

I was thinking that since you are not the only one that need to 'test' the jsp in an environment that it's outside the web server, there should be a way, maybe, to use Tomcat source code to implement some kind of Servlet/Jsp Invoker emulator, that implements (and emulates) what it's required by the servlet to run.

Re: Invoking a JSP from an external (non-Servlet) Java class.

Posted By:   John_Zukowski  
Posted On:   Monday, April 23, 2001 04:04 PM

Treat the JSP as a URL.
About | Sitemap | Contact