日期:2014-05-16  浏览次数:20617 次

jquery的ajax显示HelloWorld
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <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">    
	
	<script type="text/javascript" src="<c:url value='/jquery/jquery-1.4.2.js'></c:url>"></script>
	
	<script type="text/javascript">
		$(function()
		{
			$("#button1").click(function()
			{
				$.ajax({
					
					type:"POST",
					url:"MyServlet",
					dateType:"html",
					success:function(returnedData){
						$("#result").val(returnedData);
					}
				});
			})
		
		});
	
	</script>
	
  </head>
  
  <body>
  <input type="text" id="result"><input type="button" id="button1" value="请点击">
  </body>
</html>

?servlet代码

package com.javasky.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



public class MyServlet extends HttpServlet 
{
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException 
	{
		resp.setHeader("param", "no-cache");
		resp.setHeader("cache-control","no-cache");
		
		PrintWriter out = resp.getWriter();
		out.println("HelloWord");
		out.flush();
	
	}
	
}

?