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里帶有參數,則每次提交表單時該參數仍然會被提交。

 

二、總結:

之前沒有遇過這種情況,今天自己動手測試了一下並記錄下來以備查詢。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM