本人java純零基礎(chǔ),今天寫了自己的第一個jsp代碼,但是在請求ajax數(shù)據(jù)時,老是獲取不到想要的數(shù)據(jù)。
需求是這樣的,用戶輸入請求的頁面,服務端解析請求的頁面返回請求頁面的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); //?????o 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 { //???è?o?|????é??è|???3é?-?μ? ??? ??? ??? ??? ?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"){?????????? ??????????? ???????????? }??????? });??? })})上面標紅的地方的數(shù)據(jù)不是想要的數(shù)據(jù),求高手指導。
添加回答
舉報
0/150
提交
取消