During the execution of a package listener, I am trying to call a web page with embedded query strings to execute a task in .cfm...

The code below is a snippet from the original code to simplify the discover of the error. I have been struggling to get this simple code to work with no luck so far.

I am like two days into JSP.


<%@page import=	"

Section 1 <!-- THIS SECTION WORKS --><HR>
	URL urlOne =  new URL("http://google.com");
	Object getcontent = urlOne.getContent();

Section 2 <!-- THIS SECTION DOES NOT WORK --> <HR>
<!-- ERROR: Unhandled exception type MalformedURLException -->
	public class PassToWebService {
			URL urlTwo = new URL("http://google.com/");
			Object connection = urlTwo.getContent();
8 Years
Discussion Span
Last Post by rickprospers

Though I am not sure as to what you are trying to achieve, you are getting the mentioned error because the URL constructor (the call new URL() ) throws a checked exception which you need to either catch or make the method have a throws clause.

try {
  URL url = new URL("someURL");
} catch(MalformedURLException mue) {
  // received an invalid url


public void doSomething() throws MalformedURLException {
  URL url = new URL("someURL");

And BTW, scriptlets are a strict no-no for a variety of reasons. Plus, it would be kind of difficult for you to get into J2EE if your basic Java concepts aren't clear enough.

Votes + Comments
Good post. (and congrats on you 7th dot;) )

In the example I gave, I have the URL as http://www.google.com. Does Java take this as a bad malformed url? - am I supposed to escape certain characters?

I have also seen many other examples online and they have plain URLs like the one I listed. In addition, the first half of the code works perfectly. It is only when I place it within a class.

I have also tried to copy and paste examples I found on the web and they will not work. Though I am new to JSP / java, the only thing i can think of is that it does not want to work within a jsp file while in a class. I dont know if that makes any sense.

Here is what I am trying to do...

I am working with Smack.jar for instant messaging. There is a presence listener that I can manipulate. I am working with coldfusion which interacts with smack.

When someone's presence changes, I need to update the database with a boolean online/offline flag. When the event is triggured, I want to call a url like


That's It!

If you have a better way of accomplishing this, please advise.

Note: The processing needs to be done in coldfusion for many reasons.


Since you can manipulate the listener, something like this should be of some use to you:

package home.projects.daniweb;

import java.net.URL;
import java.net.MalformedURLException;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class URLTester {
  public static void main(String args[]) {
    String url = "http://www.google.com/";
    try {
      InputStream in = null;
      try {
        // Open a connection to the resource specified in 'url' and grab hold
        // of the stream.
        URL urlObj = new URL(url);
        in = urlObj.openStream();
        // Spit the response to the screen
        BufferedReader bufIn = new BufferedReader(new InputStreamReader(in));
        System.out.println("RESPONSE BEGINS\n");
        String str;
        while((str = bufIn.readLine()) != null) {
        System.out.println("RESPONSE ENDS\n");
      } finally {
        if(in != null) {
    } catch(Exception e) {
      System.out.println("An exception has occured; " + e.getMessage());

If you are relying on a user input and if the action for that is well defined e.g. a user clicking on the logout link, you can code it as a simple HTML page:

  <script type="text/javascript">
  // Some script
  <a href="/setPresence.cfm?user=bob&isOnline=0">Logout</a>

I found a plugin that added the info to the database already. Thanks for the help!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.