Instance: the text box for dynamically adding and deleting uploaded files
<% @ Page Language ="Java"Import ="Java. util .*"Pageencoding =UTF-8"%>
<%
String Path = request. getcontextpath ();
String basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/";
%>
Upload. jsp
<SCRIPT type ="Text/JavaScript">
FunctionAddFile (){
VaRFiles = Document. getelementbyid ("Files ");
VaRInput = Document. createelement ("input ");
Input. Name = "file ";
Input. type = "file ";
VaRDelbutton = Document. createelement ("input ");
Delbutton. type = "button ";
Delbutton. value = "delete ";
Delbutton. onclick =FunctionDel (){
This. Parentnode. parentnode. removechild (This. Parentnode );
}
VaRDIV = Document. createelement ("Div ");
Div. appendchild (input );
Div. appendchild (delbutton );
Files. appendchild (DIV );
}
</SCRIPT>
</Head>
<Body>
<Form action ="$ {Pagecontext. Request. contextpath}/Servlet/uploadservlet"Enctype ="Multipart/form-Data"Method ="Post">
Upload user <input type ="Text"Name ="Username"/> <Br/>
File 1 <input type ="File"Name ="File1"/> <Br/>
File 2 <input type ="File"Name ="File2"/> <Br/>
<Input type ="Submit"Value ="Submit"/> <Br/>
</Form>
<Br/> <br> <HR/>
CommonsFileuploadDemo: <br/>
<Form action ="$ {Pagecontext. Request. contextpath}/Servlet/uploadservlet2"Enctype ="Multipart/form-Data"Method ="Post">
Upload user <input type ="Text"Name ="Username"/> <Br/>
File 1 <input type ="File"Name ="File1"/> <Br/>
File 2 <input type ="File"Name ="File2"/> <Br/>
<Input type ="Submit"Value ="Submit"/> <Br/>
</Form>
<HR/>
<Form action ="$ {Pagecontext. Request. contextpath}/Servlet/uploadservlet2"Enctype ="Multipart/form-Data"Method ="Post">
<Table>
<Tr>
<TD>
User name:
</TD>
<TD>
<Input type ="Text"Name ="Username">
</TD>
</Tr>
<Tr>
<TD> </TD>
<TD>
<Input type ="Button"Value ="Add file"Onclick = "AddFile ()">
</TD>
</Tr>
<Tr>
<TD> </TD>
<TD>
<Div id ="Files"> </Div>
</TD>
</Tr>
<Tr>
<TD> </TD>
<TD>
<Input type ="Submit"Value ="Upload files">
</TD>
</Tr>
</Table>
</Form>
</Body>
</Html>
Uploadservlet. Java
Public voidDopost (httpservletrequest request, httpservletresponseresponse)
ThrowsServletexception, ioexception {
Inputstream is = request. getinputstream ();
Byte[] Buff =New byte[1024];
IntLen = 0;
While(LEN = is. Read (buff)> 0 ){
System.Out. Println (NewString (buff, 0, Len ));
}
}