English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Quando navighiamo sui siti web, spesso dobbiamo inviare informazioni al server e farle elaborare dai programmi in background. Nelle browsers possiamo inviare dati al server utilizzando i metodi GET e POST.
Il metodo GET aggiunge le informazioni di codifica della richiesta alla fine dell'indirizzo URL, separato dall'URL stesso con il segno di interrogazione "?". Ecco un esempio:
http://it.oldtoolbag.com/hello?key1=value1&key2=value2
GET 方法是浏览器默认传递参数的方法,一些敏感信息,如密码等建议不使用 GET 方法。
用 get 时,传输数据的大小有限制(注意不是参数的个数有限制),最大为 1024 字节。
一些敏感信息,如密码等我们可以通过 POST 方法传递,POST 提交数据是隐式的。
POST 提交数据是不可见的,GET 是通过在 url 里面传递的(可以看一下你浏览器的地址栏)。
JSP 使用 getParameter() 来获得传递的参数,getInputStream() 方法用来处理客户端的二进制数据流的请求。
getParameter(): 使用 request.getParameter() 方法来获取表单参数的值。
getParameterValues(): 获得如 checkbox 类(名字相同,但值有多个)的数据。 接收数组变量,如 checkbox 类型
getParameterNames():该方法可以取得所有变量的名称,该方法返回一个 Enumeration。
getInputStream():调用此方法来读取来自客户端的二进制数据流。
以下是一个简单的 URL,并使用 GET 方法来传递 URL 中的参数:
http://localhost:8080/testjsp/main.jsp?name=基础教程网&url=http://ww.oldtoolbag.com
testjsp 为项目地址。
以下是 main.jsp 文件的 JSP 程序用于处理客户端提交的表单数据,我们使用 getParameter() 方法来获取提交的数据:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"% <%@ page import="java.io.*,java.util.*" %> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>基础教程网(oldtoolbag.com)</title> </head> <body> <h1>使用 GET 方法读取数据</h1> <ul> <li><p><b>Nome del sito:</b> <%= request.getParameter("name")%> </p></li> <li><p><b>Indirizzo web:</b> <%= request.getParameter("url") %> </p></li> </ul> </body> </html>
接下来我们通过浏览器访问 http://localhost:8080/testjsp/main.jsp?name=JSP教程&url=http://ww.oldtoolbag.com 输出结果如下所示:
以下是一个简单的 HTML 表单,该表单通过 GET 方法将客户端数据提交 到达 main.jsp Nel file:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>基础教程网(oldtoolbag.com)</title> </head> <body> <form action="main.jsp" method="GET"> Nome del sito: <input type="text" name="name"> <br /> Indirizzo web: <input type="text" name="url" /> <input type="submit" value="Invia" /> </form> </body> </html>
Salva il codice HTML sopra nel file test.htm. Posiziona questo file nella directory WebContent del progetto JSP corrente (nella stessa directory del main.jsp).
通过访问 http://localhost:8080/testjsp/test.html Invia i dati del modulo al file main.jsp, come mostrato nella figura seguente:
Inserisci le informazioni nei campi "Nome del sito" e "Indirizzo web", quindi clicca sul pulsante "Invia". Questo genererà i risultati.
Ora passiamo a utilizzare il metodo POST per trasmettere i dati del modulo, modificando i file main.jsp e Hello.htm come segue:
Codice del file main.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"% <%@ page import="java.io.*,java.util.*" %> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>基础教程网(oldtoolbag.com)</title> </head> <body> <h1>Lettura dei dati con metodo POST</h1> <ul> <li><p><b>Nome del sito:</b> <% // Risolve il problema della codifica cinese sbagliata String name = new String((request.getParameter("name")).getBytes("ISO-8859-1"),"UTF-8"); %> <%= name %> </p></li> <li><p><b>Indirizzo web:</b> <%= request.getParameter("url") %> </p></li> </ul> </body> </html>
Usiamo nel codice new String((request.getParameter("name")).getBytes("ISO-8859-1"),"UTF-8")Per convertire la codifica e prevenire la comparsa di caratteri cinese sbagliati.
Di seguito è il codice modificato di test.htm:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>基础教程网(oldtoolbag.com)</title> </head> <body> <form action="main.jsp" method="POST"> Nome del sito: <input type="text" name="name"> <br /> Indirizzo web: <input type="text" name="url" /> <input type="submit" value="Invia" /> </form> </body> </html>
通过访问 http://localhost:8080/testjsp/test.html 提交表单数据到 main.jsp 文件,如下所示:
Le caselle di selection checkbox possono trasmettere un numero anche molto elevato di dati.
Di seguito è riportato un semplice codice HTML, salvato nel file test.htm:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>基础教程网(oldtoolbag.com)</title> </head> <body> <form action="main.jsp" method="POST" target="_blank"> <input type="checkbox" name="google" checked="checked" /> Google <input type="checkbox" name="w3codebox" /> 基础教程网 <input type="checkbox" name="taobao" checked="checked" /> Taobao <input type="submit" value="选择网站" /> </form> </body> </html>
Il codice sopra è visibile nel browser come segue:
Ecco il codice del file main.jsp, utilizzato per elaborare i dati delle caselle di selection:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"% <%@ page import="java.io.*,java.util.*" %> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>基础教程网(oldtoolbag.com)</title> </head> <body> <h1>Lettura dei dati da caselle di selection</h1> <ul> <li><p><b>Se selezionato Google:</b> <%= request.getParameter("google")%> </p></li> <li><p><b>Se selezionato基础教程网:</b> <%= request.getParameter("w3codebox")%> </p></li> <li><p><b>Se selezionato淘宝:</b> <%= request.getParameter("taobao")%> </p></li> </ul> </body> </html>
通过访问 http://localhost:8080/testjsp/test.html 提交表单数据到 main.jsp 文件,如下所示:
dopo aver cliccato su "Seleziona sito":
Di seguito utilizzeremo HttpServletRequest del getParameterNames() Per leggere tutti i parametri del modulo, questo metodo può ottenere i nomi di tutte le variabili e restituisce un'enumerazione.
Una volta che abbiamo un Enumeration (enumerazione), possiamo chiamare il metodo hasMoreElements() per determinare se ci sono altri elementi e utilizzare il metodo nextElement() per ottenere il nome di ogni parametro.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"% <%@ page import="java.io.*,java.util.*" %> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>基础教程网(oldtoolbag.com)</title> </head> <body> <h1>读取所有表单参数</h1> <table width="100%" border="1" align="center"> <tr bgcolor="#949494"> <th>参数名</th><th>参数值</th> </tr> <% Enumeration paramNames = request.getParameterNames(); while(paramNames.hasMoreElements()) { String paramName = (String)paramNames.nextElement(); out.print("<tr><td>" + paramName + "</td>\n"); String paramValue = request.getParameter(paramName); out.println("<td> " + paramValue + "</td></tr>\n"); } %> </table> </body> </html>
以下是test.htm文件的内容:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>基础教程网(oldtoolbag.com)</title> </head> <body> <form action="main.jsp" method="POST" target="_blank"> <input type="checkbox" name="google" checked="checked" /> Google <input type="checkbox" name="w3codebox" /> 基础教程网 <input type="checkbox" name="taobao" checked="checked" /> 淘宝 <input type="submit" value="选择网站" /> </form> </body> </html>
现在我们通过浏览器访问 test.htm 文件提交数据,输出结果如下:
通过访问 http://localhost:8080/testjsp/test.html 提交表单数据到 main.jsp 文件,如下所示:
点击 "选择网站" 后:
你可以尝试使用以上的JSP代码读取其他对象,如文本框,单选按钮或下拉框等等其他形式的数据。