Solved

Out.println in Bean & Javax.servlet.jsp

Posted on 2004-10-20
6
5,625 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

In this article, I show you step by step with screenshots to assist you - HOW TO: Deploy and Install the VMware vCenter Server Appliance 6.5 (VCSA 6.5), with some helpful tips along the way.
In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now