zoukankan      html  css  js  c++  java
  • response.sendRedirect页面跳转的方法总结——实习第六天

    今天想到要写这个博客完全是因为今天在这上面摔了一跤,同时也意识到了对于一个学Java的人来讲,知其然并且知其所以然是有多么的重要。

    今天报了一个错误,讲的是空指针异常,原因就是在response.sendRedirect()这个上面。下面对我所遇见到的几种页面跳转方法做一个总结,以后若是再遇见,继续补充!

    页面跳转方式1——herf

    在一些html的文档中,用herf实现页面跳转的比较常见,也很好用。

    页面跳转方式2——利用表单action

    <td>
          <form method="post" action="ChangrCount.jsp" id="form1">
                  <input type="hidden" name="albumId" value="${item.albumId}" />
                  <input style="50px;" type="text" name="count" value="${item.count}" id="namecount"/>                                
          </form>
    </td>

    近期学习的项目写action都是在jsp里面写的,虽然应该在action写,但是在JSP写的话,也是很好理解的。

    页面跳转方式3——response.sendRedirect("cart.jsp");

    response.sendRedirect("cart.jsp");的功能是地址重定向(页面跳转);     

    
    
    
    
    服务器端进行转向的4种方法:
    1. servletcontext 的 getrequestdispatcher()
    路径必须是相对上下文的绝对路径
    2. servletcontext 的 getnameddispatcher()
    3. servletrequest 的 getrequestdispatcher()
    可以是相对也可以是绝对
    4. servletresponse 的 sendredirect()进行转向。

    response.sendredirect(url);  对服务器的响应进行重定向。当server作出响应后,client客户端的请求的生存周期就终止了。
    这个时候再用request.getparameter()或request.getattribute()得到的只能是null。
    getservletcontext().getrequestdispatcher(url).forward(request,response);它则可以认为是对client的请求(request)进行传递,在server没有进行响应前,即没有response一直可以进行传递--重定向。

    目前就写这些了。
  • 相关阅读:
    Web前段学习索引
    Python学习索引
    Centos7下安装Docker(详细的新手教程)
    Django部署阿里云服务器(nginx+uwsgi)
    (转)Mongo db 与mysql 语法比较
    (转)mongo基本常用命令
    python操作mongodb的基本操作命令
    http响应码
    python回调函数
    Flask框架的使用
  • 原文地址:https://www.cnblogs.com/zxcjj/p/7082323.html
Copyright © 2011-2022 走看看