Change Tomcat Default Port

1. Overview

By default, Apache Tomcat runs on the port 8080. However, we can change the port if needed, mostly, when another application is already using this port.

In this short article, we’ll discuss how to change the Tomcat port.

2. Change Port

First, we need to navigate to the Conf folder inside the Tomcat root folder. Then, open the server.xml file.

In the server.xml file, we’ll find the port configuration similar to the below:

    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

Now, let’s change the connector port to 9090, which allows the Tomcat to run on 9090 instead of 8080:

    <Connector port="9090" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

Finally, restart the Tomcat to use the new port.

3. Conclusion

In this article, we discussed the process to change the port on which Tomcat is running.

Fixing FileUploadBase$SizeLimitExceededException on Tomcat.

 

Mostly when doing hot deployments of web applications through manager on Tomcat, the hot deployment may fail with reason similar to below.

 

SEVERE [http-nio-8080-exec-2] org.apache.catalina.core.ApplicationContext.log HTMLManager: FAIL – Deploy Upload Failed, Exception: [org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (75715094) exceeds the configured maximum (52428800)]
java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (75715094) exceeds the configured maximum (52428800)
    at org.apache.catalina.connector.Request.parseParts(Request.java:2890)
    at org.apache.catalina.connector.Request.parseParameters(Request.java:3185)
    at org.apache.catalina.connector.Request.getParameter(Request.java:1116)
    at org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:381)
    at org.apache.catalina.filters.CsrfPreventionFilter.doFilter(CsrfPreventionFilter.java:109)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:607)
    at org.apache.catalina.valves.RequestFilterValve.process(RequestFilterValve.java:348)
    at org.apache.catalina.valves.RemoteAddrValve.invoke(RemoteAddrValve.java:52)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:770)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (75715094) exceeds the configured maximum (52428800)
    at org.apache.tomcat.util.http.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:805)
    at org.apache.tomcat.util.http.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:256)
    at org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:280)
    at org.apache.catalina.connector.Request.parseParts(Request.java:2846)
    … 28 more

 

This is because the max file size that can be uploaded through manager app is set to 50MB, and your war file size is larger than the 50 MB.

This limit can be modified in web.xml file available at <Your Tomcat Root Folder>\webapps\manager\WEB-INF. If you open the web.xml, you should find configurations similar to below.

 

<servlet>
    <servlet-name>HTMLManager</servlet-name>
    <servlet-class>org.apache.catalina.manager.HTMLManagerServlet</servlet-class>
    <init-param>
      <param-name>debug</param-name>
      <param-value>2</param-value>
    </init-param>

  <!– Uncomment this to show proxy sessions from the Backup manager or a
       StoreManager in the sessions list for an application
  <init-param>
    <param-name>showProxySessions</param-name>
    <param-value>true</param-value>
  </init-param>
  –>
    <multipart-config>
      <!– 50MB max –>
      <max-file-size>52428800</max-file-size>
      <max-request-size>52428800</max-request-size>

      <file-size-threshold>0</file-size-threshold>
    </multipart-config>
  </servlet>

 

Change the max-file-size and max-request-size to a larger value. The value is in Bytes, so convert the value you want to configure into bytes. So if you want to configure 100 MB, then the value should be 100*1024*104 = 104857600. So the new configuration will look similar to below.

 

<servlet>
  <servlet-name>HTMLManager</servlet-name>
  <servlet-class>org.apache.catalina.manager.HTMLManagerServlet</servlet-class>
  <init-param>
    <param-name>debug</param-name>
    <param-value>2</param-value>
  </init-param>
  <!– Uncomment this to show proxy sessions from the Backup manager or a
       StoreManager in the sessions list for an application
  <init-param>
    <param-name>showProxySessions</param-name>
    <param-value>true</param-value>
  </init-param>
  –>
  <multipart-config>
    <!– 100MB max –>
    <max-file-size>104857600</max-file-size>
    <max-request-size>104857600</max-request-size>

    <file-size-threshold>0</file-size-threshold>
  </multipart-config>
</servlet>

Once the modification is saved, restart the tomcat for the settings to apply and then should be able to deploy a war file up to 100 MB without any issues. The value can be changed based on the size of war files that you normally deploy.