zoukankan      html  css  js  c++  java
  • javaWeb学习笔记之关于重定向和请求转发的区别

    • 相同点

      都会实现页面跳转

    • 不同点

      请求转发的时候,URL路径不会发生改变

      重定向,URL路径会发生改变

    简单代码实现重定向:

    修改index.jsp实现一个简单的表单

    <%--这里提交的路径,需要寻找到项目的路径--%>
    <%--${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>
    
    public class RedirectServlet extends HttpServlet {
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.setCharacterEncoding("utf-8");
            resp.setCharacterEncoding("utf-8");
            String userName = req.getParameter("username");
            String password = req.getParameter("password");
            //进行重定向
            resp.sendRedirect("/r/success.jsp");
        }
    }
    

    新的jsp页面:

    <%@ page contentType="text/html;charset=UTF-8" language="java" %> 
    <html> 
        <head>
            <title>Title</title> 
        </head> 
        <body> 
            <h1>Success</h1> 
        </body> 
    </html>
    
  • 相关阅读:
    随记
    bzoj3551 [ONTAK2010]Peaks加强版
    bzoj2763 [JLOI2011]飞行路线
    bzoj1758 [Wc2010]重建计划
    bzoj1857 [Scoi2010]传送带
    bzoj4519 [Cqoi2016]不同的最小割
    bzoj2229 [Zjoi2011]最小割
    bzoj4129 Haruna’s Breakfast
    bzoj1835 [ZJOI2010] 基站选址
    bzoj2160 拉拉队排练
  • 原文地址:https://www.cnblogs.com/yedan/p/13438359.html
Copyright © 2011-2022 走看看