File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Trouble calling a JavaBean from JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Trouble calling a JavaBean from JSP" Watch "Trouble calling a JavaBean from JSP" New topic

Trouble calling a JavaBean from JSP

Brian Gerling

Joined: Jul 03, 2001
Posts: 3
Hello all,
I am new to Java programing so thank you in advance. I am trying to call a JavaBean from a JSP that will get the HTML of the target URL. I am using JRun 3.1.
Bean code:

public class URLReader {

// Declare class variables
String cs_HTML;

// no argument constructor
public URLReader() {}

// Getter method that returns HTML property
public String getHTML() {
return cs_HTML;

// setHTML setter method that sets the HTML property
public void setHTML(String ls_Action) {
String ls_ReturnedHTML = "";
try {
// Create url action page
URL url_ActionPage = new URL(ls_Action);
// Create url connection
URLConnection urlcon_Connection = url_ActionPage.openConnection();
// Create buffered reader to get content
BufferedReader br_InputStream = new BufferedReader(new InputStreamReader(urlcon_Connection.getInputStream()));

String ls_InputLine;
String ls_OutputHTML = "";

while ((ls_InputLine = br_InputStream.readLine()) != null)
// Concatenate HTML output string
ls_OutputHTML = ls_OutputHTML + ls_InputLine;

// Close buffered reader

cs_HTML = ls_OutputHTML;
// Catch URL exceptions
catch(MalformedURLException e) {
// Catch IO exceptions
catch(IOException e) {
I am calling the above JavaBean with the following JSP code:
String ls_HTML;
String ls_Action = "";
URLReader myURL = new URLReader();
ls_HTML = myURL.getHTML();

<%= ls_HTML %>
I am getting the following error:
Exception thrown on line '6' from page 'D:\\Projects\\bgerling\\test\\http.jsp'.
I do not understand why I am getting this error since I have a setHTML method in my URLReader class.
Any help or ideas would be of great help. Thanks.
Phil Hanna
Ranch Hand

Joined: Apr 05, 2001
Posts: 118
Did you import the class in your JSP?
<%@ page import="URLReader" %>

Phil Hanna<BR>Sun Certified Programmer for the Java 2 Platform<BR>Author of :<BR><A HREF="" TARGET=_blank rel="nofollow">JSP: The Complete Reference</A><BR><A HREF="" TARGET=_blank rel="nofollow">Instant Java Servlets</A>
Brian Gerling

Joined: Jul 03, 2001
Posts: 3
Hello Phil,
I hadn't tried that. I imported the class and everything now works as expected.
Do you always have to import your own classes even though they are in the WEB-INF\classes directory?
I have tried other examples from books/tutorials and they worked. As a rule should you always import your own classes?
Thank you so much for your help!
I agree. Here's the link:
subject: Trouble calling a JavaBean from JSP
It's not a secret anymore!