Search code examples
javaurlgrailsurlconnection

Viewing data on browser


i have a domain with fields like URL, lastModified etc. Now i want to view the information i get from this code into the list page. How can i do tht

def url = new URL("http://www.google.com")
HttpURLConnection connection = (HttpURLConnection)url.openConnection()
//  long contentLength = Long.parseLong(connection.getHeaderField("Content-Length"));
connection.setRequestMethod("GET")
connection.connect()
connection.getURL()
println("Date: " + new Date(connection.getDate()))
println("LastModified Date:" + new Date(connection.getLastModified()))
if (connection.responseCode == 200 || connection.responseCode == 201){
    def returnMessage = connection.content

    //print out the full response
    println "${returnMessage}";
    System.out.println(connection.getURL());
} else {
    println "Error Connecting to " + url
    println "Couldn't connect to url"
}

Solution

  • From your controller, you can use the render method,

    render returnMessage
    

    (see http://grails.org/doc/2.0.x/ref/Controllers/render.html)

    or pass it in a model to a gsp page and then print it from gsp,

    return [returnMsg: returnMessage]
    
    ....(in .gsp)
    ${returnMsg}
    

    (see http://grails.org/doc/2.0.x/guide/theWebLayer.html#GSPBasics)