cancel
Showing results for 
Search instead for 
Did you mean: 

Load a html, sort its content and display it in a JSP

Former Member
0 Kudos

HI,

I would like my JSP page to load a html file, retrieve only the 50 first words of it's content and display it in my JSP file.

How can I do that ? I am using a portal application and my Java class file only have the following methods: doContent(IPortalComponentRequest request, IPortalComponentResponse response) and init(IPortalComponentInitContext arg0)

What I want to do is load the HTML file on the Java side, save the 50 first words into a String value then display it into my JSP file.

Any ideas around ??

Thanks in advance for your help.

Thibault Schalck

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi.

In order to get the HTML page in a string value:

try {
        // Create a URL for the desired page
        URL url = new URL("http://<your url>");
    
        // Read all the text returned by the server
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String str;
        while ((str = in.readLine()) != null) {
            // str is one line of text; readLine() strips the newline character(s)
        }
        in.close();
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    }

From there on you can choose: the fancy way is to find a HTML parser, otherwise you can scan the string for the body tag and use the StringTokenizer class to fetch the first 50 words. (and rule out the tags.)

Good luck, Roelof

Former Member
0 Kudos

Thank you for the answer.

Answers (0)