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

jsp/servlet communication  RSS feed

 
Peter Guillebaud
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there a way for a jsp to write to an InputStream that has been created on a servlet? I need the servlet to read the InputStream, call various methods depending on what the input is, and then send an OutputStream back again to the jsp page. Many thanks
 
Gerry Giese
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your question is confusing. To answer your question, No, you cannot, by definition, write to an InputStream. InputStreams can only be read.
Your explanation of what you want to do gives me a clue, but isn't detailed enough for me to do more than guess. And that guess is that you want to read the input, conditionally perform processing depending on that output, then send the results of that processing to a JSP for display. In other words, you don't want to pass streams around, just chunks of data. If this is the case, you want to do the following after you process your data in the servlet:

Then, in your "DisplayResults.jsp", do this to get the data back out:

Then just access the Results string like you would any variable in a JSP. What this amounts to is treating the HttpServletRequest as a Hashtable you can store data in, then pass it on to the next Servlet (use relative path to Servlet) or JSP for more processing or for display.
Hope this is what you were looking for. If you're new to Servlets and JSPs, I highly recommend buying "Special Edition using Java Server Pages and Servlets". It covers both quite well individually as well as how best to get them to work together. Instead of just using it as reference, I actually started reading it (skimming a little here and there) and learned tons, which completely changed the way I did things and made my work much less frustrating.
Hope this helps!
 
Peter Guillebaud
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Gerry, this helps a lot... I feel a bit foolish for trying to write to an InputStream!
What I really want to do is to get the jsp to call the servlet and pass a piece of text to this servlet. Is the jsp:forward tag the only way to do this? The servlet does a database query, depending on the String it has received, and returns the ResultSet in the form of another String back to the same jsp page that made the original request. The servlet will contain many methods for querying the database, each will be called depending on the String sent by the jsp page(s).
Thanks
 
Gerry Giese
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not post to the servlet instead? In my setup I always post to a control servlet, read the parameters, call another class to do database lookups if needed, then forward my data to the JSP view. Check out:
http://developer.java.sun.com/developer/restricted/patterns/Image96.gif
http://developer.java.sun.com/developer/restricted/patterns/ServiceToWorker.html
My version is a little simpler than that, but it keeps the processing and the view pretty well separated, and reduces the java code in the JSP. Another option is do your database calls from a bean instead of a servlet. The JSP just has to declare it then call methods on the bean (which might then call the database). In my opinion it's much easier to handle things in a servlet calling another class to do work than to have a JSP doing calls to beans or custom tags. Plus, you can better do things like connection pooling, caching, and other resource sharing. It's also easier to debug servlets than JSPs.
Check out these other resources:
http://java.oreilly.com/news/jsptips_1100.html
http://www.roseindia.net/jsp/usingbeansinjsp.htm
http://stardeveloper.com:8080/articles/072001-1.shtml
http://technet.oracle.com/sample_code/tech/java/jsp/htdocs/jspdemo/aboutjspdemo.html
http://www.jsptut.com/Index.html
 
Peter Guillebaud
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gerry,
Thanks a lot!
At the moment I'm going to use one servlet that keeps the db connection open ( and one statement object). It will deal with all the db queries that come from the jsp pages and hand back the data as an attribute of the request object... unless there is a good reason for using beans instead?
 
Jason Menard
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Guillebaud:
Hi Gerry,
Thanks a lot!
At the moment I'm going to use one servlet that keeps the db connection open ( and one statement object). It will deal with all the db queries that come from the jsp pages and hand back the data as an attribute of the request object... unless there is a good reason for using beans instead?

That sounds dangerous, as well as inefficient. What happens when more than one person at a time is calling your servlet? Are you synchronizing to protect against this? Even if you are it is still slow and inefficient. Connection pooling would be the absolute best way to go, however even just opening up a new connection without pooling would probably be better.
And yes it is better to use java beans for the data, if for no other reason than that is the standard idiom. Jsps are simply for viewing data, and shouldn't have to have the code in them necessary to pars through a resultset or however you were going to send your data back. Just have the servlet place the data in a bean, place the bean into the request, and forward the request on to the jsp who can then work with the bean.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!