Link to home
Start Free TrialLog in
Avatar of LarryAndro
LarryAndro

asked on

Out.println in Bean & Javax.servlet.jsp

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?  
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

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");
          }
     }
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");
          }
     }
ASKER CERTIFIED SOLUTION
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of LarryAndro
LarryAndro

ASKER

It's working, and I'm a very excited puppy!!  Thanks, both...
:-)  Good luck!!