问题:当点击“登录”按钮时,似乎无法把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>