web.xml配置
servlet
<servlet>
<servlet-name>ServletDemo1</servlet-name>
<servlet-class>com.yuan.servlet.ServletDemo1</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>name</param-name>
<param-value>yuan</param-value>
</init-param>
<multipart-config>
<max-file-size>5242880</max-file-size>
<max-request-size>418018841</max-request-size>
<file-size-threshold>1048576</file-size-threshold>
</multipart-config>
</servlet>
<servlet-name> servlet注册名
<servlet-class> servlet注册类
<load-on-startup> 启动加载servlet顺序,0表示不加载
<init-param> servlet初始化参数
<multipart-config> 文件上传相关
servlet-mapping
<servlet-mapping>
<servlet-name>ServletDemo1</servlet-name>
<url-pattern>/ServletDemo1</url-pattern>
</servlet-mapping>
<url-pattern> 映射servlet的url
context-param
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/test</param-value>
</context-param>
<param-name> 参数名
<param-value> 参数值
附录
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<context-param>
<param-name></param-name>
<param-value></param-value>
</context-param>
<listener>
<listener-class></listener-class>
</listener>
<servlet>
<servlet-name></servlet-name>
<servlet-class></servlet-class>
<init-param>
<param-name></param-name>
<param-value></param-value>
</init-param>
<load-on-startup></load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name></servlet-name>
<url-pattern></url-pattern>
</servlet-mapping>
<filter>
<filter-name></filter-name>
<filter-class></filter-class>
<init-param>
<param-name></param-name>
<param-value></param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name></filter-name>
<url-pattern></url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file></welcome-file>
</welcome-file-list>
</web-app>
注解配置
@WebServlet()
@WebServlet(name="fileServlet",urlPatterns="/file/*")
name
urlPatterns
@MultipartConfig()
@MultipartConfig(location="E:/jsp",maxFileSize=5*1024*1024)
fileSizeThreshold 整数值设置,默认值为0,若上传文件的大小超过了这个值,就会先写入缓存文件
location 字符串设置,默认值为空字符串。如果设置这个属性,缓存文件就是写到制定目录
maxFileSize 限制文件上传大小。默认值为-1L,表示不限制大小
maxRequestSize 限制multipart/form-data请求格式,默认值为-1L,表示不限制个数
注意
查看servlet的版本,打开servlet-api.jar中META-INF/MAINMEFT.MF文件,如下:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.9
Created-By: 1.7.0_80-b15 (Oracle Corporation)
X-Compile-Source-JDK: 1.7
X-Compile-Target-JDK: 1.7
Name: javax/servlet/
Specification-Title: Java API for Servlets
Specification-Version: 3.1
Specification-Vendor: Sun Microsystems, Inc.
Implementation-Title: javax.servlet
Implementation-Version: 3.1.FR
Implementation-Vendor: Apache Software Foundation