實現重定向
什么是重定向?
客戶端請求一個web資源,這個web資源讓客戶端訪問另一個web資源
案例:用戶登錄
void sendRedirect(String var1) throws IOException;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/*
resp.setHeader("Location","/r/img");
resp.setStatus(302);
*/
resp.sendRedirect("/r/img");//重定向
}
<%--這里提交的路徑,需要尋找到項目的路徑--%>
<%--${pageContext.request.contextPath}代表當前的項目--%>
<form action="${pageContext.request.contextPath}/login" method="get">
用戶名:<input type="text" name="username"> <br>
密碼:<input type="password" name="password"> <br>
<input type="submit">
</form>
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//處理請求
String username = req.getParameter("username");
String password = req.getParameter("password");
System.out.println(username+":"+password);
//重定向時候一定要注意,路徑問題,否則404;
resp.sendRedirect("/r/success.jsp");
}

浙公網安備 33010602011771號