【发布时间】:2021-04-05 16:29:42
【问题描述】:
我的“login.html”中有一个表单,单击“登录”按钮后,该表单将提交到我的 java 后端代码。但是,我不知道如何将我的登录页面重定向到新页面。
在以下代码中:
<script>
$(function() {
$('#ff').form({
url: "LoginServlet",
success:function(data){
$.messager.alert(data);
}
});
});
</script>
在成功部分,我的重定向页面将显示在此消息窗口中。但我希望它跳转到 servlet 返回的页面。
这是我的 login.html 代码:
<!DOCTYPE html>
<html lang="en">
<head>
<script>
$(function() {
$('#ff').form({
url: "LoginServlet",
success:function(data){
$.messager.alert(data);
}
});
});
</script>
</head>
<body>
<form id="ff" role="form" method="post">
<div>
<h1>User Login</h1>
</div>
<div>
<input id="username" name="username" class="easyui-textbox" data-options="iconCls:'icon-man',iconWidth:30,iconAlign:'left',prompt:'Username'" style="width:100%;height:35px;" />
</div>
<div>
<input id="password" name="password" class="easyui-passwordbox" data-options="iconWidth:30,iconAlign:'left',prompt:'Password'" style="width:100%;height:35px;" />
</div>
<div>
<a href="javascript:void(0)" class="easyui-linkbutton" onclick="$('#ff').submit()" style="width:100%;height:35px;">Submit</a>
</div>
<div>
<div style="display:inline;">
<a href="javascript:void(0)">Register</a>
</div>
</div>
</form>
</body>
</html>
这是我的 java servlet 代码:
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
IUserService userService = new UserServiceImpl();
User user = userService.login(username, password).get(0);
if (user != null) {
response.sendRedirect(request.getContextPath() + "/index.jsp");
} else {
}
}
}
【问题讨论】:
-
删除成功后会发生什么:function(data){ $.messager.alert(data); } ?
-
您好 Panagiotis,如果我删除了成功部分,我的代码可以成功运行并且没有任何反应。
-
如果您已经解决了问题,请考虑接受答案
标签: java jquery ajax servlet-3.0 jquery-easyui