开发者

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

开发者 https://www.devze.com 2023-02-14 18:16 出处:网络
How do I get an xml page (I mean an开发者_Python百科 REST API from an web service), parse it and display it in my website, in jsp?You\'ll need to use a library to retrieve the content via HTTP (HttpCl

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


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消