JSP and upload encapsulation
I. Introduction to JSP
1.jsp is the Java Server Pages enables dynamic generation of HTML code in a page using Java as a scripting language
2.jsp First and HTML differences, HTML can be placed directly in the browser to execute, but the JSP must start Tomcat to see the effect
3. Components: 1. Static content; 2. directives; 3. Expressions; 4. Scripts; 5. Statements; 6. Notes
3.1 Notes: JSP has HTML-used annotations, Java-used annotations, and its own comments
The first: As with HTML annotations, this comment is the first one that can be seen on the client
The second JSP comment, which is not visible at the client, is erased at compile time <%--Comment--
The third is standard Java annotations, which can only be written in the JSP script//annotation; /* Comment */
3.2 Instructions: JSP page can be added at the top of the current page configuration and other information called JSP directives
Page directive: The configuration information of the current pages, most commonly used
Include: In one page, embed another file in it
Taglib: Introducing Tag Libraries
3.3 Script: <%--jsp script--%> can write any Java code, but cannot define methods, global variables. All JSP scripts are mixed with HTML code into the _jspservice method.
3.4 Static content: All written in the JSP file is not wrapped by any JSP tag, is static content, that is, all the HTML code, is static content
3.5 declaration: Used to write custom methods, member variables of the
3.6 expression: is in HTML code, in order to use the global variables defined in the declaration, or the local variables defined in the script exists that the expression is not complex logic, only values can be used
Two. Upload the package
public class Smartupload {
Private HttpServlet servlet;//
Private servletfileupload sfu;//for uploading global variables
Private file uploaddir;//The folder used to create the uploaded file
Private hashmap<string,string> parameters;//used to store normal form data
Private hashmap<string,file> files;//used to store file names and files for uploading files
Public Smartupload (HttpServlet httpservlet) {
This.servlet = HttpServlet; Constructs a method for manipulating a Servlet object that uses this wheel
Inintdir ();//Initialize the upload folder
SFU = new Servletfileupload ();//
Sfu.setfileitemfactory (New Diskfileitemfactory ());// initialize Servletfileupload
Parameters = new hashmap<> ();//Initialize Object
Files = new hashmap<> (); Initialize Object }
private void Initdir () {
String Uploadpath = Servlet.getservletcontext (). Getrealpath ("") +file.separator+ "upload";//stitching Upload file path
Uploaddir = new File (Uploadpath); Creating a File object by stitching paths
if (!uploaddir.exists ()) {//Judgment folder is present
Uploaddir.mkdirs (); } //Does not exist create folder
public void Parse (HttpServletRequest request) {//Parse request
list<fileitem> Fileitems = sfu.parserequest (request);//parse request into List
if (fileitems! = null) {//Judgment NOT NULL
for (Fileitem fileitem:fileitems) {//Traverse list
if (Fileitem.isformfield ()) {//To determine whether the form data
String key = Fileitem.getfieldname ();//Get Key
String value = fileitem.getstring ("Utf-8");//Get value
Parameter.put (Key,value);//Deposit in HashMap
}else {//Not form data
String filename = fileitem.getname ();//Get file name
if (Stringutils.isempty (fileName)) {//reference is a package under Apache
Continue;}
String realname = testuuid.handfilename (filename);//use UUID to generate a new file name
File File = new (uploaddir,realname);//Create files by folder and file name
Fileitem.write (file);//write files
Files.put (filename,file);//Save file name and file object HashMap
public string GetParameter (string key) {
return Parameter.get (key);}
Public map<string,string> Getparametermap () {
return parameters;}
Public map<string,file> Getuploadfiles () {
return files;}
Three. Request Domain
put values in the request domain, values are stored as Key-value , andkey can only be string,value any object
Stored value: Request.setattribute ("msg", msg);//key-value pair form
Value: Request.getattribute ("MSG");//value by key
Java_day25_uuid, package upload, JSP