Tomcat
Remote Debugging
Eine sehr gute Beschreibung zum Thema Tomcat Remote Debugging kann hier gefunden werden.
JMX Monitoring
Das Monitoring und Management via JMX in Java wird hier beschrieben. Die Einstellungen für den Tomcat 9 hier. Als Tools zum JMX Monitoring dienen JConsole oder VisualVM.
Bei der Verwendung von VisualVM sollte das Plugin VisualVM-MBeans installiert sein.
Lokales Monitoring
Werden die Monitoring Tools lokal, unter demselben Benutzer ausgeführt, unter dem auch der Tomcat läuft, müssen meist keine weiteren Einstellungen vorgenommen werden.
Remote Monitoring
Werden die Monitoring Tools Remote oder lokal und einem anderen Benutzer ausgeführt, so müssen folgende Einstellungen am Tomcat vorgenommen werden.
Command Line Interface (CLI)
Wird der Tomcat via CLI gestartet, so muss folgendes Setting spezifiziert werden. Als JMX Port wurde beispielhaft Port 8090 gewählt.
Unter Linux und MacOS muss folgendes in die Datei bin/setenv.sh aufgenommen werden.
CATALINA_OPTS=-Dcom.sun.management.jmxremote.port=8090 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false
Unter Microsoft Windows muss folgendes in die Datei setenv.bat aufgenommen werden.
set CATALINA_OPTS=-Dcom.sun.management.jmxremote.port=8090 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false
Windows Service
Läuft der Tomcat als Windows Service, so müssen im Tomcat Config Tool folgende Zeilen im Tab "Java" zu den "Java Options" hinzugefügt werden. Als JMX Port wurde beispielhaft Port 8090 gewählt.
-Dcom.sun.management.jmxremote.port=8090
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false
Namensgebung der WAR Datei
Bei der Namensgebung einer WAR Datei ist zu beachten, dass der Dateiname einen direkten Zusammenhang mit dem Context Name, dem Context Path und der Context Version besitzt. Hierzu kann auch die Tomcat Hilfe zum Thema Context eingesehen werden: siehe hier.
Im folgenden ist ein Auszug dieser Hilfe.
Naming
When autoDeploy or deployOnStartup is used then there is a close relationship between the context name, context path, context version and the base file name used for the WAR and/or directory that contains the web application when the WAR or directory is located in the Host's appBase. When no version is specified, the rules are:
- contextName = contextPath
- If the contextPath is a zero length string, the base name is ROOT
- If the contextPath is not a zero length string, the base name is the contextPath with the leading '/' removed and any remaining '/' characters in the path replaced with '#'.
When a version is specified, ##version is added to the contextName and base name. To help clarify these rules, some examples are given in the following table.
| Context Path | Context Version | Context Name | Base filename |
|---|---|---|---|
| /foo | None | /foo | foo |
| /foo/bar | None | /foo/bar | foo#bar |
| Empty String | None | Empty String | ROOT |
| /foo | 42 | /foo##42 | foo##42 |
| /foo/bar | 42 | /foo/bar##42 | foo#bar##42 |
| Empty String | 42 | ##42 | ROOT##42 |
The version component is treated as a String both for performance reasons and to allow flexibility in versioning schemes. String comparisons are used to determine version order. If version is not specified, it is treated as the empty string. Therefore, foo.war will be treated as an earlier version than foo##11.war and foo##11.war will be treated as an earlier version than foo##2.war. If using a purely numerical versioning scheme it is recommended that zero padding is used so that foo##002.war is treated as an earlier version than foo##011.war. If you want to deploy a WAR file or a directory using a context path that is not related to the base file name then one of the following options must be used to prevent double-deployment:
- Disable autoDeploy and deployOnStartup and define all Contexts in server.xml
- Locate the WAR and/or directory outside of the Host's appBase and use a context.xml file with a docBase attribute to define it.
Automatic Deployment via Maven
Das automatische Deployment via Maven erfordert die Spezifikation eines Admin-Users im Tomcat. Im Folgenden ist die Datei tomcat-users.xml abgebildet, welche exemplarisch die notwendigen Einstellungen zeigt.
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--><tomcat-users>
<!--
NOTE: By default, no user is included in the "manager-gui" role required
to operate the "/manager/html" web application. If you wish to use this app,
you must define such a user - the username and password are arbitrary.
-->
<!--
NOTE: The sample user and role entries below are wrapped in a comment
and thus are ignored when reading this file. Do not forget to remove
<!.. ..> that surrounds them.
-->
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="admin"/>
<user password="21admin" roles="admin,manager-gui,manager-script" username="admin"/>
</tomcat-users>
PermGen - Command Line Parameter
Der folgende JVM Command Line Parameter spezifiziert die zu verwendende maximale Größe der Java Permanent Generation:
-XX:MaxPermSize=512m
Proxy - Command Line Parameter
Die folgenden JVM Command Line Parameter spezifizieren die zu verwendende HTTP Proxy:
-Dhttp.proxyHost=<host>
-Dhttp.proxyPort=<port>
-Dhttp.proxyUser=<user name>
-Dhttp.proxyPassword=<password>
-Dhttp.nonProxyHosts=
Die folgenden JVM Command Line Parameter spezifizieren die zu verwendende HTTPS Proxy. Die nonProxyHosts werden durch die HTTP Konfiguration spezifiziert.
-Dhttps.proxyHost=<host>
-Dhttps.proxyPort=<port>
-Dhttps.proxyUser=<user name>
-Dhttps.proxyPassword=<password>
-Dhttp.nonProxyHosts=
Apache Tomcat 8 in Eclipse (Ubuntu)
Die Installation des Apache Tomcat unter Ubuntu erstellt mehrere Verzeichniss unterhalb des Root.
/usr/share/tomcat8
/usr/share/tomcat8-admin
/usr/share/tomcat8-docs
/usr/share/tomcat8-examples
/etc/tomcat8
/var/lib/tomcat8
Soll nun dieser Tomcat als Server in Eclipse unter Ubuntu verwendet werden, so müssen folgende Einstellungen vorgenommen werden. Man hat zwei verschiedene Möglichkeiten:
1
- Close Eclipse
- Go to your Eclipse workspace directory
- Then go to directory
.metadata/.plugins/org.eclipse.core.runtime/.settings - In Ubuntu, I do it by:
cd ~/workspace/.metadata/.plugins/org.eclipse.core.runtime/.settings - Delete the following two files:
org.eclipse.wst.server.core.prefsorg.eclipse.jst.server.tomcat.core.prefs
You can do it by the following command:rm org.eclipse.wst.server.core.prefsrm org.eclipse.jst.server.tomcat.core.prefs
- Start Eclipse
2
If the above solution did not solve the problem then you can try the following solution which contains creating some softlinks along with deleting the above mentioned files.
Run the following commands in terminal:
cd ~/workspace/.metadata/.plugins/org.eclipse.core.runtime/.settings/
rm org.eclipse.jst.server.tomcat.core.prefs
rm org.eclipse.wst.server.core.prefs
cd /usr/share/tomcat7
sudo service tomcat7 stop
sudo update-rc.d tomcat7 disable
sudo ln -s /var/lib/tomcat7/conf conf
sudo ln -s /etc/tomcat7/policy.d/03catalina.policy conf/catalina.policy
sudo ln -s /var/log/tomcat7 log
sudo chmod -R 777 /usr/share/tomcat7/conf
sudo ln -s /var/lib/tomcat7/common common
sudo ln -s /var/lib/tomcat7/server server
sudo ln -s /var/lib/tomcat7/shared shared
- Restart eclipse
- In Project Explorer of Eclipse, you can see ‘Servers’. Right click and delete it.
- Re-add the Server (File -> New -> Other -> Server)
- Now your project on Eclipse should run fine.
Verwendung von UTF-8 in allen Seiten
Die folgende Beschreibung ist hier entnommen.
Using UTF-8 as your character encoding for everything is a safe bet. This should work for pretty much every situation.
In order to completely switch to using UTF-8, you need to make the following changes:
- Set
URIEncoding="UTF-8"on your<Connector>in server.xml. References: HTTP Connector, AJP Connector. - Use a character encoding filter with the default encoding set to UTF-8.
- Change all your JSPs to include charset name in their contentType.
For example, use<%@page contentType="text/html; charset=UTF-8" %>for the usual JSP pages and<jsp:directive.page contentType="text/html; charset=UTF-8" />for the pages in XML syntax (aka JSP Documents). - Change all your servlets to set the content type for responses and to include charset name in the content type to be UTF-8.
Useresponse.setContentType("text/html; charset=UTF-8")orresponse.setCharacterEncoding("UTF-8"). - Change any content-generation libraries you use (Velocity, Freemarker, etc.) to use UTF-8 and to specify UTF-8 in the content type of the responses that they generate.
- Disable any valves or filters that may read request parameters before your character encoding filter or jsp page has a chance to set the encoding to UTF-8. For more information see http://www.mail-archive.com/users@tomcat.apache.org/msg21117.html.
org.apache.catalina.filters.SetCharacterEncodingFilter in web.xml spezifizieren
Das folgende Code-Fragment zeigt die Spezifikation bzw. Konfiguration des org.apache.catalina.filters.SetCharacterEncodingFilter zum Festlegen von UTF-8 für alle Requests.
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>org.apache.catalina.filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>