AjaxJava交互
jsp代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
<%@ 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</
>
Meta
http-equiv="pragma" content="no-cache">
http-equiv="cache-control" content="no-cache">
http-equiv="expires" content="0">
http-equiv="keywords" content="keyword1,keyword2,keyword3">
http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</
>
script
type="text/javascript">
var xmlHttprequest = null;
function ajax(){
if(window.ActiveXObject){
xmlHttprequest = new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
xmlHttprequest = new XMLHttpRequest;
}
if(xmlHttprequest != null){
var v1 = document.getElementById("v1").value;
var v2 = document.getElementById("v2").value;
xmlHttprequest.open("post","ajaxServlet.do",true);
xmlHttprequest.onreadystatechange=ajaxCallBack;
xmlHttprequest.setRequestHeader("Content-type",
"application/x-www-form-urlencoded");
xmlHttprequest.send("v1="+v1+"&v2="+v2);
}
}
function ajaxCallBack(){
if(xmlHttprequest.readyState == 4){
if(xmlHttprequest.status == 200){
var responseText = xmlHttprequest.responseText;
document.getElementById("div").innerHTML = responseText;
}
}else{
document.getElementById("div").innerHTML="服务器出现错误!";
}
}
>
body
>
input
type="button" value="click here" onclick="ajax();" />
type="text" id="v1"></
>
type="text" id="v2"></
>
div
id="div"></
>
>
>
|
java后台
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 AjaxServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1733653367006626421L;
@Override
protected void doGet(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req,HttpServletResponse resp)
PrintWriter out = resp.getWriter();
String v1 = req.getParameter("v1");
String v2 = req.getParameter("v2");
out.print(v1+v2+"dadasdsadsa");
out.flush();
}
|
web.xml
<?
xml
version="1.0" encoding="UTF-8"?>
web-app
version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
servlet
servlet-name
>ajax</
>
servlet-class
>AjaxServlet</
>
servlet-mapping
>
>
url-pattern
>*.do</
>
>
>
|