In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use JSP". In daily operation, I believe many people have doubts about how to use JSP. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use JSP". Next, please follow the editor to study!
JSP, whose full name is Java Server Pages, is a dynamic web page development technology. It uses JSP tags to insert Java code into HTML pages. The tag usually ends with, and JSP is a Java servlet that is mainly used to implement the user interface part of a Java web application. Web developers write JSP by combining HTML code, XHTML code, XML elements, and embedded JSP actions and commands.
Create a file upload form
The following HTML code creates a file upload form. The following points need to be noted:
1. The form method property should be set to the POST method, and the GET method cannot be used.
two。 The form enctype property should be set to multipart/form-data.
3. The form action property should be set to handle Servlet files uploaded by files on the back-end server. The following example uses UploadServlet
4.Servlet to upload files. To upload a single file, you should use a single tag with the attribute type= "file". To allow multiple file uploads, include multiple name tags with different input attribute values. Enter a value for the label with a different name attribute. The browser associates a browse button for each input tag.
The upload.jsp file code is as follows:
"java" contentType= "text/html; charset=UTF-8" pageEncoding= "UTF-8"% > nbsp;html PUBLIC "- / / W3C//DTD HTML 4.01 Transitional//EN"http://www.w3.org/TR/html4/loose.dtd">"Content-Type" content=" text/html; charset=UTF-8 "> file upload instance-rookie tutorial" post "action=" / TomcatTest/UploadServlet "enctype=" multipart/form-data "> Select a file: type=" file "name=" uploadFile "/ >
Type= "submit" value= "upload" / > write background Servlet
The following is the source code of UploadServlet, which is the same as dealing with file upload. Before that, let's make sure that the dependency package has been introduced into the WEB-INF/lib directory of the project: the source code of commons-fileupload-1.3.2.jar,commons-io-2.5.jar UploadServlet is as follows:
Package com.runoob.test;import java.io.File;import java.io.IOException;import java.io.PrintWriter;import java.util.List;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.fileupload.FileItem;import org.apache.commons.fileupload.disk.DiskFileItemFactory;import org.apache.commons.fileupload.servlet.ServletFileUpload / * Servlet implementation class UploadServlet*/// if web.xml is not configured, you can use the following code / / @ WebServlet ("/ UploadServlet") public class UploadServlet extends HttpServlet {private static final long serialVersionUID = 1L; / / upload file storage directory private static final String UPLOAD_DIRECTORY = "upload"; / / upload configuration private static final int MEMORY_THRESHOLD = 1024 * 1024 * 3; / / 3MB private static final int MAX_FILE_SIZE = 1024 * 1024 * 40 / / 40MB private static final int MAX_REQUEST_SIZE = 1024 * 1024 * 50; / / 50MB / * upload data and save files * / protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {/ / check whether multimedia upload if (! ServletFileUpload.isMultipartContent (request)) {/ / if not, stop PrintWriter writer = response.getWriter () Writer.println ("Error: the form must contain enctype=multipart/form-data"); writer.flush (); return;} / / configure the upload parameter DiskFileItemFactory factory = new DiskFileItemFactory (); / / set the memory threshold-after which a temporary file will be generated and stored in the temporary directory factory.setSizeThreshold (MEMORY_THRESHOLD) / / set the temporary storage directory factory.setRepository (new File (System.getProperty ("java.io.tmpdir")); ServletFileUpload upload = new ServletFileUpload (factory); / / set the maximum file upload value upload.setFileSizeMax (MAX_FILE_SIZE); / / set the maximum requested value (including file and form data) upload.setSizeMax (MAX_REQUEST_SIZE) / / Chinese processing upload.setHeaderEncoding ("UTF-8"); / / construct a temporary path to store uploaded files / / this path is relative to the currently applied directory String uploadPath = getServletContext () .getRealPath ("/") + File.separator + UPLOAD_DIRECTORY; / / create File uploadDir = new File (uploadPath) if the directory does not exist If (! uploadDir.exists ()) {uploadDir.mkdir ();} try {/ / parse the requested content extraction file data @ SuppressWarnings ("unchecked") List formItems = upload.parseRequest (request) If (formItems! = null & & formItems.size () > 0) {/ iteratively represents single data for (FileItem item: formItems) {/ / handles fields that are not in the form if (! item.isFormField ()) {String fileName = new File (item.getName ()) .getName () String filePath = uploadPath + File.separator + fileName; File storeFile = new File (filePath); / / upload path of the output file in the console System.out.println (filePath); / / Save the file to the hard disk item.write (storeFile) Request.setAttribute ("message", "File uploaded successfully!");} catch (Exception ex) {request.setAttribute ("message", "error message:" + ex.getMessage ()) Jump to message.jsp getServletContext () .getRequestDispatcher ("/ message.jsp") .forward (request, response);}}
The message.jsp file code is as follows:
"java" contentType= "text/html; charset=UTF-8" pageEncoding= "UTF-8"% > nbsp;html PUBLIC "- / / W3C//DTD HTML 4.01 Transitional//EN"http://www.w3.org/TR/html4/loose.dtd">"Content-Type" content=" text/html; charset=UTF-8 "> File upload result ${message} compile and run Servlet
Compile the above Servlet UploadServlet and create the required entries in the web.xml file, as follows:
"http://www.w3.org/2001/XMLSchema-instance" xmlns=" http://java.sun.com/xml/ns/javaee" xmlns:web= "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation=" http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id= "WebApp _ ID "version=" 2.5" > UploadServlet UploadServlet com.runoob.test.UploadServlet UploadServlet / TomcatTest/UploadServlet so far The study on "how to use JSP" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.