Java中jQueryAjax实例Word格式.docx
- 文档编号:19531822
- 上传时间:2023-01-07
- 格式:DOCX
- 页数:8
- 大小:17.33KB
Java中jQueryAjax实例Word格式.docx
《Java中jQueryAjax实例Word格式.docx》由会员分享,可在线阅读,更多相关《Java中jQueryAjax实例Word格式.docx(8页珍藏版)》请在冰豆网上搜索。
text/javascript"
src="
jquery-1.3.2.js"
<
/script>
12<
13functionsub()
14{
15$.ajax({
16type:
"
post"
//请求方式
17url:
./test"
//发送请求地址
18data:
{//发送给数据库的数据
19username:
$("
#username"
).val(),
20content:
#content"
).val()
21},
22//请求成功后的回调函数有两个参数
23success:
function(data,textStatus){
24$("
#resText"
).html(data);
25}
26});
27
28}
29<
30<
/head>
31<
body>
32
33<
formaction="
id="
form1"
method="
34<
p>
评论:
/p>
35<
姓名:
inputtype="
text"
name="
username"
/>
36<
内容:
textareaname="
rows="
2"
cols="
20"
/textarea>
37<
button"
send"
value="
提交"
onclick="
sub();
38<
/form>
39<
divclass="
comment"
40已有评论:
41<
/div>
42<
divid="
resText"
43<
44
45<
/body>
46<
/html>
复制代码
47importjava.io.IOException;
48importjava.io.PrintWriter;
49
50importjavax.servlet.ServletException;
51importjavax.servlet.http.HttpServlet;
52importjavax.servlet.http.HttpServletRequest;
53importjavax.servlet.http.HttpServletResponse;
54
55publicclasstestextendsHttpServlet{
56
57/**
58*ThedoPostmethodoftheservlet.<
br>
59*
60*Thismethodiscalledwhenaformhasitstagvaluemethodequalstopost.
61*
62*@paramrequesttherequestsendbytheclienttotheserver
63*@paramresponsetheresponsesendbytheservertotheclient
64*@throwsServletExceptionifanerroroccurred
65*@throwsIOExceptionifanerroroccurred
66*/
67publicvoidservice(HttpServletRequestrequest,HttpServletResponseresponse)
68throwsServletException,IOException{
69//处理中文问题
70request.setCharacterEncoding("
);
71response.setContentType("
72System.out.println("
start............"
73Stringusername=request.getParameter("
74Stringcontent=request.getParameter("
75//username=newString(username.getBytes("
gb2312"
),"
76PrintWriterout=response.getWriter();
77out.write(username);
78System.out.println("
username>
=="
+username);
79out.write(content);
80out.flush();
81out.close();
82System.out.println("
end"
83}
84
85}
下面来看第二个demo
86<
87<
88<
89<
90<
jqueryajax<
91<
pragma"
no-cache"
92<
cache-control"
93<
expires"
0"
94<
keywords"
keyword1,keyword2,keyword3"
95<
description"
Thisismypage"
96<
scriptsrc="
97type="
98<
scriptlanguage="
javascript"
99$(function(){
100$("
.sumbit"
).click(function(){
101varv=$("
#account"
).val();
102if($("
).val().length==0){
103$("
.hint"
).text("
用户名不能位空"
).css({"
background-color"
:
green"
});
104}else{
105$.ajax({
106type:
get"
107url:
servlet/JqueryAjaxServlet"
108data:
{account:
).val()},
109dataType:
json"
//很重要!
.预期服务器返回的数据类型,
110success:
function(data){
111$.each(data.jsonArrayy,function(index){
112$.each(data.jsonArray[index],function(key,value){
113alert(key+"
+value);
114});
115});
116
117$("
body"
).append("
div>
+data.account+"
).css("
color"
"
red"
118},
119error:
function(){
120alert("
erroroccured!
121}
122
123});
124}
125});
126});
127<
128<
129
130<
131<
h3align="
center"
jqueryAjaX<
/h3>
132<
hr>
133<
label>
请输入你的账户:
/label>
134<
inputid="
account"
135<
inputclass="
sumbit"
检测"
136<
hint"
137<
138<
139packagetest;
140
141importjava.io.IOException;
142importjava.io.PrintWriter;
143
144importjavax.servlet.ServletException;
145importjavax.servlet.http.HttpServlet;
146importjavax.servlet.http.HttpServletRequest;
147importjavax.servlet.http.HttpServletResponse;
148
149importnet.sf.json.JSONArray;
150importnet.sf.json.JSONObject;
151
152/**
153*Author:
YangT
154*Version:
1.0
155*Create:
2013-3-13上午10:
34:
58
156*/
157publicclassJqueryAjaxServletextendsHttpServlet{
158
159publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
160throwsServletException,IOException{
161System.out.println("
JqueryAjaxServlet:
begin"
162
163//response.setContentType("
164Stringaccount=request.getParameter("
165System.out.println("
account:
+account);
166
167JSONObjectjson=newJSONObject();
168JSONArrayjsonArray=newJSONArray();
169
170JSONObjectmember=null;
171for(inti=1;
i<
3;
i++){
172member=newJSONObject();
173member.put("
name"
"
yangting"
+i);
174member.put("
age"
22+i);
175jsonArray.add(member);
176}
177json.put("
jsonArray"
jsonArray);
178json.put("
account);
179
180//错误:
response.setContentType("
181response.setContentType("
application/x-json"
182PrintWriterpw=response.getWriter();
183pw.print(json.toString());
184
185System.out.println("
jsonarray:
+jsonArray.toString());
186System.out.println("
jsonobject:
+json.toString());
187System.out.println("
end"
188
189pw.close();
190
191}
192
193publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
194throwsIOException,ServletException{
195this.doGet(request,response);
196}
197
198}
199
200
201<
?
xmlversion="
1.0"
encoding="
UTF-8"
202<
web-appversion="
2.5"
203xmlns="
204xmlns:
xsi="
http:
//www.w3.org/2001/XMLSchema-instance"
205xsi:
schemaLocation="
206
207<
display-name>
/display-name>
208<
servlet>
209<
description>
ThisisthedescriptionofmyJ2EEcomponent<
/description>
210<
ThisisthedisplaynameofmyJ2EEcomponent<
211<
servlet-name>
JqueryAjaxServlet<
/servlet-name>
212<
servlet-class>
test.JqueryAjaxServlet<
/servlet-class>
213<
/servlet>
214
215<
servlet-mapping>
216<
217<
url-pattern>
/servlet/JqueryAjaxServlet<
/url-pattern>
218<
/servlet-mapping>
219<
welcome-file-list>
220<
welcome-file>
index.jsp<
/welcome-file>
221<
/welcome-file-list>
222
223<
/web-app>
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java jQueryAjax 实例