You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Yair Ben-Meir <ya...@sundaysky.com> on 2008/04/13 16:12:37 UTC

API for running JSP

Hi

I m looking for a way to run a JSP by an API from my code, instead of
sending an HTTP request to a server. That means that I m not going to start
tomcat, and instead just use Tomcat's classes to run a JSP from my (desktop)
application, as if surfing to it.

Is there way to do this?

Thanks

Yair

 


RE: API for running JSP

Posted by Yair Ben-Meir <ya...@sundaysky.com>.
Well, I found a solution:
			String path = "C:\\Program Files\\tomcat6";
			
			Embedded embedded = new Embedded();
			embedded.setCatalinaBase(path);
			embedded.setCatalinaHome("C:\\Program
Files\\tomcat6");
			
			Engine engine = embedded.createEngine();
			engine.setDefaultHost("localhost");
			engine.setName("myCatalina");
			
			Host host = embedded.createHost("localhost",
"webapps");
			engine.addChild(host);
			
			// Root context.
			Context context = embedded.createContext("", path +
"\\webapps\\ROOT");
			host.addChild(context);
			
			// mywebapp context.
			context = embedded.createContext("/mywebapp", path +
"\\webapps\\mywebapp");
			host.addChild(context);
			
			embedded.addEngine(engine);
			
			Connector connector = new Connector();
			
			embedded.start();
			
			StandardWrapper jsp = (StandardWrapper)
context.findChild("jsp");
			JspServlet serv = (JspServlet) jsp.getServlet();
			
			Request request = new Request();
			Response response = new Response();
			org.apache.coyote.Request coyoteRequest = new
org.apache.coyote.Request();
			
			request.setContextPath("/mywebapp");
			request.setServletPath("/try1.jsp");
			request.setCoyoteRequest(coyoteRequest);
			request.setContext(context);
			request.setConnector(connector);
			request.setResponse(response);
			
			response.setConnector(connector);
			response.createOutputStream();
			org.apache.coyote.Response coyoteResponse = new
org.apache.coyote.Response();
			InternalOutputBuffer iob = new
InternalOutputBuffer(coyoteResponse);
			ByteArrayOutputStream result = new
ByteArrayOutputStream();
			iob.setOutputStream(result);
			coyoteResponse.setOutputBuffer(iob);
			response.setRequest(request);
			response.setCoyoteResponse(coyoteResponse);
			
			serv.service(request, response);
			response.finishResponse();
			
			if (response.isError())
			{
				throw new Exception("Status:
"+response.getStatus() + ", message: " + response.getMessage());
			}
			
			System.out.println(result.toString());
			
			embedded.stop();



The question now is, can I give the JspServlet a foreign jsp file (a path
outside of the context) to run?

Thanks,
Yair




---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org