ajax 传递参数到servlet的问题

hzzjlaozhao 2014-03-11 11:47:37
问题:当点击“登录”按钮时,似乎无法把ajax的参数传递到servlet这,不知道错在哪里,麻烦大家指点下

index.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" />
<link href="${pageContext.request.contextPath}/js/themes/icon.css" rel="stylesheet" type="text/css"/>
<link href="${pageContext.request.contextPath}/js/themes/black/easyui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.easyui.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/locale/easyui-lang-zh_CN.js"></script>
<script>
var loginAndRegDialog;
$(function(){
loginAndRegDialog=$("#loginAndRegDialog").dialog({
buttons:[{text:'登录',
handler:function(){
$.ajax({
type:"post",//请求方式
url:"/Logincl",

data:{
username:$('#username input[name=username]').val(),
password:$('#password input[name=password]').val()
},
success:function(data,textStatus){
$("#result").html(data);

}
});
}},
{text:'取消',
handler:function(){


}
}]
});

});
</script>
<title>Insert title here</title>
</head>
<body>
<div id="loginAndRegDialog" title="用户登录" Style="width:250px;height:200px;">
<form id="loginInputForm" action="Logincl" method="post">
<table>
<tr>
<th align="right"> 用户名:</th>
<td><input name="username" type="text" /></td>
</tr>
<tr> <th align="right">密码 :</th>
<td><input name="password" type="password" /></td>
</tr>
</table>
</form>
</div>
<div>id=result</div>
</body>
</html>

servlet
package com.test;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class Logincl
*/
@WebServlet("/Logincl")
public class Logincl extends HttpServlet {
private static final long serialVersionUID = 1L;


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
try{
String u = request.getParameter("username");
String p = request.getParameter("password");
if(u.equals("admin")&&p.equals("123")){
request.setAttribute("username", u);
request.getRequestDispatcher("welcome.jsp").forward(request, response);
//response.sendRedirect("welcome.jsp");
}else{
response.sendRedirect("error.jsp");

}
}
catch(Exception ex){
ex.printStackTrace();
}
}


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doGet(request, response);
}

}

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>jqueryui</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Logincl</servlet-name>
<servlet-class>com.test.Logincl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Logincl</servlet-name>
<url-pattern>/Logincl</url-pattern>
</servlet-mapping>
</web-app>

...全文
941 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
noahxinhao 2014-03-11
  • 打赏
  • 举报
回复
这样的问题,先找请求路径是不是正确的,先确定请求是能到达后台的,可以设置一个断点,能断住请求,再看前台传过来的数据在请求参数里面有没有,如果没有,很显然就是前台的问题,如果有,那就是你后台拿数据的方式问题了.
Derek-Chen 2014-03-11
  • 打赏
  • 举报
回复
data:{ "username":$('input[name=username]').val(), "password":$('input[name=password]').val() }
Ace_Luffy 2014-03-11
  • 打赏
  • 举报
回复
servlet里面配置url路径,前台的url是不是不需要‘/’了,不确定了
suciver 2014-03-11
  • 打赏
  • 举报
回复
楼主发代码请使用代码格式。这样的真没耐心看。 两点错误,第一个地方版主已经说了jquery的选择器出错,第二个是最致命的ajax后台无需跳转,直接用response输出即可

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String u = request.getParameter("username");
String p = request.getParameter("password");
response.setContentType("text/plain;charset='utf-8'");
PrintWriter pw=response.getWriter();
if(u.equals("admin")&&p.equals("123")){
  pw.write("登陆成功!");
}else{
  pw.write("用户名或密码错误!");
}
hzzjlaozhao 2014-03-11
  • 打赏
  • 举报
回复
大家能帮忙看下吗,急
hzzjlaozhao 2014-03-11
  • 打赏
  • 举报
回复
好的,谢谢版主热心回答,我到jsp的版再问问看
Go 旅城通票 2014-03-11
  • 打赏
  • 举报
回复
java就不懂了,你的选择器绝对是错的,获取不到值,获取到值后动态页收不到就是你配置的问题了 还有就是不要再服务器端response.sendRedirect("error.jsp");跳转,浏览器不会跳转的
hzzjlaozhao 2014-03-11
  • 打赏
  • 举报
回复
试过了,还是没用何解
Go 旅城通票 2014-03-11
  • 打赏
  • 举报
回复
data:{ username:$('#username input[name=username]').val(), password:$('#password input[name=password]').val() } 选择器错了。。对象都没有获取到 data:{ username:$('input[name=username]').val(), password:$('input[name=password]').val() }

81,090

社区成员

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

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