我有一个带有登录系统的Web应用程序,登录网站时,页面没有重定向到仪表板,当检查chrome浏览器终端时,信息中心文件的状态代码为200,但没有页面重定向。
package DAO;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AjaxController extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
try {
switch(request.getServletPath()){
case "/Login" : {
String email = request.getParameter("email").trim();
String senha = request.getParameter("senha").trim();
UsuarioDAO usudao = new UsuarioDAO();
if(usudao.login(email, senha)){
response.setStatus(response.SC_MOVED_TEMPORARILY);
response.setHeader("location", "http://localhost:8080/dbdfacu2/dashboard.jsp");
}
}
break;
case "/Register" : {
String nome = request.getParameter("nome").trim();
String email = request.getParameter("email").trim();
String senha = request.getParameter("senha").trim();
UsuarioDAO usudao = new UsuarioDAO();
try (PrintWriter out = response.getWriter()) {
out.println(usudao.inserir(nome, email, senha));
}
}
break;
}
} catch (Exception erro) {
throw new RuntimeException("ERRO Ajax" + erro);
}
}
}
resp.sendRedirect(req.getContextPath() + "/redirected");
我们使用原始响应对象将该请求重定向到另一个URL