本人java纯零基础,今天写了自己的第一个jsp代码,但是在请求ajax数据时,老是获取不到想要的数据。
需求是这样的,用户输入请求的页面,服务端解析请求的页面返回请求页面的html给客户端,我的代码是这样的
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><%@ page import="java.io.*"%><%@ page language="java" import="java.net.*"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><script language="JavaScript" src="js/jquery-1.7.2.min.js"></script><script language="JavaScript" src="js/connectToServer.js"></script><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Web Security</title></head><body> <% String url = request.getParameter("url"); String html = ""; if (url != null && url != "") { String htmpath = null; BufferedReader in = null; InputStreamReader isr = null; InputStream is = null; PrintWriter pw = null; HttpURLConnection huc = null; try { htmpath = request.getParameter("username"); URL urlObj = new URL(url); //å建 URL System.out.println(url); huc = (HttpURLConnection) urlObj.openConnection(); is = huc.getInputStream(); isr = new InputStreamReader(is); in = new BufferedReader(isr); String line = null; while (((line = in.readLine()) != null)) { if (line.length() == 0) continue; //System.out.println(line); html += line; } System.out.println(html); System.out.println("hello"); } catch (Exception e) { //System.err.println(e); out.println("xxx:" + e.getMessage()); } finally { //æ 论å¦ä½é½è¦å
³éæµ try { is.close(); isr.close(); in.close(); huc.disconnect(); pw.close(); } catch (Exception e) { } } } %> <table style="width: 50%" align='center'> <tr> <td width=60%><input id='txtUrl' value='http://www.baidu.com'></td> <td width=40%><input id='btnStart' type='button' value='Start'></td> </tr> <tr> <td colspan="2"><textarea style="width: 100%; height: 100px;"> </textarea></td> </tr> <tr> <td colspan="2"><iframe id='ifm1' style="width: 100%; height: 100%;"></iframe></td> </tr> </table></body></html>
js
$(function(){ $("#btnStart").click(function(){ var theURL = $("#txtUrl").val(); alert("come in start URL:" + theURL); $.ajax({ url: ' http://localhost:8080/index.jsp', type: 'post', data: 'url=' + theURL, dataType: 'text', error: function(XMLHttpRequest, textStatus, errorThrown){ console.log('Error occurs at server.' + errorThrown); alert('Error occurs at server.' + errorThrown); }, success: function(data){ var s='<%=html %>'; alert(s) alert("success:"+data); console.log("" + data); $("#ifm1").attr("srcdoc", data); // if(data == "success"){ } }); })})上面标红的地方的数据不是想要的数据,求高手指导。
添加回答
举报
0/150
提交
取消