为什么$.ajax 访问不到servlet

chl19871024 2010-01-28 05:28:38
想写个jQuery 无刷新验证用户登录 但是最后找不到servlet 大家帮看怎么解决解决
login.jsp代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(function() {
$("#username").focus();
$("#login").click(function() {
var name=$("#username").val();
var pass=$("#password").val();
var rand=$("#rand").val();
if(name==""||pass=="")
{
$("#message").text("用户名或密码不能为空");
} else if(rand=="")
{
$("#message").text("验证码不能为空");
}
else
{
$.ajax({
//选择提交方式
type:"POST",
//默认: true,dataType为script时默认为false) jQuery 1.2 新功能,设置为 false 将不会从浏览器缓存中加载请求信息。
cache:"false",
//发送请求地址
url:"loginServlet",
//发送到服务器的数据
data:"username="+name+"&password="+pass+"&rand="+rand,
//返回纯文本字符串
dataType:"text",
//设置请求超时时间(毫秒)
timeout:20000,
//请求失败时调用的函数
error:function(){
$("#message").text("请求失败");
},
//请求成功返回回调函数
success:function(message) {
$("#message").text("message");
}
});
}
});
});
</script>
<script type="text/javascript">
function seeE() {
document.getElementById('vadimg').src='./random.jsp?t='+new Date().getTime();
}
</script>
</head>
<body>
<table>
<thead>
<tr><td colspan="2">用户登录</td></tr>
</thead>
<tbody>
<tr>
<td>登陆帐号:</td>
<td><input type="text" id="username"/></td>
</tr>
<tr>
<td>登陆密码:</td>
<td><input type="password" id="password" /></td>
</tr>
<tr>
<td>验证码:</td>
<td><input height="12" id="rand" name="rand" maxlength="" size="7" />
<img id="vadimg" class="yzm" src="./random.jsp" border="0" />
<a href="javascript:void(0);" onclick="seeE();return false;">看不清楚?</a></td>
</tr>
<tr><td colspan="2"><input type="button" id="login"/></td></tr>
<tr><td colspan="2"><div id="message" style="text-align: center;color: red;"></div></td></tr>
</tbody>
</table>
</body>
</html>


loginServlet代码

package com.Pluto.servelt;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class for Servlet: loginServlet
*
*/
@SuppressWarnings("serial")
public class loginServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public loginServlet() {
super();
}


protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
PrintWriter out=response.getWriter();
System.out.println("访问到");
@SuppressWarnings("unused")
String message="";
@SuppressWarnings("unused")
String name=request.getParameter("username");
System.out.println(name);
@SuppressWarnings("unused")
String pass=request.getParameter("password");
System.out.println(pass);
String num=request.getParameter("rand");
String rand=(String)(request.getSession().getAttribute("rand"));

}
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
login(request,response);
}

/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
login(request,response);
}
}

...全文
707 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
岁月之梦 2010-01-29
  • 打赏
  • 举报
回复
晚了 一步 看来解释太多没用!
岁月之梦 2010-01-29
  • 打赏
  • 举报
回复
url:"loginServlet",
//发送到服务器的数据
data:"username="+name+"&password="+pass+"&rand="+rand,


问题就在这!你看看你的路径!你根本没有请求到!

再一个
protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
PrintWriter out=response.getWriter();
System.out.println("访问到");
@SuppressWarnings("unused")
String message="";
@SuppressWarnings("unused")
String name=request.getParameter("username");
System.out.println(name);
@SuppressWarnings("unused")
String pass=request.getParameter("password");
System.out.println(pass);
String num=request.getParameter("rand");
String rand=(String)(request.getSession().getAttribute("rand"));

}

这个方法 你要返回数据啊,没有看出你把得到的数据怎么返回到页面了!

底下这个是我拷了点代码应该给你点启发:
request.setCharacterEncoding("UTF-8");
response.setContentType("text/xml;charset=utf-8");
response.setHeader("Cache-Control","no-cache");
String name=request.getParameter("name");
PrintWriter out = response.getWriter();
List list = adPosBo.queryAllAdPosLocation(name);

out.print("<?xml version='1.0' encoding='utf-8'?>");
out.print("<SELECT name=\"adPosId\" id=\"adPosId\" onchange=\"changeAdType()\">");
out.print("<option >" + "请选择二级目录" + "</option>");
for (int i = 0; i < list.size(); i++) {
AdPosition position = (AdPosition) list.get(i);
if(position.getValidFlag().equals("Y")){
// out.print("<option value=" + position.getId() + " "+ad3+" == "+position.getTitle()+"\"selected=\'selected'\" >" + position.getTitle() + "</option>");
out.print("<option value=" + position.getId() + " ");

out.print(" >"+ position.getTitle() + "</option> ");
}
}
out.print("</SELECT>");

out.close();
}
这个我就是返回了一个下拉框的文本!
再给你个页面的参考:返回xml

function request(){
jQuery.ajax({
type: 'GET',
url: 'http://localhost:8080/Jdom/hehe.do?act=json',
data: "&info="+info,
cache: false,
dataType: 'xml',

error: function(){
alert('Error loading XML document');
},
success: function(xml){

自己写
}
});
}
仅供参考!
chl19871024 2010-01-29
  • 打赏
  • 举报
回复
是路径的问题。。。 搞定了 谢谢大家!
feyyee 2010-01-29
  • 打赏
  • 举报
回复
把 dataType:"text",
去掉试试
shadow55 2010-01-29
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 xinlan1022 的回复:]
web.xml文件发出来看看,是不是servlet配置路径有问题???

[/Quote]同问!
villagehead 2010-01-29
  • 打赏
  • 举报
回复
1,别用ajax,用个a标签或者直接用url访问那个servlet看看,
不行的话就是servlet配置有问题

2,用ajax访问个别的肯定能访问的servlet,
不行的话就是ajax的程序问题

good luck
bin05 2010-01-29
  • 打赏
  • 举报
回复
你把url随便指到个 a.jsp,再a.jsp里输出看看。能输出 就是web.xml里配错了,不然就是ajax错
xinlan1022 2010-01-28
  • 打赏
  • 举报
回复
web.xml文件发出来看看,是不是servlet配置路径有问题???
zhj92lxs 2010-01-28
  • 打赏
  • 举报
回复
你的jsp 页面的目录,还有你的servlet配置的路径是什么样的
chl19871024 2010-01-28
  • 打赏
  • 举报
回复
自己顶下。。
chl19871024 2010-01-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 waterdr 的回复:]
  url:"loginServlet",

以前用jquery都+action的.不是url:"loginServlet.action"吗?

[/Quote]
还是不行。。。
chl19871024 2010-01-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 waterdr 的回复:]
  url:"loginServlet",

以前用jquery都+action的.不是url:"loginServlet.action"吗?

[/Quote]
我去试试!
waterdr 2010-01-28
  • 打赏
  • 举报
回复
url:"loginServlet",

以前用jquery都+action的.不是url:"loginServlet.action"吗?
chl19871024 2010-01-28
  • 打赏
  • 举报
回复
loginServlet 里还有这个代码 判断验证码的
但是 就是访问不到servlet
if(!rand.equals(num))
{
message="验证码错误";
} else
{
message="";
}
chl19871024 2010-01-28
  • 打赏
  • 举报
回复
点击登录的时候 老是显示 请求失败

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