问题描述
描述:不是获得当前url路径,是调用其他的url
解决方案
目前为止还不知道LZ到底要的是什么,不管了,再来个Ajax的,很简单的demo。把两个jsp放到Web应用里面,要放在同一个目录下,打开ajaxGetPost.jsp这个页面,ajaxGetPost.jsp页面通过Ajax访问ajaxGetPostUse.jsp页面。<%@ page language="java" pageEncoding="GBK"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>Ajax Using Get and Post</title><script type="text/javascript">var xmlHttp;// 创建对象function createXmlHttpRequest(){if(window.XMLHttpRequest){xmlHttp = new XMLHttpRequest();} else if(window.ActiveXObject){xmlHttp = new ActiveXObject("Microsoft.XMLHttp");} else {alert("Your Browser Don't Support AJAX!");return false;}return true;}// 拼装查询字符串function createQueryString(){var firstName = document.getElementById("firstName").value;var birthday = document.getElementById("birthday").value;var queryString = "firstName=" + firstName + "&birthday=" + birthday; //两次编码解决中文乱码问题return encodeURI(encodeURI(queryString));}// GET方式进行异步请求function doRequestUsingGet(){if(!createXmlHttpRequest()) {return;}var url = "ajaxGetPostUse.jsp?" + createQueryString();//IE会自动缓存异步通信结果,在url后加一个毫秒数,使每次请求的地址都不一样,可解决问题url = url + "×tamp=" + new Date().getTime();xmlHttp.onreadystatechange = handleStateChange;xmlHttp.open("GET", url);xmlHttp.send(null);}// POST方式进行异步请求function doRequestUsingPost(){if(!createXmlHttpRequest()) {return;}//IE会自动缓存异步通信结果,在url后加一个毫秒数,使每次请求的地址都不一样,可解决问题var url = "ajaxUsingGetPost?timestamp=" + new Date().getTime();var queryString = createQueryString();xmlHttp.onreadystatechange = handleStateChange;xmlHttp.open("POST", url);xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");xmlHttp.send(queryString);}// 处理服务端返回数据function handleStateChange(){if(xmlHttp.readyState == 4 && xmlHttp.status == 200){var responseDiv = document.getElementById("serverResponse");// responseDiv.innerHTML = xmlHttp.responseText; // 原始responseDiv.innerHTML = decodeURI(xmlHttp.responseText); // 解码}}</script></head><body><h4>哈哈,我的第一个Ajax程序O(∩_∩)O</h4><h3>输入姓名和生日</h3><form><input type="text" id="firstName" /><br /><input type="text" id="birthday" /><br /><input type="button" value="Get" onclick="doRequestUsingGet();" /><br /><input type="button" value="Post" onclick="doRequestUsingPost();" /></form><div id="serverResponse"></div></body></html><%@ page language="java" pageEncoding="GBK"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>Ajax Using Get and Post Use</title></head><body><%String firstName = request.getParameter("firstName");String birthday = request.getParameter("birthday");// 返回给Ajax调用者的内容out.println("Hello, " + firstName + ", your birthday is " + birthday + ".");%></body></html>
解决方案二:
描述:不是获得当前url路径,是调用其他的url1、如使用URLConnection 或 httpclient等工具抓取数据
解决方案三:
这个不是只要在第一个JSP页面ONload的时候用AJAX发起一个请求就可以了吗?
解决方案四:
你可以使用ajax得到返回的内容,然后再解析得到的内容就可以了
解决方案五:
简单的通过URLConnection:import java.net.*;import java.io.*;import java.util.Date;public class URLConnectionDemo {public static void main(String[] args) throws IOException {URL url = new URL("http://127.0.0.1:8080/cfStruts2Ex2/");URLConnection urlConn = url.openConnection();System.out.println("Date: " + new Date(urlConn.getDate()));System.out.println("Content-Type: " + urlConn.getContentType());int length = urlConn.getContentLength();System.out.println("Content-Lentgth: " + length);if (length > 0) {System.out.println("========== Content ==========");InputStream input = urlConn.getInputStream();int i = length;int c;while ((c = input.read()) != -1 && --i > 0) {System.out.print((char) c);}input.close();} else {System.out.println("No Content.");}}}或者可以用HttpClient:import java.io.IOException;import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.HttpException;import org.apache.commons.httpclient.HttpStatus;import org.apache.commons.httpclient.methods.GetMethod;import org.apache.commons.httpclient.params.HttpMethodParams;public class HttpClientTutorial {private static String url = "http://www.baidu.com/";public static void main(String[] args) {// Create an instance of HttpClient.HttpClient client = new HttpClient();// Create a method instance.GetMethod method = new GetMethod(url);// Provide custom retry handler is necessarymethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler(3, false));try {// Execute the method.int statusCode = client.executeMethod(method);if (statusCode != HttpStatus.SC_OK) {System.err.println("Method failed: " + method.getStatusLine());}// Read the response body.byte[] responseBody = method.getResponseBody();// Deal with the response.// Use caution: ensure correct character encoding and is not binary// dataSystem.out.println(new String(responseBody, "GBK"));} catch (HttpException e) {System.err.println("Fatal protocol violation: " + e.getMessage());e.printStackTrace();} catch (IOException e) {System.err.println("Fatal transport error: " + e.getMessage());e.printStackTrace();} finally {// Release the connection.method.releaseConnection();}}}如果需要鉴权,那就只能用HttpClient,而且要加鉴权:String newUrl = "http://10.167.129.108/fnst_travel/BasicInformation.do";NameValuePair[] paramPairs = new NameValuePair[] {new NameValuePair("registrationPersonNum", "1"),new NameValuePair("myName", "Chen Feng"),new NameValuePair("myNameIDnumber", "123321123321"),new NameValuePair("myNamePhonenumber", "15951836132") };PostMethod method = new PostMethod(newUrl);method.setRequestBody(paramPairs);int statusCode = client.executeMethod(method);