xiaoye-Blog

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

关于form表单的action的探究:

  1. 加"/":以当前主机为所在目录:localhost:8080进行寻找

    <form action="/index.jsp">
       <input type="submit" value="first">
    </form>
    <!--该表单提交后会跳转到:
       http://localhost:8080/index.jsp-->
  2. 不加"/":

    1. 通过另一个jsp表单的action跳转到当前jsp

      <!--该表单路径:http://localhost:8080/test/index.jsp-->
      <form action="html/first.jsp">
       <input type="submit" value="index">
      </form>

      <!--跳转后first.jsp后
         first.jsp路径:http://localhost:8080/test/html/first.jsp-->
      <form action="index.jsp">
         <input type="submit" value="first">
      </form>
      <!--此时路径会跳转到
         http://localhost:8080/test/html/index.jsp
         也就是找不到,是根据当前的路径也就是http://localhost:8080/test/html去找要跳转的路径的,使用应该改为action="../index.jsp"
         同理,此时action写servlet也是在http://localhost:8080/test/html目录上进行,所以也需要../-->

      <!--假设是通过index.jsp跳转到firstServlet(路径:http://localhost:8080/test/first),然后通过该servlet跳转到first.jsp-->
      <form action="first">
       <input type="submit" value="index">
      </form>

      req.getRequestDispatcher("html/first.jsp").forward(req,resp);

      <form action="index.jsp">
         <input type="submit" value="first">
      </form>
    2. 通过servlet跳转到当前jsp,也就是通过请求转发

      <form action="first">
       <input type="submit" value="index">
      </form>

      req.getRequestDispatcher("html/first.jsp").forward(req,resp);

      <form action="index.jsp">
         <input type="submit" value="first">
      </form>
      <!--此时如果不加斜杠的那么表单是根据当前webapp的跟目录进行请求,也就是http://localhost:8080/test/-->
posted on 2022-07-24 13:04  小也取不到名字  阅读(269)  评论(0)    收藏  举报