Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trouble calling a JavaBean from JSP

 
Brian Gerling
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
---------------------------------------------------------------
import java.net.*;
import java.io.*;

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
br_InputStream.close();

cs_HTML = ls_OutputHTML;
}
// Catch URL exceptions
catch(MalformedURLException e) {
System.err.println(e);
e.printStackTrace();
}
// Catch IO exceptions
catch(IOException e) {
System.err.println(e);
e.printStackTrace();
}
}
}
----------------------------------------------------------------
I am calling the above JavaBean with the following JSP code:
<%
String ls_HTML;
String ls_Action = "http://www.forta.com/";
URLReader myURL = new URLReader();
myURL.setHTML(ls_Action);
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'.
java.lang.NoSuchMethodError
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
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you import the class in your JSP?
<%@ page import="URLReader" %>
 
Brian Gerling
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic