10

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)
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
kosta
  • 296
  • 2
  • 6
  • 15

1 Answers1

24

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>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    even when using servlet 3.0 I have to redefine everything in web.xml ? – kosta Oct 02 '13 at 19:53
  • 5
    Yes. You've completely overriden the `@WebServlet` by redefining exactly the same servlet name in `web.xml`. You should then also take the remainder in account. The alternative would be to just directly edit the `@MultipartConfig` on the class. You choose: annotations or XML. – BalusC Oct 02 '13 at 19:54