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
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!
CJP (Certifiable Java Programmer), AMSE (Anti-Microsoft Software Engineer)
Author of Posts in the Saloon
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
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?
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.
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop