表单后的Servlet中的URL重定向

时间:2015-12-11 10:38:38

标签: java servlets

在我的Java Web应用程序中,我有一个唯一的Front Controller,用于映射所有请求,以及执行逻辑的各种控制器,并返回表示用户转发到的下一页的字符串。

这样可行,但是当我使用post方法提交表单时,表单操作会附加到地址栏中的URL。例如,如果LoginController中的登录方法返回false(以便nextPage =" /index.jsp"),它会正确地重定向到该页面,但在地址栏中我会有/ MyAPP / app / home.jsp无论如何。有没有办法避免这种情况?

我看了一下Post / Redirect / Get模式,但是我想弄清楚如何在不积极改变结构的情况下实现它。

@WebServlet("/app/*")
public class FrontController extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Map<String, Controller> controllers = new HashMap<String, Controller>();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        processRequest(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        processRequest(req, resp);
    }

    private void processRequest(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String pathInfo = req.getPathInfo();
        String controllerName = pathInfo.substring(pathInfo.lastIndexOf('/'));
        Controller c = controllers.get(controllerName);
        String resource = c.action(req, resp);
        req.getRequestDispatcher(resource).forward(req, resp);
    }

    @Override
    public void init() throws ServletException {
        super.init();
        controllers.put("/index.jsp", new IndexController());
        controllers.put("/home.jsp", new LoginController());
    }
}


public class LoginController implements Controller {

    private static final String USERNAME_PARAM = "username";
    private static final String PASSWORD_PARAM = "password";
    private static final String USERBEAN_ATTR = "userBean";

    public String action(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameter(USERNAME_PARAM);
        String password = request.getParameter(PASSWORD_PARAM);
        boolean result = false;
        UserBean userBean = (UserBean)request.getSession().getAttribute(USERBEAN_ATTR);

        userBean.setUsername(username);
        userBean.setPassword(password);

        if (!username.isEmpty() && !password.isEmpty())
            result = userBean.doLogin();

        String nextPage = result ? "/home.jsp" : "/index.jsp";

        if (!result)
            request.setAttribute("error", "Login Error");

        return nextPage;
    }
}

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Welcome!</title>
</head>
<body>
    <h1>Welcome</h1>

    <form
        action="${pageContext.request.contextPath}/app/home.jsp"
        method="post">
        Username: <input type="text" name="username"> <br>
        Password: <input type="text" name="password"> <br> <input
            type="submit" value="Log In" />
    </form>
    <p>${error}</p>
</body>
</html>

0 个答案:

没有答案