simple question from newbie


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: simple question from newbie

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Posts
    2

    simple question from newbie

    I've been asked to write a servlet to monitor web services, and I am new to servlets.
    Here's my question:

    How can I test the HTML output of a URL request?

    For example, when visiting the URL to a web service, it returns a simple HTML page that reads "And now...some services"

    I need to write a servlet on an application server that runs frequently to make sure that the URL has this message.

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Use a HttpURLConnection

    Use the class below like:

    Code:
    String urlStr="http://www.thesite.com/";
    URL url=new URL(urlStr);
    SiteConn sC=new SiteConn(url);
    StringBuffer sb=sC.getContents();
    if (sb.toString.equals("And now...some services")) {
      // well well...
    }
    (note: it returns the entire html code for the url)

    SiteConn class:

    Code:
    public class SiteConn {
      URL url=null;
      public SiteConn(URL  url) {
        this.url=url;
      }
      public StringBuffer getContents() throws Exception {
        StringBuffer buffer;
        String line;
        int responseCode;
        HttpURLConnection connection;
    
        BufferedReader dataInput;
        connection = (HttpURLConnection)url.openConnection();
        responseCode = connection.getResponseCode();
        if (responseCode != HttpURLConnection.HTTP_OK) {
          throw new Exception("HTTP response code: " +
             String.valueOf(responseCode));
        }
        try {
          buffer = new StringBuffer();
          InputStream input = connection.getInputStream();
          dataInput = new BufferedReader(new InputStreamReader(input));
    
          while ( (line = dataInput.readLine()) != null) {
            buffer.append(line);
            buffer.append('\n');
            //System.out.println("line: " + line);
          }
          input.close();
        } catch (Exception ex) {
          ex.printStackTrace(System.err);
          return null;
        }
       return buffer;
      }
    }
    Last edited by sjalle; 10-24-2005 at 12:21 PM.
    eschew obfuscation

Similar Threads

  1. Very Simple Question - I think
    By Andrew Murphy in forum VB Classic
    Replies: 1
    Last Post: 09-03-2002, 02:17 AM
  2. Newbie question : .net studio
    By Steven in forum .NET
    Replies: 1
    Last Post: 04-14-2002, 10:01 AM
  3. Simple Java DOM question
    By Lenin in forum XML
    Replies: 0
    Last Post: 03-21-2001, 05:01 PM
  4. A simple question on setContentType
    By H. Ma in forum Java
    Replies: 3
    Last Post: 02-26-2001, 01:25 PM
  5. Simple Data Environment Question
    By Alex Nitulescu in forum VB Classic
    Replies: 1
    Last Post: 10-21-2000, 06:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center