使用 jquery 框架:下载 jquery.js
新建一个 web 工程 ajax ;
在 webRoot 下新建一个 jslib 文件夹:专门存放 js 文件;
在 webRoot 下新建一个 html/jsp 页面:
login.html
在 jslib 下新建一个 js 文件:
verify.js ;
function verify()
{
var paramObj=encodeURI(encodeURI($( "#username" ).val()));
$.get( "TestSvlt?username=" +paramObj, null ,callback);
}
function callback(data)
{
var resultObj=$( "#result" );
resultObj.html(data);
}
新建一个 servlet : AjaxLogin 类;在 doGet 方法中写
response.setContentType( "text/html;charset=utf-8" );
PrintWriter out=response.getWriter();
String name=request.getParameter( "username" );
String username= URLDecoder.decode (name, "UTF-8" );
System. out .println(username);
if ( null ==username|| "" .equals(username))
{
out.print( "username is not null" );
}
else
{
if (! "lej" .equals(username))
{
out.print(username+ " not existing" );
}
else {
out.print(username+ " login successs" );
}
}
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn