用prototype开源框架写了一个简单的ajax登录程序。
TestAjax.jsp
<%
...
@ page language="java" import="java.util.*" pageEncoding="GBK"
%>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
<%
...
String contextPath = request.getContextPath();
%>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
html
>
<
head
>
<
title
>
操作完成
</
title
>
<
meta
http-equiv
="Content-Type"
content
="text/html; charset=GBK"
>
<
link
rel
="stylesheet"
href
="<%=contextPath%>/mc/common/css/grayStyle.css"
>
<
script
language
="JavaScript"
src
="<%=contextPath%>/mc/common/include/js/prototype.js"
></
script
>
</
head
>
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
<
script
type
="text/javascript"
>
...
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
function login()...{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
var name = $F("name");
var psd = $F("password");
var paras = 'name='+name+'&password='+psd;
var url = "<%= contextPath %>/mc/inducorp/mc/di/agreement/test/TestAjaxServlet";
var myAjax = new Ajax.Updater(
'result',
url,
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
method: 'get',
parameters: paras
});
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
function showResponse(originalRequest)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
$("result").value=originalRequest.responseText;
//alert("showResponse:"+originalRequest.responseText);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/0196c3df5ea9e936f21e9932cca91014.gif)
</
script
>
<
body
>
<
form
action
= ""
method
="get"
name
="agreementform"
>
<
label
>
Name:
<
input
type
="text"
name
="name"
id
="name"
>
</
label
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
p
>
<
label
>
password:
<
input
type
="text"
name
="password"
id
="password"
>
</
label
>
</
p
>
<
p
>
<
label
>
<
input
type
="button"
name
="button"
id
="button"
value
="Login"
onClick
="login()"
>
</
label
>
</
p
>
<
p
>
</
p
>
<
p
>
</
p
>
<
p
><
span
id
="content"
>
<
textarea
id
=result
cols
=60
rows
=10
></
textarea
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
</
span
></
p
>
</
form
>
</
body
>
</
html
>
在Ajax.Updater构造方法中传入了一个元素 id,用于显示后台处理后传回的结果。
TestAjaxServlet.java
package
com.icss.mc.inducorp.mc.di.agreement.test;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
java.io.Writer;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
com.icss.mc.base.servlet.BaseServlet;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
TestAjaxServlet
extends
BaseServlet
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public void performTask(HttpServletRequest request,
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
HttpServletResponse response) throws Exception ...{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
String name = this.getParameter(request, "name", true,true,false,"");
String password = this.getParameter(request, "password",true,true,false,"");
log.debug("登录:"+name+":"+password);
String msg = "登录失败!";
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if(name.equals("test")&&password.equals("test"))...{
msg = "登录成功!";
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
response.setContentType("text/xml; charset=UTF-8");
response.setHeader("Cache-Control","no-cache");
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
StringBuffer sb = new StringBuffer();
sb.append(msg);
Writer writer = response.getWriter();
writer.write(sb.toString());
writer.flush();
writer.close();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}