在 IntelliJ IDEA 8.1中编写一个ajax jquery实例,取不到页面上的值0
brnm12942014.08.17浏览115次分享举报
public class AjaxServlet extends HttpServlet{
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
throws ServletException, IOException {
doPost(httpServletRequest,httpServletResponse); //To change body of overridden methods use File | Settings | File Templates.
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
response.setContentType("text/html;charset=GB2312");
request.setCharacterEncoding("GB2312");
PrintWriter out = response.getWriter();
String oldname=null;
try {
oldname = request.getParameter("username");
System.out.println(oldname);
}catch(Exception e)
{e.getStackTrace();}
String password= request.getParameter("password");
if(oldname==null ||oldname.length()==0)
{ out.println("该用户名不能为空值");}
else {
System.out.println("2222222");
String name=new String(oldname.getBytes("ISO8859-1"));
if(name.equals("amaker")){ System.out.println("*****");
out.println("该用户名称已存在!!");
}
else {
System.out.println("-------------");
out.println("验证通过,你输入的信息是:"+"["+name+"]"+"["+password+"]");
}
}
}
catch (Exception e)
{
System.out.println("异常");
}
}
}
verify.js
function verify()
{
alert("你点击了按钮,接下来来将执行验证了!!!");
// 基于DOM技术 document.getElementById("userName");
//基于Jquery技术
// $("#userName")
var jqueryObj = $("#username");
var jqueryObj2 =$("#password");
var username= jqueryObj.val();
var password = jqueryObj2.val();
alert (username+": "+password);
$.get("servlet/AjaxServlet?name=" +username,null,callback);
//回调函数callback()
// 真正的验证操作要在callback中做
function callback(data)
{
alert("服务器端的信息在这里返回!!");
alert(data);
var resultObj =$("#result");
resultObj.html(data);
alert("");
}
}
Validator.html:
/p>
"http://www.w3.org/TR/html4/loose.dtd">
hello,this is my first validateing html用户名称:
用户密码: