用java写一个网页输入url点击查询即可在下面显示网页源代码
2个回答
推荐于2017-10-08 · 知道合伙人数码行家
关注
展开全部
import java.io.*;
import java.net.*;
public class Demo {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入要显示源码的地址:");
URL url = new URL(br.readLine());
URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();
byte[] b = new byte[1024];
int len = 0;
while((len = is.read(b))!=-1){
System.out.println(new String(b,0,len,"UTF-8"));
}
}
}
//下班,控制台的,网页的你稍微修改一下就OK啦。
追问
求帮改
追答
//index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<body>
<form action="UrlJsp.jsp" method="post">
<input type="text" name="urlDiZhi" id="urlDiZhi" value="" />
<input type="submit" value=" 查 看 " />
</form>
</body>
</html>
//UrlJsp.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.io.*,java.net.*" %>
<html>
<body>
<%!
String result = "";
%>
<%
String dizhi = request.getParameter("urlDiZhi");
URL url = new URL(dizhi);
System.out.println(dizhi);
URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();
byte[] b = new byte[1024];
int len = 0;
while((len=is.read(b))!=-1){
result = new String(b,0,len,"UTF-8");
System.out.println(new String(b,0,len,"UTF-8"));
}
%>
<%=result %>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询