前段时间写的上传文件的例子,并没有考虑到当文件上传时候加入其它信息的情况.
如果要是在页面中加入其它信息像用户名,性别等等..其它信息
要是在处理页面或者servlet中用request进行获得的时候将会
获得时空值,这时因为设置了
ENCTYPE="multipart/form-data 而导致的,至于本质原因应
该和html协议本身时有关的吧!
处理方法cos包中自带了获得方法
multi.getParameter("userName");
看似简单的方法实现起来还真不简单
1. 看了代码发现就是获得的方法不同了
multi.getParameter("userName");
的实现!
- if (request.getQueryString() != null) {
- // Let HttpUtils create a name->String[] structure
- Hashtable queryParameters =
- HttpUtils.parseQueryString(request.getQueryString());
- // For our own use, name it a name->Vector structure
- Enumeration queryParameterNames = queryParameters.keys();
- while (queryParameterNames.hasMoreElements()) {
- Object paramName = queryParameterNames.nextElement();
- String[] values = (String[])queryParameters.get(paramName);
- Vector newValues = new Vector();
- for (int i = 0; i < values.length; i++) {
- newValues.add(values[i]);
- }
- parameters.put(paramName, newValues);
- }
- }
public String getQueryString()
-
Returns the query string that is contained in the request URL after the path. This method returns
null
if the URL does not have a query string. Same as the value of the CGI variable QUERY_STRING.
getQueryString 应该似比较原始的方法,request.getparameter(),request.getparameterNames(),
request.getparameterValues()应该都是从他扩展而来的!
parseQueryString
public static Hashtable parseQueryString(String s)
- Deprecated.
-
Parses a query string passed from the client to the server and builds a
HashTable
object with key-value pairs. The query string should be in the form of a string packaged by the GET or POST method, that is, it should have key-value pairs in the form key=value, with each pair separated from the next by a & character.A key can appear more than once in the query string with different values. However, the key appears only once in the hashtable, with its value being an array of strings containing the multiple values sent by the query string.
The keys and values in the hashtable are stored in their decoded form, so any + characters are converted to spaces, and characters sent in hexadecimal notation (like %xx) are converted to ASCII characters.
-
-
Parameters:
-
s
- a string containing the query to be parsed
Returns:
-
a
HashTable
object built from the parsed key-value pairs
Throws:
-
IllegalArgumentException
- if the query string is invalid
-
2.第一步只是为了将http协议携带的信息进行解析并重新封装,这步是留下的调用接口方法
- public String getParameter(String name) {
- try {
- Vector values = (Vector)parameters.get(name);
- if (values == null || values.size() == 0) {
- return null;
- }
- String value = (String)values.elementAt(values.size() - 1);
- return value;
- }
- catch (Exception e) {
- return null;
- }
- }
3.经过测试
直接通过:
String userName1=request.getParameter("userName");
String userName2=request.getQueryString();
获得的话值是为空的!
只有通过multi.getParameter("userName");才可以!