日期:2014-05-16 浏览次数:20964 次
jsp代码
<%@ 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", data:{'param1':$("#param1").val(), 'param2':$("#param2").val()}, success:function(returnedData){ $("#result").val(returnedData); } }); }) }); </script> </head> <body> <input type="text" id="param1">+ <input type="text" id="param2">= <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 { System.out.println(req.getParameter("param1")); System.out.println(req.getParameter("param2")); int param1 = Integer.parseInt(req.getParameter("param1")); int param2 = Integer.parseInt(req.getParameter("param2")); resp.setHeader("param", "no-cache"); resp.setHeader("cache-control","no-cache"); PrintWriter out = resp.getWriter(); out.println(String.valueOf(param1+param2)); out.flush(); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } }
??