Submits the information from client to the server is very common. For example, registration, user input information in browser, and will transmit information to server after submit.
the client via a Web browser, enter the person's name in the form and after the contents of the data can be transmitted to the server. So you can use “request” built-in object to get information of submit.
The “request” object has five methods can get submit data by name. The submit data can use HTML form table to upload data, or use URL parameters to upload data.
1. public String getParameter(String key, String charsetName)
Returns a string containing the lone value of the specified parameter, or null if the parameter does not exist. The value of returns will be encoded by specified charset. You should use this method only when they are sure that there is only one value for the parameter, If the parameter has (or could have) multiple values please use “getParameterValues” method.
Parameters: key - the name of the parameter whose value is required. The “key” is case sensitive.
charsetName - specified charset name, the name of a supported charset, for example BIG-5, UTF-8, GBK…
Returns: a string containing the lone value of the specified parameter, or null if the parameter does not exist.
2. public String getParameter(String key)
Returns a string containing the lone value of the specified parameter, or null if the parameter does not exist. The value of returns will be encoded by default charset. The default charset please refer to section 2.1. You should use this method only when they are sure that there is only one value for the parameter, If the parameter has (or could have) multiple values please use “getParameterValues” method.
Parameters: key - the name of the parameter whose value is required. The “key” is case sensitive.
Returns: a string containing the lone value of the specified parameter, or null if the parameter does not exist.
3. public String[] getParameterValues(String key, String charsetName)
Returns the values of the specified parameter for the request as an array of strings, or null if the named parameter does not exist. This method would return the values of the specified query string or posted form as an array of strings. All values of returns will be encoded by specified charset.
Parameters: key - the name of the parameter whose value is required. The “key” is case sensitive.
charsetName - specified charset name, the name of a supported charset, for example BIG-5, UTF-8, GBK…
Returns: the values of the specified parameter for the request as an array of strings, or null if the named parameter does not exist.
4. public String[] getParameterValues(String key)
Returns the values of the specified parameter for the request as an array of strings, or null if the named parameter does not exist. This method would return the values of the specified query string or posted form as an array of strings. All values of returns will be encoded by default charset. The default charset please refer to section 2.1.
Parameters: key - the name of the parameter whose value is required. The “key” is case sensitive.
charsetName - specified charset name, the name of a supported charset, for example BIG-5, UTF-8, GBK…
Returns: the values of the specified parameter for the request as an array of strings, or null if the named parameter does not exist.
5. public Set<String> getParameterNameSet()
Returns: Returns the parameter names for this request as a set of strings, or an empty set if there are no parameters.
For example, “post.htm” is a static html file, you can use it to submit data to “post.dqm”. Source code:
|
1 2 3 4
5 |
<form method="POST" action="post.dqm?key2=kangaroo-egg"> <p>key1: <input type="text" name="key1" size="20"></p> <p>key1: <input type="text" name="key1" size="20"></p> <p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p> </form> |
The “post.dqm” dqm file can get data of submit. Source code:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<%@page import="java.util.*"%> <pre><%
//get all the parameter names of this request Set nameSet = request.getParameterNameSet(); if (nameSet.size() == 0) { out.println("No any parameters in current request"); } else { Iterator<String> nameItor = nameSet.iterator(); while (nameItor.hasNext()) { String tempName = nameItor.next(); out.println("------------------------------------------------------"); out.println("parameter name: " + tempName);
//use "getParameter" method out.println("current parameter's value: " + request.getParameter(tempName));
//use "getParameterValues" method String tempValues[] = request.getParameterValues(tempName); out.println("Total value of current parameter: " + tempValues.length); out.print("All values of current parameter: "); for (int i = 0; i < tempValues.length; i++) { out.print(tempValues[i] + ", "); } out.println(""); } }
%></pre> |
Visit post.htm and input “hello” and “world”, see picture 6-1-1.

