栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Servlet重定向和转发

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Servlet重定向和转发

重定向和转发的区别

相同点

  • 页面都会实现跳转

不同点

  • 请求转发的时候,URL地址栏不会发生变化
  • 重定向的时候,URL地址栏会发生变化

HttpServletRequest代表客户端的请求,用户通过Http协议访问服务器,HTTP请求中的所有信息会被封装到HttpServletRequest,通过这个HttpServletRequest的方法,获得客户端的所有信息

重定向:

public class RequestTest extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //处理请求
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //重定向
        resp.sendRedirect("/success.jsp");
		//转发
        //req.getRequestDispatcher("success.jsp").forward(req,resp);
    }

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

web.xml


      request
      com.kuang.servlet.RequestTest
    
  
    request
    /
  

index.jsp

<%--设置编码类型为utf-8,不然会乱码--%>
<%@ page contentType="text/html; charset=UTF-8" %>


登录
<%--这里提交的路径,需要寻找到项目的路径--%>
<%--${pageContext.request.contextPath}代表当前的项目--%>
用户名:
密码:

success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


success



运行结果:

响应状态码

200:请求响应成功 200

3xx:请求重定向

4xx:找不到资源 404

5xx:服务器代码错误 500 网关错误 502

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/658335.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号