Ajax与后台的交互

前端之家收集整理的这篇文章主要介绍了Ajax与后台的交互前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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">
@H_404_181@< html @H_404_181@>
head @H_404_181@>
base @H_404_181@href="<%=basePath%>">
title @H_404_181@>My JSP 'index.jsp' starting page</ >
Meta @H_404_181@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 @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后台

35
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

19
<? 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 servlet-name @H_404_181@>ajax</ >
servlet-class @H_404_181@>AjaxServlet</ >
servlet-mapping @H_404_181@>
>
url-pattern @H_404_181@>*.do</ >
>
>
原文链接:https://www.f2er.com/ajax/160605.html

猜你在找的Ajax相关文章