加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Ajax与后台的交互

发布时间:2020-12-16 03:16:37 所属栏目:百科 来源:网络整理
导读: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

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后台

35
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

19
<? 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</ >
>
>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读