• form表单提交路径action="" 时的一种特殊情况


    一、说明:

    当页面的form表达的action=""时,表示表单会提交到当前页面,但是如果当前页面的URL里已经带有一个参数了,每次提交表达时这个参数依然存在,不管form表单里有没有提交该参数。

    例如:

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    System.out.println("userid="+request.getParameter("userid"));
    System.out.println("username="+request.getParameter("username"));
    %>
    <html>
      <head>
      <title>My JSP 'index.jsp' starting page</title>
    </head>
      <body>
        This is my JSP page. <br>
        <form method="post" id="submit" action="">  
        	<input type="text" name="username" id="username" value=""/> username<br>
        	<input type="submit" value="提交"/>
        </form>
      </body>
    </html>
    

    若访问URL为:http://localhost:8080/MyWebPro/index.jsp?userid=1

    则控制台打印:

    userid=1
    username=null
    

    输入username提交表达可以发现后台打印:

    userid=1
    username=jack
    

    因此,可以发现,如果action提交到当前页面时,如果当前页面URL里带有参数,则每次提交表单时该参数仍然会被提交。

    二、总结:

    之前没有遇过这种情况,今天自己动手测试了一下并记录下来以备查询。

  • 相关阅读:
    IIT(ISM) Virtual Farewell E Dictator's plan for Valentine's day! 二分
    HDU
    LightOJ
    HDU 4407 Sum 容斥原理
    HDU 4686 Arc of Dream 矩阵快速幂
    uoj418
    Gym101237C The Palindrome Extraction Manacher
    Codeforces 100032 K
    codechef Suffix Palindromes
    Codeforces 932G Palindrome Partition
  • 原文地址:https://www.cnblogs.com/crazytrip/p/5476802.html
Copyright © 2020-2023  润新知