Solved

Out.println in Bean & Javax.servlet.jsp

Posted on 2004-10-20
6
5,634 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
We need a new way to communicate time sensitive or critical info.   The best part of my role at xMatters is visiting our clients all over the world to learn about how they operate their businesses, share insights that xMatters has gleaned across…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

932 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

11 Experts available now in Live!

Get 1:1 Help Now