Picture 6-1-1
Result:
|
------------------------------------------------------ parameter name: key1 current parameter's value: hello Total value of current parameter: 2 All values of current parameter: hello, world, ------------------------------------------------------ parameter name: B1 current parameter's value: Submit Total value of current parameter: 1 All values of current parameter: Submit, ------------------------------------------------------ parameter name: key2 current parameter's value: kangaroo-egg Total value of current parameter: 1 All values of current parameter: kangaroo-egg, |
From the results we can see, “getParameter” method only can get first value(source ode line 17), even “key1” parameter containing two values, but this method only can get it’s first value “hello”. The “getParameterValues” can get all values of the specified parameter(source code line 20).
|
Notice: Section2.3 and 2.5 introduced “maxPostLen” element, if submit data size over “maxPostLen” then server will prompt error information or close connection. |
If you want to save temporary data when rquest a dqm file, you can use the following two methods.
1. public void setAttribute(String name, Object value)
This method stores a temporary data in the request context. The temporary data effective scope only in the same request.
Parameters: name - a String specifying the name of the attribute.
value - a temporary data stored with the key.
2. public Object getAttribute(String name)
Returns the value of the named attribute of the request temporary data, or null if the temporary data does not exist
Parameters: name - the name of the attribute whose value is required.
Returns: Temporary data by name.
Actually in the same page you can use a variable to achieve store temporary data. But if need to insert other dynamic web page, use this method can easy to transfer data. For example, a “setAttribute.txt” file, in this file we set two data “name” and “age” (source code line 2 to 3), source code:
|
1 2 3 4 |
<% request.setAttribute("name", "kangaroo-egg"); request.setAttribute("age", "1"); %> |
Another dqm file “setAttribute.dqm”, it’s included “setAttribute.txt” file, and need to output data of set by “setAttribute.txt” (source code line 3 to 4). It’s source code:
|
1 2 3 4 |
<%@include file="setAttribute.txt"%> <pre> name: <%=request.getAttribute("name")%> age: <%=request.getAttribute("age")%></pre> |
Result:
name: kangaroo-egg age: 1 |
Sometimes the we need to get client visit information, for example client IP address, client version.
1. public String getRemoteAddr()
Returns the IP address of the agent that sent the request. Same as the CGI variable REMOTE_ADDR.
Returns: the IP address of the agent that sent the request.
2. public String getRemoteHost()
Returns the fully qualified host name of the agent that sent the request. Same as the CGI variable REMOTE_HOST.
Returns: the fully qualified host name of the agent.
3. public int getRemotePort()
Returns the port number of the agent that sent the request.
Returns: the port number of the agent that sent the request.
4. public String[] getAllHttpHead()
Gets the all header contents of this request.
Returns: the all header contents of this request as a array of strings.
5. public String getHeader(String name)
Gets the value of the requested header field of this request. The case of the header field name is ignored.
Parameters: name - the String containing the name of the requested header field.
Returns: the value of the requested header field, or null if not known.
For example, “clientInfo.dqm” dqm file, it will show client information of this request. Source code:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
17 |
<pre> Information of the client that sent current request
Client IP address: <%=request.getRemoteAddr()%> Client host name: <%=request.getRemoteHost()%> Client port number: <%=request.getRemotePort()%>
All header contents of current request:<% String[] allHttpHead = request.getAllHttpHead(); for (int i = 0; i < allHttpHead.length; i++) { out.println(allHttpHead[i]); } %>
The value of the "Accept-Language" field of current request: <%=request.getHeader("accept-language")%>
</pre> |
The results maybe not same under different environment. Our result is:
Information of the client that sent current request Client IP address: 127.0.0.1 Client host name: 127.0.0.1 Client port number: 3032 All header contents of current request:GET /us/clientInfo.dqm HTTP/1.1 Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */* Referer: http://127.0.0.1:8080/us/ Accept-Language: en-us Accept-Encoding: gzip, deflate User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; InfoPath.1) Host: 127.0.0.1:8080 Connection: Keep-Alive The value of the "Accept-Language" field of current request: en-us |
Sometimes we need to get server response information.
1. public String getLocalAddr()
Returns the server IP address on which this request was received.
Returns: the server IP address on which this request was received.
2. public String getLocalHost()
Returns the server host name on which this request was received.
Returns: the server host name.
3. public int getLocalPort()
Returns the port number on which this request was received. Same as the CGI variable SERVER_PORT.
Returns: the port number on which this request was received.
For example, dqm file “serverInfo.dqm”, it will show server information of this request. Source code:
|
1 2 3 4 5 6 7 8 |
<pre> Information of the server
Server IP address: <%=request.getLocalAddr()%> Server host name: <%=request.getLocalHost()%> Client port number: <%=request.getLocalPort()%>
</pre> |
Result:
Information of the server Server IP address: 127.0.0.1 Server host name: 127.0.0.1 Client port number: 8080 |
In section 5.6, we introduced how to save the cookie to the client, so this section will introduce how to read the cookie from client..
1. public String getCookieValue(String cookieName)
Returns the cookie’s value by specified cookieName.
Parameters: cookieName - the specified name of the cookie.
Returns: cookie’s value by specified cookieName or null if not found.
2. public DCookie[] getCookies()
Gets the array of cookies found in this request.
Returns: the array of cookies found in this request. If not found any cookie then return a empty array (array length is 0)
And how to use it we will introduce at chapter 7.
Sometimes we need know dqm file absolute path, url path, so this section we will introduce how to get these information.
1. public String getNowFolder()
Returns the absolute file pathname string of current dqm file.
Returns: The absolute pathname string of current dqm file, here not return null unless the system error.
2. public String getNowPath()
Returns the absolute pathname string of current dqm file.
Returns: The string form of this absolute pathname.
3. public File getNowFile()
Returns the absolute form of this abstract file pathname.
Equivalent to new File(this.getNowPath()).
Returns: the absolute form of this abstract file pathname
4. public String getWebPath()
Get current host home directory. Its value is equivalent “webPath” item, about “webPath” refer to section 2.3 and 2.5.
Returns: the absolute pathname string of current host home directory.
5. public String getNowUrlFolder()
Gets, from the first line of the HTTP request, the part of this request's URI that is to the left of any query string.
Returns: this request's URI
6. public String getNowUrlQuery()
Gets any query string that is part of the HTTP request URI. Same as the CGI variable QUERY_STRING. Please notice the query string will be decoded (refer to “urlEnc” item in section 2.1).
Returns: query string that is part of this request's URI, or null if it contains no query string
For example, “getDqmFileInfo.dqm” dqm file can get its own information. We put this dqm file under the “test_folder” folder. Source code:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<%@page import="java.util.Date"%>
<pre> Current host home directory: <%=request.getWebPath()%>
The absolute file pathname string of current dqm file: <%=request.getNowFolder()%> The absolute pathname string of current dqm file: <%=request.getNowPath()%> Current request's URI: <%=request.getNowUrlFolder()%> Query string that is part of this request's URI: <%=request.getNowUrlQuery()%>
Use "request.getNowFile()" method can get more information-------------------------- <%File file = request.getNowFile();%> Current dqm file name: <%=file.getName()%> Current dqm file size: <%=file.length()%> Current dqm file last modified: <%=new Date(file.lastModified())%> </pre> |
The results maybe not same under different environment. Our result is:
|
1 2 3 4
5 6 7 8 9 10 11 12 |
Current host home directory: C:\kgserver\webapp
The absolute file pathname string of current dqm file: C:\kgserver\webapp\us\test_folder The absolute pathname string of current dqm file: C:\kgserver\webapp\us\test_folder\getDqmFileInfo.dqm Current request's URI: /us/test_folder/ Query string that is part of this request's URI: null
Use "request.getNowFile()" method can get more information--------------------------
Current dqm file name: getDqmFileInfo.dqm Current dqm file size: 686 Current dqm file last modified: Tue Sep 11 16:29:50 GMT+08:00 2007 |
We set current host home directory to “C:\kgserver\webapp”, so dqm absolute pathname was included home directory (result line 3 and 4). The request not include any query string so return null (result line 6). Use “getNowFile” method we can get more dqm file information, example file size (result line 8 to 12).
7. public boolean isSSL()
Get the type of current connection. Because the server can simultaneously enable http and https service (refer to section 2.2), so user can visit resources through http or https service.
Returns: If user visit by https service then return true, else visit by http service return false.
This method is also very useful, for example there is a “login.dqm” dqm file under “hr_system” folder, it’s a simulation login page of the HR system, source code:
|
1
2 3 4 5
6 7 8 9 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24
25 26 27 28 29 30 31 32 33 34 35 36 |
<p align="center"><font color="#0000FF" size="7"><b>Welcome to the HR system</b></font></p> <form method="POST" action=""> <p align="center">Username: <input type="text" name="T1" size="20"></p> <p align="center">Password: <input type="text" name="T2" size="20"></p> <p align="center"><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p> </form>
<% //check the type of current connection (http or https) boolean isSSL = request.isSSL(); String bgColor, link, linkInfo; if (isSSL) { //If user visit by https service //change the background color to pale yellow bgColor = "#FFFF99"; //show http link url link = "http://localhost:8080" + request.getNowUrlFolder() + request.getNowFile().getName(); //prompt user can use http service linkInfo = "Normal connection"; } else { //If user visit by http service //change the background color to gray bgColor = "#C0C0C0"; //show https link url link = "https://localhost:8443" + request.getNowUrlFolder() + request.getNowFile().getName(); //prompt user can use https service linkInfo = "Secure connection"; }
%>
<table border="0" width="100%" id="table1" bgcolor="<%=bgColor%>"> <tr> <td> <p align="right"><a href="<%=link%>"><%=linkInfo%></a></td> </tr> </table> |
When user visit this dqm file by HTTP service, then page bottom will prompt can use secure connection to visit it. Please notice that bottom form’s background color is gray, it prompt current visit by HTTP service, and provide HTTPS link url. See picture 6-6-1.

Picture 6-6-1
When user visit this dqm file by HTTPS service, then page bottom will prompt can use normal connection to visit it. Please notice that bottom form’s background color is pale yellow, it prompt current visit by HTTPS service, and provide HTTP link url. See picture 6-6-2.

Picture 6-6-2
Source code line 12 will check the type of current connection. Source code line 16 and 24 is create HTTP and HTTPS url.
|
Notice: Only simultaneously enable HTTP and HTTPS service then can successfully execute this program. Please refer to section 2.2 to enable HTTP and HTTPS service. And HTTP service port must set be “8080”, HTTPS service port must set be “8443”. |
In section 6.1 we introduced how to get upload data, but it only can get TEXT data. So this section we will introduce multipart, use multipart we can upload binary file to server, for example we can upload picture, music files to server.
1. public DMpFormData.FormDataEntry getFormDataEntry(String key)
Returns a lone FormDataEntry(multipart object) by the specified parameter, or null if the parameter does not exist. You should use this method only when they are sure that there is only one FormDataEntry for the parameter, If the parameter has (or could have) multiple values(multipart object) please use “getFormDataEntryValues” method.
Parameters: key - the name of the parameter whose FormDataEntry is required. The “key” is case sensitive.
Returns: a lone FormDataEntry of the specified parameter, or null if the parameter does not exist. About FormDataEntry class we will introduce later.
2. public DMpFormData.FormDataEntry[] getFormDataEntryValues(String key)
Returns the multipart objects of the specified parameter for the request as an array of FormDataEntry class, or null if the named parameter does not exist. This method would return the multipart objects of the specified posted form as an array of FormDataEntry class.
Parameters: key - the name of the parameter whose FormDataEntry is required. The “key” is case sensitive.
Returns: the FormDataEntry objects of the specified parameter for the request as an array of FormDataEntry class, or null if the named parameter does not exist.
3. public Set<String> getFormDataNameSet()
Returns: the multipart parameter names for this request as a set of strings, or null if there are no multipart parameters.
4. public int getContentLength()
Returns: the size of the request entity data. Same as the CGI variable CONTENT_LENGTH. If use multipart form to upload data then return the size of multipart post content. If use ordinary form to upload data then return this size of ordinary post content. The size of URL query string not be include.
Next we will talk about the “FormDataEntry” class, it includes multipart data and information. The “FormDataEntry” is inter class of “com.kangaroo_egg.dqm.DMpFormData”, now we introducing methods of this class.
1. public String getValue(String charsetName) throws
UnsupportedEncodingException
Returns a string containing the value of the current multipart object. The value of returns will be encoded by specified charset. This method can be applied to get multipart text data, if current multipart object’s value is binary data then this method also encoded binary data to text format.
Parameters: charsetName - specified charset name, the name of a supported charset, for example BIG-5, UTF-8, GBK…
Returns: a string containing the value of the current multipart object.
Throws: UnsupportedEncodingException - if charsetName is not supported
2. public String getValue() throws UnsupportedEncodingException
Returns a string containing the value of the current multipart object. The value of returns will be encoded by default charset, the default charset please refer to section 2.1. This method can be applied to get multipart text data, if current multipart object’s value is binary data then this method also encoded binary data to text format.
Returns: a string containing the value of the current multipart object.
Throws: UnsupportedEncodingException - if default charset is not supported
3. public int getContentSize()
Returns: the size of current multipart object’s data. This value is the users upload data size.
4. public boolean isFile()
Judgment of the current multipart object’s type is file or not, because the multipart form can upload TEXT data too.
Returns: If multipart object’s data is file of user uploaded then return true, else return false.
5. public String getAbsolutePath(String charsetName) throws
UnsupportedEncodingException
If current multipart object’s type is file then returns it’s absolute pathname. The value of returns will be encoded by specified charset.
Parameters: charsetName - specified charset name, the name of a supported charset, for example BIG-5, UTF-8, GBK…
Returns: If current multipart object’s type is file then returns it’s absolute pathname. If current multipart object’s type is not file then returnsnull. If current multipart object’s type is file but user not upload any file then returns empty string, user can use “isFile()” method to judge current multipart object’s type first.
Throws: UnsupportedEncodingException - if charsetName is not supported.
6. public String getAbsolutePath() throws UnsupportedEncodingException
If current multipart object’s type is file then returns it’s absolute pathname. The value of returns will be encoded by default charset, the default charset please refer to section 2.1.
Returns: If current multipart object’s type is file then returns it’s absolute pathname. If current multipart object’s type is not file then returns null. If current multipart object’s type is file but user not upload any file then returns empty string, user can use “isFile()” method to judge current multipart object’s type first.
Throws: UnsupportedEncodingException - if default charset is not supported
7. public String getFileName(String charsetName) throws
UnsupportedEncodingException
If current multipart object’s type is file then returns filename of uploaded. The value of returns will be encoded by specified charset.
Parameters: charsetName - specified charset name, the name of a supported charset, for example BIG-5, UTF-8, GBK…
Returns: If current multipart object’s type is file then returns it’s filename. If current multipart object’s type is not file then returns null. If current multipart object’s type is file but user not upload any file then returns empty string, user can use “isFile()” method to judge current multipart object’s type first.
Throws: UnsupportedEncodingException - if charsetName is not supported.
8. public String getFileName() throws UnsupportedEncodingException
If current multipart object’s type is file then returns filename of uploaded. The value of returns will be encoded by default charset, the default charset please refer to section 2.1.
Returns: If current multipart object’s type is file then returns it’s filename. If current multipart object’s type is not file then returns null. If current multipart object’s type is file but user not upload any file then returns empty string, user can use “isFile()” method to judge current multipart object’s type first.
Throws: UnsupportedEncodingException - if default charset is not supported
9. public String getFileExtName(String charsetName) throws
UnsupportedEncodingException
If current multipart object’s type is file then returns Ext filename of uploaded. The value of returns will be encoded by specified charset.
Parameters: charsetName - specified charset name, the name of a supported charset, for example BIG-5, UTF-8, GBK…
Returns: If current multipart object’s type is file then returns it’s Ext filename. If current multipart object’s type is not file then returns null. If current multipart object’s type is file but user not upload any file then returns empty string, user can use “isFile()” method to judge current multipart object’s type first.
Throws: UnsupportedEncodingException - if charsetName is not supported.
10. public String getFileExtName() throws UnsupportedEncodingException
If current multipart object’s type is file then returns Ext filename of uploaded. The value of returns will be encoded by default charset, the default charset please refer to section 2.1.
Returns: If current multipart object’s type is file then returns it’s Ext filename. If current multipart object’s type is not file then returns null. If current multipart object’s type is file but user not upload any file then returns empty string, user can use “isFile()” method to judge current multipart object’s type first.
Throws: UnsupportedEncodingException - if default charset is not supported.
11. public String getName()
Get parameter name of current multipart object. The returns value is equal to “request. getFormDataEntry(String key)” method’s key parameter.
Returns: parameter name of current multipart object.
12. public String getContentType()
Get current multipart object’s MIME type, This type determined according to the upload file's type.
Returns: current multipart object’s MIME type. For example upload a picture file then returns MIME type is “image/pjpeg”, upload a text file then returns MIME type is “text/plain”. If current multipart object’s type is not file then returns null. If current multipart object’s type is file but user not upload any file then returns “application/octet-stream”, user can use “isFile()” method to judge current multipart object’s type first.
13. public String getContentDisposition()
Get current multipart object’s disposition type.
Returns: current multipart object’s disposition type. Usually it will returns “form-data”.
14. public void saveToFile(String fileName, boolean append) throws IOException
Writing current multipart object’s data to a File.
Parameters: fileName - the system-dependent file name. It can use relative path or absolute path. Relative path is relative to the current dqm file's path. For example: “../xxx.txt”, “test/xxx.txt”. Absolute path’s example is “c:\\xxx.txt”, “/xxx.txt”.
append - if true, then bytes will be written to the end of the file rather than the beginning
Throws: IOException - if an I/O error occurs.
15. public boolean saveToFile(File file, boolean overWrite, boolean append)
Writing current multipart object’s data to specified File.
Parameters: file - the file to be opened for writing
overwrite - If the target file exist, whether to allow it to overwrote
append - if true, then bytes will be written to the end of the file rather than the beginning
Returns: Is save the file successful.
If the target file existence:
If “overwrite” value was “false”, this meaning is not allow to revise the file which already existed, so like this status “append” value will be ignored and current method will not to do anything, returns “false” to express the save file defeat.
If “overwrite” value was “true”, this meaning is allow to revise the file which already existed, so need to judge based on the “append” value. If append or overwrite file successfully then returns “true”, else returns “false”.
If the target file does not exist:
This status the “append” value will be ignored, so need to judge based on the “append” value. If append or overwrite file successfully then returns “true”, else returns “false”.
|
Notice: The “saveToFile” method can use relative path, the relative path is relative to the kangaroo-egg installation directory. For example: <%saveToFile(new File("save.txt"), true, false);%> Then “save.txt” file will save in the installation directory. If you need to save file in directory of current dqm file, then you can use the method of section 6.6, example: <%saveToFile(new File(request.getNowFolder + "/save.txt"), true, false);%> |
16. public int read()
Reads a byte of data from current multipart object’s data. You can use current method in conjunction with “OutputStream. write(int b)” method.
Returns: the next byte of current multipart object’s data, or -1 if the end of current multipart object’s data is reached.
17. public void resetRead()
Reset “read()” method. After execute this method, “read()” method will from current multipart object data’s first byte to read.
Next we will show a example. First we create a static html file “upfile.htm”, it can upload data to dqm file. Source code:
|
1 2 3 4 5
6 7 8 9 10 11 12 13
14 |
<form method="POST" enctype="multipart/form-data" action="upfile.dqm"> <p>Self introduction:</p> <p><textarea rows="7" name="intro" cols="31"></textarea></p> <p> Gender: <input type="radio" value="male" checked name="gender">male <input type="radio" name="gender" value="female">female </p> <p> Hobbies: <input type="checkbox" name="favorite" value="football">football <input type="checkbox" name="favorite" value="cartoon">cartoon <input type="checkbox" name="favorite" value="Internet">Internet</p>
<input type="FILE" name="upfile" size="50"><br> <p><input type="submit" value="Submit" name="button"><input type="reset" value="Reset" name="B2"></p> </form> |
Please notice string of enctype="multipart/form-data" at source code line 1, it meaning is upload data use multipart. And string of action="upfile.dqm" at source code line 1, so the “upfile.dqm” will process data of uploaded, it’s source code:
|
1 2 3 4 5 6 7 8 9 10 11 12
13 14 15 16 17 18 19 20 21 22 23 24
25 26
27 28
29 30 31
32 33
34 35 36 37 38
39 40 41 42 43 44 45 46 47 48 49 50 |
<%@page import="java.util.*"%>
<pre>
Get the size of current request entity data: <%=request.getContentLength()%>
<% //read all of the multipart parameter names for this request Set nameSet = request.getFormDataNameSet(); %>
How many multiparts of current request: <%if (nameSet != null) out.print(nameSet.size()); else out.print("No multiparts");%>
<% if (nameSet != null) { Iterator<String> nameItor = nameSet.iterator(); while (nameItor.hasNext()) { String tempName = nameItor.next(); out.println("------------------------------------------------------");
//read formDataEntry by the specified parameter DMpFormData.FormDataEntry tempFDE = request.getFormDataEntry(tempName);
out.println("The parameter name of current multipart object: " + tempFDE.getName()); //it's value is equal to current tempName's value
out.println("The MIME type of current multipart object: " + tempFDE.getContentType());
out.println("The disposition type current multipart object: " + tempFDE.getContentDisposition());
//output the size of current multipart object's data out.println("The size of current multipart object's data: " + tempFDE.getContentSize());
if (!tempFDE.isFile()) { //if current multipart object's type is not file, then output it's content out.println("Current multipart object's content: " + tempFDE.getValue()); } else { //if current multipart object's type is file out.println("Current multipart object's type is file."); out.println("The absolute pathname of uploaded file: " + tempFDE.getAbsolutePath()); out.println("The filename of uploaded file: " + tempFDE.getFileName()); out.println("The ext filename of uploaded file: " + tempFDE.getFileExtName()); out.println("Save the uploaded File."); if (tempFDE.getContentSize() > 0) { //If has uploaded file tempFDE.saveToFile("uploaded_file.txt", false); } }
} } %> </pre> |
We start to upload the data, the operation like picture 6-7-1.

Picture 6-7-1
The result is:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
Get the size of current request entity data: 721
How many multiparts of current request: 5
------------------------------------------------------ The parameter name of current multipart object: upfile The MIME type of current multipart object: text/plain The disposition type current multipart object: form-data The size of current multipart object's data: 8 Current multipart object's type is file. The absolute pathname of uploaded file: C:\myself.txt The filename of uploaded file: myself.txt The ext filename of uploaded file: .txt Save the uploaded File. ------------------------------------------------------ The parameter name of current multipart object: gender The MIME type of current multipart object: null The disposition type current multipart object: form-data The size of current multipart object's data: 4 Current multipart object's content: male ------------------------------------------------------ The parameter name of current multipart object: intro The MIME type of current multipart object: null The disposition type current multipart object: form-data The size of current multipart object's data: 17 Current multipart object's content: I'm kangaroo-egg. ------------------------------------------------------ The parameter name of current multipart object: favorite The MIME type of current multipart object: null The disposition type current multipart object: form-data The size of current multipart object's data: 7 Current multipart object's content: cartoon ------------------------------------------------------ The parameter name of current multipart object: button The MIME type of current multipart object: null The disposition type current multipart object: form-data The size of current multipart object's data: 6 Current multipart object's content: Submit |
We have to analyze the results of the above output, this time we upload five multipart objects. We can see the “upfile” multipart object is file type, and if user upload the file then will show it’s filename, pathname (Source code line 38 to 40). The size of “upfile” multipart object is equal to size of uploaded file. Last save the uploaded file at current folder, file name is “uploaded_file.txt” (source code line 42 to 44).
We look at an interesting phenomenon, the result 1st line displayed that current upload the byte count is 721. But five multipart objects data size plus together is 42 bytes. (result line 11, 21, 27, 33 and 39). Why did they not equal? Because request entity data includes the description information, but kangaroo-egg server was processed(deleted) the description information.
Another interesting phenomenon, seen picture 6-7-1, in “hobbies” item we chose “carton” and “internet” two options. But results only show "carton", because we used “getFormDataEntry” method (source code line 22). Ok now we create two new files “upfile1.htm” and “upfile1.dqm”, it’s use “getFormDataEntryValues” method to read multipart object, upfile1.htm soure code:
|
1 2 3 4 5 6 7 8 9 10 11 |
<form method="POST" enctype="multipart/form-data" action="upfile1.dqm"> <p> Hobbies: <input type="checkbox" name="favorite" value="football">football <input type="checkbox" name="favorite" value="cartoon">cartoon <input type="checkbox" name="favorite" value="Internet">Internet </p> <p> <input type="submit" value="Submit" name="button"> <input type="reset" value="Reset" name="B2"> </p> </form> |
Please notice string of enctype="multipart/form-data" at “upfile1.htm” source code line 1, it meaning is upload data use multipart. And string of action="upfile1.dqm" at “upfile1.htm” source code line 1, so the “upfile1.dqm” will process data of uploaded, it’s source code:
|
1 2 3 4 5 6
7 8 9 10 11 12 13 14 15 |
<%@page import="java.util.*"%>
<pre>
<% DMpFormData.FormDataEntry[] tempFDE = request.getFormDataEntryValues("favorite"); if (tempFDE != null) { out.println("Item of hobbies:"); for (int i = 0; i < tempFDE.length; i++) { out.println("Content: " + tempFDE[i].getValue()); } } %>
</pre> |
This time we selected “football” and “Internet”, seen picture 6-7-2.

Picture 6-7-2
The result:
|
Item of hobbies: Content: football Content: Internet |
You can seen from the result that “football” and “Internet” all displayed (upfile1.dqm source code line 6).
|
Notice: Section2.3 and 2.5 introduced “maxPostLen” element, if submit data size over “maxPostLen” then server will prompt error information or close connection. |