Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

calling a jsp from a function?  RSS feed

 
Sam Doder
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having trouble figuring out a way to call a jsp from
a function in a regular java class file.

What I have is URLConnection to the JSP page I try getting the input/output streams but it fails on I beleive the output stream.
Maybe I have to setDoOutput? Anyway...

This is really my first time calling a jsp from a function other then another jsp page.

Note my jsp page is strictly for processing purposes and does not have html code. It is not intended to display it as a webpage. It's
intent is just to pass it input and it returns an output back.

It should work exactly like a function.
I have to use a URLConnection and the input/output streams
but when I write output to the jsp can I still use response.getparameter
or do I use a get servlet stream method. (Like class ServletOutputStream)

Because I am getting confused on how you can use the reponse function in a jsp to get the stuff in the stream. I always just used a query string and got it by getparameters method. But I am unsure write something with the urlconnection stream how to have my jsp recieve it.
And also how to have my jsp write back to the urlconnection stream.

URLConnection is like doing a Post to the jsp.
The only thing is how do you do it with streams instead of query strings.

Thanks for any help
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For starters, you shouldn't be doing I/O in a JSP page. Use a servlet or backing bean instead.

I'm not quite sure what the problem is -seeing the code would help- but here are examples of how to access URLs via GET and POST.
 
Sam Doder
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the snippet of code I need to use. And it is the same code as given in the hyperlink.

try
{
// do a post to the url with the message as parameter named 'msg'
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
System.out.println("Got here 1") ;
OutputStream os = conn.getOutputStream();
out = new OutputStreamWriter(os);
out.write("msg=" + msg);
out.flush();
System.out.println("Got here 2") ;
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
System.out.println("Got here 3") ;
while ((line = in.readLine()) != null)
{
returnXML += line;
}

return returnXML;
}
catch (Exception e)
{
// TODO: handle exception
}

It doesn't get to got here 2.

Note the JSP page is just <% code %> has no html tags.

I am having trouble finding how to retrieve what I put into the stream from the jsp. Do I just use response.getparameter's ???

Also how do I send the result back to the stream from the jsp page.
I would think I would need to get the input/output stream on the jsp side.

I know this can be done with servlets but I need to do it thru a jsp.
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know this can be done with servlets but I need to do it thru a jsp.


A jsp gets compiled to a servlet, so anything you can do in a servlet you can do in a jsp.
The first thing you should have done when you had a problem is checked your exception handling. Never ever Ever EVER do this:
 
Sam Doder
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never mind it works if I do

JSP

<%

String xml_message = request.getParameter("msg") ;

ServletOutputStream return_param = response.getOutputStream() ;
return_param.println( "true" ) ;
return_param.flush() ;
return_param.close() ;

%>

But when I call it by previous post I get a warning Illegal State Exception:Stream

what could this be from?

Note I have to call this function in a while loop many times.
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you calling return_param.close() inside the loop? Once the output stream of a servlet is closed, it's gone for good. That's part of the HTTP specification. Move the flush and close outside the loop.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!