日期:2014-05-18  浏览次数:20654 次

Servlet中无法实现跳转了,真找不出来问题出在哪了
今天刚开始学习jQuery EasyUI, 做了个简单的登录窗口, 用户和密码两个字段, 访问的是Servlet, 可以在后台打印出两个字段的值,简单的验证了一下值然后跳转, 结果浏览器无反应

页面代码如下:

<div class="easyui-panel" title="用户登录" style="width:300px">
<div style="padding:10px 0 0 40px">
<form id="ff" method="post" action="http://localhost:8080/easyui/UserLoginServlet">
<table>
<tr>
<td>
用户:
</td>
<td>
<input class="ww" type="text" name="name"></input>
</td>
</tr>
<tr>
<td>
密码:
</td>
<td>
<input class="ww" type="password" name="password"></input>
</td>
</tr>
</table>
</form>
</div>
<div style="text-align:center;padding:5px">
<a href="javascript:void(0)" class="easyui-linkbutton"
onclick="submitForm()">登录</a>
<a href="javascript:void(0)" class="easyui-linkbutton"
onclick="clearForm()">取消</a>
</div>
</div>
<script>
function submitForm(){
$('#ff').form('submit');
}
function clearForm(){
$('#ff').form('clear');
}
</script>


后台Servlet如下:

public class UserLoginServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletExceptionIOException {
this.doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

request.setCharacterEncoding("UTF-8");

String name = request.getParameter("name");
String password = request.getParameter("password");

System.out.println("用户: " + name);
System.out.println("密码: " + password);

if("admin".equals(name) && "123".equals(password)) {
request.getRequestDispatcher("/success.html").forward(request, response);
} else {
response.sendRedirect("http://localhost:8080/easyui/fail.html");
}
}
}


工程web.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  
  <servlet>
    <servlet-name>UserLoginServlet</servlet-name>
    <servlet-class>com.wisher.easyui.servlet.UserLoginServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>UserLoginServlet</servlet-name>
    <url-pattern>/UserLoginServlet</url-pattern>
 &nbs