日期:2014-05-17  浏览次数:20747 次

jquery如果与后台的Servlet建立连接
为什么jquery无法与后台的Servlet建立连接呢?感觉代码没错啊。
代码如下:
index.jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

<script type="text/javascript" src="scripts/jquery-1.8.3.js"></script>

<script type="text/javascript">

$(function()
{
$("#button1").click(function()
{
$.get("GetImageServlet",
{
categoryName: $("#categoryName").val();
picSource: $("input[name=picSource]:checked").val();

}, function(returnedData)//回调
{
alert(returnedData);  //test

$("#div1").empty(); //每次点击时先清空

var html = "";

for(var i = 0; i < returneData.length; i++)
{
html += "<img src='" + returnedData[i] + "' width='150' height='150'>";

}

$("#div1").append(html);

});
});


$("#button2").click(function()
{
$("#div2").html("<font color='red'>processing</font>");

$.post("SaveImageServlet",{}, function()
{


});

});


});

</script>





  </head>
  
  <body>
    
    <input type="text" id="categoryName"/>
    <input type="radio" name="picSource" value="google" checked>Google
    <input type="radio" name="picSource" value="flickr" />Flickr
    <input type="button" value="click" id="button1">"
    <input type="button" value="save" id="button2"/>
    
    <div id="div2"></div>
    
    <div id="div1"></div>
    
  </body>
</html>




后台GetImageServlet代码:
package com.imagetools;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import&nbs