Solved

Out.println in Bean & Javax.servlet.jsp

Posted on 2004-10-20
6
5,647 Views
Last Modified: 2012-05-05
I'm having a problem writing to a webpage using out.println from inside a bean class.  Below, I'm including my best conjectures and also the information that I think an expert would want.  If I'm going in a wrong direction, let me know.  Again, my need is to print to a webpage from inside a bean class.

Currently, I am usering JSP code with some embedded <% java code using out.println %>.  I want to move the <% java code %> out of the JSP, into a bean class.  But, in the bean class, out.println doesn't work.  

I have reviewed the one Experts Exchange message I can find on this topic...  'How to perform out.println() from class'... and know I can't use out.println directly in the bean.  If I understand what TimYates and JugglerW said, I have to pass into a print method I make, either java.io.Writer or javax.servlet.jsp.JspWriter's 'out' object, so the out.println method can be found.  

And, I think that's where my problem starts.  I don't seem to have all the needed javax.servlet.[...] resources I would expect. ( So, I'm probably dealing with an import or a CLASSPATH problem.)

In my class, when I try to import javax.servlet.jsp.JspWriter (see TimYates comments in other message), intellisense doesn't find javax.servlet.jsp or anything descendant.  And, this also seems to verify that javax.servlet.[...] isn't on a build path somewhere.  More on that later...

Here's the top of my bean class...

package gov.vha.hvm.deliveryservice.scs.jsp;
import gov.vha.hvm.deliveryservice.scs.persistence.DataStore;
import gov.vha.hvm.deliveryservice.scs.persistence.impl.*;
import gov.vha.hvm.deliveryservice.scs.model.domain.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.lang.*;
import java.util.Collection;
import javax.servlet.*;
public class SubscriptionCalls {
      private static String topNm;
      public static void AddTopics(javax.servlet.jsp.JspWriter out){
            DataStore ds = TransientDataStoreImpl.getInstance();      
            Collection topics = ds.getTopics(); Iterator
            iterTopics = topics.iterator();
            while (iterTopics.hasNext()){
                  Topic topic = (Topic)iterTopics.next();
                  topNm=topic.getName();
                  out.println("<OPTION name='entryTopic' value='"+topNm+"'>"+topNm+"</OPTION>");
            }
      }

The error listed for the out.println("<OPTION...</OPTION>"); line above is...

     javax.servlet.jsp cannot be resolved (or is not a valid type) for the
     parameter out of the method AddTopics

My understanding is that javax.servlet.jsp.[...' should be in either Tomcat's servlet-api.jar or in servlet.jar.  These two jar's are both in my CLASSAPATH, which includes both...

      c:\Tomcat\common\lib\servlet-api.jar    and...
      c:\j2re1.4.2_05\lib\ext\servlet.jar

Below, I have included the contents of both jar's showing that javax.servlet.jsp.[...] is not in either jar.

Here are the contents of servlet.jar...

C:\j2re1.4.2_05\lib\ext>jar tf servlet.jar
META-INF/MANIFEST.MF
javax/servlet/LocalStrings.properties
javax/servlet/http/LocalStrings.properties
javax/servlet/http/HttpServlet.class
javax/servlet/http/NoBodyResponse.class
javax/servlet/http/NoBodyOutputStream.class
javax/servlet/http/Cookie.class
javax/servlet/http/HttpSessionBindingEvent.class
javax/servlet/http/HttpServletRequest.class
javax/servlet/http/HttpServletResponse.class
javax/servlet/http/HttpSession.class
javax/servlet/http/HttpSessionBindingListener.class
javax/servlet/http/HttpSessionContext.class
javax/servlet/http/HttpUtils.class
javax/servlet/GenericServlet.class
javax/servlet/ServletInputStream.class
javax/servlet/RequestDispatcher.class
javax/servlet/Servlet.class
javax/servlet/ServletConfig.class
javax/servlet/ServletContext.class
javax/servlet/ServletException.class
javax/servlet/ServletOutputStream.class
javax/servlet/ServletRequest.class
javax/servlet/ServletResponse.class
javax/servlet/SingleThreadModel.class
javax/servlet/UnavailableException.class

Here are the contents of servlet-api.jar...

C:\Tomcat\common\lib>jar tf servlet-api.jar
META-INF/
META-INF/MANIFEST.MF
javax/
javax/servlet/
javax/servlet/http/
javax/servlet/resources/
javax/servlet/ServletResponse.class
javax/servlet/ServletOutputStream.class
javax/servlet/FilterChain.class
javax/servlet/ServletRequest.class
javax/servlet/ServletInputStream.class
javax/servlet/RequestDispatcher.class
javax/servlet/ServletException.class
javax/servlet/ServletConfig.class
javax/servlet/ServletContext.class
javax/servlet/Servlet.class
javax/servlet/GenericServlet.class
javax/servlet/UnavailableException.class
javax/servlet/http/HttpServletResponse.class
javax/servlet/http/Cookie.class
javax/servlet/http/HttpServletResponseWrapper.class
javax/servlet/http/HttpSessionBindingEvent.class
javax/servlet/http/HttpSessionEvent.class
javax/servlet/http/HttpSession.class
javax/servlet/http/HttpSessionContext.class
javax/servlet/http/HttpServletRequestWrapper.class
javax/servlet/http/HttpServletRequest.class
javax/servlet/http/HttpSessionActivationListener.class
javax/servlet/http/HttpSessionAttributeListener.class
javax/servlet/http/HttpSessionBindingListener.class
javax/servlet/http/HttpUtils.class
javax/servlet/http/HttpServlet.class
javax/servlet/http/NoBodyResponse.class
javax/servlet/http/NoBodyOutputStream.class
javax/servlet/http/HttpSessionListener.class
javax/servlet/http/LocalStrings.properties
javax/servlet/http/LocalStrings_es.properties
javax/servlet/http/LocalStrings_ja.properties
javax/servlet/http/LocalStrings_fr.properties
javax/servlet/ServletResponseWrapper.class
javax/servlet/ServletRequestWrapper.class
javax/servlet/SingleThreadModel.class
javax/servlet/FilterConfig.class
javax/servlet/ServletContextListener.class
javax/servlet/ServletContextEvent.class
javax/servlet/Filter.class
javax/servlet/ServletContextAttributeListener.class
javax/servlet/ServletContextAttributeEvent.class
javax/servlet/ServletRequestAttributeEvent.class
javax/servlet/ServletRequestEvent.class
javax/servlet/ServletRequestAttributeListener.class
javax/servlet/ServletRequestListener.class
javax/servlet/LocalStrings_ja.properties
javax/servlet/LocalStrings_fr.properties
javax/servlet/LocalStrings.properties
javax/servlet/resources/j2ee_web_services_1_1.xsd
javax/servlet/resources/j2ee_1_4.xsd
javax/servlet/resources/XMLSchema.dtd
javax/servlet/resources/j2ee_web_services_client_1_1.xsd
javax/servlet/resources/web-app_2_3.dtd
javax/servlet/resources/web-app_2_2.dtd
javax/servlet/resources/jspxml.xsd
javax/servlet/resources/xml.xsd
javax/servlet/resources/web-app_2_4.xsd
javax/servlet/resources/datatypes.dtd
javax/servlet/resources/web-jsptaglibrary_2_0.xsd
javax/servlet/resources/jsp_2_0.xsd

So, what is my problem?  Why can't I write to out.println in my bean class?  Is it because I don't have access to javax.servlet.jsp.JspWriter and so don't have access to it's println method?  
0
Comment
Question by:LarryAndro
  • 4
6 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 12362566
Try:

     public static void AddTopics(java.io.Writer out){
          DataStore ds = TransientDataStoreImpl.getInstance();    
          Collection topics = ds.getTopics(); Iterator
          iterTopics = topics.iterator();
          while (iterTopics.hasNext()){
               Topic topic = (Topic)iterTopics.next();
               topNm=topic.getName();
               out.write("<OPTION name='entryTopic' value='"+topNm+"'>"+topNm+"</OPTION>\n");
          }
     }
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12362574
errr:

     public static void AddTopics(java.io.Writer out) throws IOException {
          DataStore ds = TransientDataStoreImpl.getInstance();    
          Collection topics = ds.getTopics(); Iterator
          iterTopics = topics.iterator();
          while (iterTopics.hasNext()){
               Topic topic = (Topic)iterTopics.next();
               topNm=topic.getName();
               out.write("<OPTION name='entryTopic' value='"+topNm+"'>"+topNm+"</OPTION>\n");
          }
     }
0
 
LVL 35

Accepted Solution

by:
TimYates earned 300 total points
ID: 12362578
Heh, last try:

     public static void AddTopics(java.io.Writer out) throws java.io.IOException {
          DataStore ds = TransientDataStoreImpl.getInstance();    
          Collection topics = ds.getTopics(); Iterator
          iterTopics = topics.iterator();
          while (iterTopics.hasNext()){
               Topic topic = (Topic)iterTopics.next();
               topNm=topic.getName();
               out.write("<OPTION name='entryTopic' value='"+topNm+"'>"+topNm+"</OPTION>\n");
          }
     }
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 8

Assisted Solution

by:kiranhk
kiranhk earned 200 total points
ID: 12362631
include jsp-api.jar in the classpath
this is found under  jakarta-tomcatXXXX\common\lib folder where xxxx is ur tomcat version
then u can compile the files
0
 
LVL 1

Author Comment

by:LarryAndro
ID: 12363058
It's working, and I'm a very excited puppy!!  Thanks, both...
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12367301
:-)  Good luck!!
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains the steps required to use the default Photos screensaver to display branding/corporate images
ConnectWise and their customers need to ensure critical alerts automatically reach the right person at the right time. MSP superheros efficiently respond to these alerts key is providing automatic, intelligent alerting that generates a complete audi…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question