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>
    
  • 相关阅读:
    poj2248
    poj2249
    poj2255
    电脑族每天必喝的四杯茶
    如何获得每一行的ROWID
    网上勾引MM定义大全
    A Forever Friend (永远的朋友)
    生活中的经典感人语句
    经理人必看的10个管理网站
    男人必须明白的22个道理
  • 原文地址:https://www.cnblogs.com/yedan/p/13438359.html
Copyright © 2011-2022 走看看