Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@MultipartConfig override in web.xml

So I have this servlet :

    @WebServlet(name = "StudentRegistrationUsn", urlPatterns = {"/university/student/registration"})
@MultipartConfig(maxFileSize = 10*1024*1024,maxRequestSize = 20*1024*1024,fileSizeThreshold = 5*1024*1024)
public class ActionRegistrationServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //handle file upload
    }

Everything works fine, files are uploading. Then I try to override the fileSize and Threshold in web.xml:

    <servlet>
        <servlet-name>StudentRegistrationUsn</servlet-name>
        <multipart-config>
            <max-file-size>10485760</max-file-size>
            <max-request-size>20971520</max-request-size>
            <file-size-threshold>5242880</file-size-threshold>
        </multipart-config>
    </servlet>

When I do that, tomcat crashes and whenever I try to access that servlet it gives the following exception:

The server encountered an internal error that prevented it from fulfilling this request.

exception

java.lang.NullPointerException
    sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1629)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:461)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1813)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    java.lang.Thread.run(Thread.java:722)
like image 998
kosta Avatar asked Oct 02 '13 19:10

kosta


People also ask

What is fileSizeThreshold?

fileSizeThreshold: The file size in bytes after which the file will be temporarily stored on disk. The default size is 0 bytes. MaxFileSize: The maximum size allowed for uploaded files, in bytes.

What is @MultipartConfig?

annotation. MultipartConfig , is used to indicate that the servlet on which it is declared expects requests to be made using the multipart/form-data MIME type. Servlets that are annotated with @MultipartConfig can retrieve the Part components of a given multipart/form-data request by calling the request.


1 Answers

The <servlet-class> is missing in web.xml. This exception is basically caused because the name of the to-be-loaded class is null.

In fact, you need to redefine everything, including the URL patterns.

<servlet>
    <servlet-name>StudentRegistrationUsn</servlet-name>
    <servlet-class>com.example.StudentRegistrationUsn</servlet-class>
    <multipart-config>
        <max-file-size>10485760</max-file-size>
        <max-request-size>20971520</max-request-size>
        <file-size-threshold>5242880</file-size-threshold>
    </multipart-config>
</servlet>
<servlet-mapping>
    <servlet-name>StudentRegistrationUsn</servlet-name>
    <url-pattern>/university/student/registration</url-pattern>
</servlet-mapping>
like image 110
BalusC Avatar answered Oct 02 '22 03:10

BalusC