ajax 的问题 有哪位 给小弟看看,。
<%@ 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 'User.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
function checkUser(ouser){
var uname=ouser.value;
if(!uname){
alert("用户名不能为空");
ouser.focus;
}
//发送请求到服务器,判断用户名是否存在
//Ajax代码实现
// 发送请求到服务器,判断用户名是否存在
// 请求字符串
//var url = "servlet/doReg?uname="+uname; //GET 方式
var url = "InsertUser?userid="+uname; //POST 方式
//var userinfo="="+uname;
// 1. 创建XMLHttpRequest组件
xmlHttpRequest = createXmlHttpRequest();
// 2. 设置回调函数
xmlHttpRequest.onreadystatechange = haoLeJiaoWo;
// 3. 初始化XMLHttpRequest组件
xmlHttpRequest.open("GET",url,true); //GET方式
//xmlHttpRequest.open("POST",url,true);//POST方式
//xmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");//POST方式需要设置
// 4. 发送请求
xmlHttpRequest.send(null);
}
function haoLeJiaoWo(){
if( xmlHttpRequest.readyState == 4
&& xmlHttpRequest.status == 200){
var b = xmlHttpRequest.responseText;
b=b.replace(/(^\s*)|(\s*$)/g,"");
if(b=="true"){
alert("用户名已经存在");
}else{
alert("用户名可以使用");
}
}
}
function createXmlHttpRequest(){
if(window.XMLHttpRequest){//新版本IE浏览器(IE7及以上版本)或其他浏览器
return new XMLHttpRequest();
}else { //老版本IE浏览器(包括IE5和IE6)
return new ActiveXObject("Microsoft.XMLHTTP");
}
}
</script>
</head>
<body>
<form action="checkUser" method="get">
<table width="200" border="1">
<tr>
<td>用户名:</td>
<td><input type="text" name="userid" onblur="checkUser(this);"></td>
</tr>
<tr>
<td>密 码</td>
<td><input type="password" name="userpwd"></td>
</tr>
<tr>
<td>姓名:</td>
<td><input type="text" name="userName"></td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="text" name="userAge"></td>
</tr>
<input type="submit" value="注册">
</table>
</form>
</body>
</html>
package Admin;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class InsertUser extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
AdminUser au = new AdminUser();
au.setId(request.getParameter("userid"));
au.setAge(request.getParameter("userAge"));
String name=request.getParameter("userName");
String pwd=request.getParameter("userpwd");
//String Name=new String(name.getBytes("ISO-8859-1"),"UTF-8");
//String Pwd= new String(pwd.getBytes("ISO-8859-1"),"UTF-8");
au.setName(name);
au.setPassword(pwd);
PrintWriter out = response.getWriter();
oracle or = new oracle();
String a=or.selectName(au);
System.out.println(a);
boolean f =false;
if(a!=null){
f=true;
out.print(f);
out.flush();
out.close();
}else{
out.print(f);
boolean flag= or.save(au);
if(flag){
request.getRequestDispatcher("/succeed.jsp").forward(request, response);
}else{
request.getRequestDispatcher("/User.jsp").forward(request, response);
}
}
}
我添加到数据库里面判断 , 数据库里面有这个值 但是他提示 没有这个值。
--------------------编程问答-------------------- 查询数据库看看,数据有没有?然后继续检索问题所在!。。。。我比较懒,自己的代码都懒的看,更不用说不是自己的了! --------------------编程问答-------------------- 学习,应该调试跟踪就可以找到问题了。显示查询有没有值,然后是否保存,哪里出错一看便知 --------------------编程问答--------------------
顶一下 --------------------编程问答-------------------- 调试一下就找到问题了 --------------------编程问答-------------------- 1、这种问题细心点,自己调试应该都可以调试出来。
2、没看懂or.save(au);是什么意思。
3、//var url = "servlet/doReg?uname="+uname; //GET 方式
var url = "InsertUser?userid="+uname; //POST 方式
这种方式,在Controller中应该只能渠道uNmae或者userId的值。
String name=request.getParameter("userName");
String pwd=request.getParameter("userpwd");
无用 --------------------编程问答-------------------- 一直在用jQuery,你也用jQuery.
代码错对不想看了,看看jQuery吧. --------------------编程问答-------------------- 你可以自己调试一下 看看有没有跳进doGet方法中去 要是没有执行到doGet 就说明你的js有问题 在慢慢一步一步去验证你的js哪里有问题 --------------------编程问答-------------------- 告诉你一个调试的方法。那就是在ajax后台查询语句的后面打印一句话,看是否执行了查询。然后打印这个值。如果这里都没有。那你的SQL语句可能有问题。 --------------------编程问答-------------------- 同意8楼的,你在数据库写同样的查询语句,如果查不出来,说明sql语句有问题,如果能查出来,使用跟踪调试就能找出问题了 --------------------编程问答-------------------- 我怎么看着代码像是北大青鸟的?????
补充:Java , Java EE