当前位置: 代码迷 >> J2EE >> Servlet中无法实现跳转了,真找不出来有关问题出在哪了
  详细解决方案

Servlet中无法实现跳转了,真找不出来有关问题出在哪了

热度:18   发布时间:2016-04-19 22:46:24.0
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>
  </servlet-mapping>
  
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>


success.html和fail.html都是创建在WebRoot目录下面
------解决方案--------------------
你把form表单里面的action值改成你servlet在xml中的servlet-name试一试,就是UserLoginServlet
------解决方案--------------------
有异常错误信息吗?
------解决方案--------------------
request.getRequestDispatcher改为this.getServletContext().getRequestDispatcher
------解决方案--------------------
request.getRequestDispatcher("/success.html").forward(request, response);

response.sendRedirect("http://localhost:8080/easyui/fail.html");

success.html和fail.html都是创建在WebRoot目录下面?

目测是路径
------解决方案--------------------
如果 都进了doPost方法,那几行跳转代码是没有问题的啊?你试着简单的测试,不要使用jquery
js像这样  document.forms[0].submit();看是不是环境的问题。
  相关解决方案