本人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"){
}
});
})
})
上面标红的地方的数据不是想要的数据,求高手指导。
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ page import="java.io.*"%> 4 <%@ page language="java" import="java.net.*"%> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6 <html> 7 <head> 8 <title>Insert title here</title> 9 <script src="http://common.cnblogs.com/script/jquery.js" type="text/javascript"></script> 10 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 11 <title>Web Security</title> 12 </head> 13 <body> 14 <% 15 String url = request.getParameter("url");//你只传了一个参数过来 16 String html = ""; 17 if(url != null && url != "") 18 { 19 String htmpath = null; 20 System.out.println(html); 21 System.out.println("hello"); 22 PrintWriter pw = response.getWriter(); 23 pw.write(url); 24 pw.flush(); 25 pw.close(); 26 } 27 %> 28 <table style="width: 50%" align='center'> 29 <tr> 30 <td width=60%><input id='txtUrl' value='http://www.baidu.com'></td> 31 <td width=40%><input id='btnStart' type='button' value='Start'></td> 32 </tr> 33 <tr> 34 <td colspan="2"><textarea style="width: 100%; height: 100px;"> 35 36 </textarea></td> 37 </tr> 38 <tr> 39 <td colspan="2"><iframe id='ifm1' style="width: 100%; height: 100%;"></iframe></td> 40 </tr> 41 </table> 42 <script type="text/javascript"> 43 $(function(){ 44 $("#btnStart").click(function(){ 45 46 var theURL = $("#txtUrl").val(); 47 alert("come in start URL:" + theURL); 48 49 $.ajax({ 50 url: 'index.jsp', 51 type: 'post', 52 data: 'url=' + theURL, 53 dataType: 'text', 54 error: function(XMLHttpRequest, textStatus, errorThrown){ 55 console.log('Error occurs at server.' + errorThrown); 56 alert('Error occurs at server.' + errorThrown); 57 }, 58 59 success: function(data){ 60 //var obj = eval("("+data+")");//将后台传过来的字符串转换为js对象 61 alert("success:"+data); 62 console.log("" + data); 63 $("#ifm1").attr("src",data); 64 // if(data == "success"){ 65 66 } 67 }); 68 }); 69 }); 70 </script> 71 </body> 72 </html>
应该是你要的,如果你只是想把输入的URL解析后返回页面的话,你不能放在iframe里面,可以考虑用div接收。如果你想输入URL后,服务器端把这个URL放到当前页面的iframe里面,上面的代码就是你要的。
按照你的代码思路,你应该这样实现:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ page import="java.io.*"%> 4 <%@ page language="java" import="java.net.*"%> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6 <html> 7 <head> 8 <title>Insert title here</title> 9 <script src="http://common.cnblogs.com/script/jquery.js" type="text/javascript"></script> 10 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 11 <title>Web Security</title> 12 </head> 13 <body> 14 <% 15 String url = request.getParameter("url");//你只传了一个参数过来 16 String html = ""; 17 if (url != null && url != "") { 18 String htmpath = null; 19 BufferedReader in = null; 20 InputStreamReader isr = null; 21 InputStream is = null; 22 PrintWriter pw = null; 23 HttpURLConnection huc = null; 24 try { 25 htmpath = request.getParameter("username");//该参数没有在ajax中传递过来 26 URL urlObj = new URL(url); 27 System.out.println(url); 28 huc = (HttpURLConnection) urlObj.openConnection(); 29 is = huc.getInputStream(); 30 isr = new InputStreamReader(is); 31 in = new BufferedReader(isr); 32 String line = null; 33 34 while (((line = in.readLine()) != null)) { 35 if (line.length() == 0) 36 continue; 37 //System.out.println(line); 38 html += line; 39 } 40 System.out.println(html); 41 System.out.println("hello"); 42 pw=response.getWriter();//用ajax的关键代码 43 pw.write(html); //用ajax的关键代码 44 } 45 catch (Exception e) { 46 //System.err.println(e); 47 out.println("xxx:" + e.getMessage()); 48 } finally { 49 try { 50 is.close(); 51 isr.close(); 52 in.close(); 53 huc.disconnect(); 54 pw.close(); 55 } catch (Exception e) { 56 } 57 } 58 } 59 %> 60 <table style="width: 50%" align='center'> 61 <tr> 62 <td width=60%><input id='txtUrl' value='http://www.baidu.com'></td> 63 <td width=40%><input id='btnStart' type='button' value='Start'></td> 64 </tr> 65 <tr> 66 <td colspan="2"><textarea style="width: 100%; height: 100px;"> 67 68 </textarea></td> 69 </tr> 70 <tr> 71 <td colspan="2"><div id="ifm1" style="width: 100%; height: 100%;"></div></td> 72 </tr> 73 </table> 74 <script type="text/javascript"> 75 $(function(){ 76 $("#btnStart").click(function(){ 77 78 var theURL = $("#txtUrl").val(); 79 alert("come in start URL:" + theURL); 80 81 $.ajax({ 82 url: 'index.jsp', 83 type: 'post', 84 data: 'url=' + theURL, 85 dataType: 'text', 86 error: function(XMLHttpRequest, textStatus, errorThrown){ 87 console.log('Error occurs at server.' + errorThrown); 88 alert('Error occurs at server.' + errorThrown); 89 }, 90 91 success: function(data){ 92 //var obj = eval("("+data+")");//将后台传过来的字符串转换为js对象 93 alert("success:"+data); 94 console.log("" + data); 95 $("#ifm1").html(data); 96 // if(data == "success"){ 97 98 } 99 }); 100 }) 101 }) 102 </script> 103 </body> 104 </html>