How to request and get a webpage using http request in jsp

前端 未结 2 1999
渐次进展
渐次进展 2021-01-23 13:49

How do I get an xml page (I mean an REST API from an web service), parse it and display it in my website, in jsp?

相关标签:
2条回答
  • 2021-01-23 14:32

    You'll need to use a library to retrieve the content via HTTP (HttpClient, for example) and something to parse the response (SAX).

    Avoid using scriptlets for doing this, encapsulate your logic in classes and try creating custom tags, or better yet, try using something like Spring's MVC.

    0 讨论(0)
  • 2021-01-23 14:39

    I don't have the complete answer but here is how to get a web page at least. I'm trying to do something similar so will come back when I have more.

    <%@page import="java.net.*" %>
    <%@page import="java.io.*" %>
    
    <%
       URL dest = new URL("http://www.yahoo.com/");
       URLConnection yc = dest.openConnection();
       BufferedReader in = new BufferedReader(
                               new InputStreamReader(
                               yc.getInputStream()));
       String inputLine;
    
       while ((inputLine = in.readLine()) != null)
           System.out.println(inputLine);
       in.close();
    %>
    
    0 讨论(0)
提交回复
热议问题