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
@H_301_85@
35
36
37
38
@H_403_93@
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
|
@H_404_181@<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
@H_404_181@<%
String path = request.getContextPath();
@H_404_181@String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!
DOCTYPE
@H_404_181@HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
base
@H_404_181@href="<%=basePath%>">
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
@H_404_181@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;
}
@H_404_417@
}else{
document.getElementById("div").innerHTML="服务器出现错误!";
}
}
>
body
@H_404_181@>
input
@H_404_181@type="button" value="click here" onclick="ajax();" />
type="text" id="v1"></
>
type="text" id="v2"></
>
div
@H_404_181@id="div"></
>
>
@H_404_181@>
|
java后台
import java.io.IOException;
@H_404_181@import java.io.PrintWriter;
@H_404_181@import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
@H_404_181@import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@H_404_181@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
@H_404_181@version="1.0" encoding="UTF-8"?>
web-app
@H_404_181@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-class
@H_404_181@>AjaxServlet</
>
servlet-mapping
@H_404_181@>
>
>
>
|