Servlet登录到站点后未重定向页面,在网络中,仪表板文件的状态代码超过200,但没有重定向

问题描述 投票:0回答:1

我有一个带有登录系统的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);
        }
    }
}

enter image description here

java servlets
1个回答
0
投票
改为使用resp.sendRedirect

resp.sendRedirect(req.getContextPath() + "/redirected");
我们使用原始响应对象将该请求重定向到另一个URL
© www.soinside.com 2019 - 2024. All rights reserved.