Tomcat

Aus Wiki - Jochen Hammann
Zur Navigation springen Zur Suche springen


Remote Debugging

How do I configure Tomcat to support remote debugging?

A very good explanation to remote debugging can be found here.


How do I remotely debug Tomcat using Eclipse?

This answer assumes that you have a project set up and have some idea of what you are doing in this respect. If not then that is really outside the scope of this topic and you need to go to eclipse.org and read up on how to use your IDE, and maybe practice a little bit before you come back to this. We are also going to assume that you have some idea of what a debugger is and how to use one.

  • Make sure that Tomcat is started in remote debugging mode as described above and that your app is deployed.
  • Make sure that you have the sources for the code that you are trying to debug in your IDE. For the libraries and for Tomcat itself you can "attach" the sources to the jar files: open a class file and then click "Attach Source..." button.
  • If you have a servlet or something, set a breakpoint where it is sure to hit on the next request.
  • Go to "Run->Debug Configurations...". Click on "Remote Java Applications", then click "New". Type in the title. Note that port 8000 from the Tomcat instructions. Save and run.
  • Eclipse will connect to the JVM that Tomcat is running under. Wow, that was easy!

Now go type the url to submit to your servlet or whatever in your browser. Boom you hit the breakpoint right? Have fun!


How do I remotely debug Tomcat using NetBeans?

This answer assumes that you know how to work with a NetBeans Project, and also how to use the NetBeans debugger. If not, please go to http://www.netbeans.org/kb/using-netbeans/40/debug.html and read up on how to use NetBeans and its debugger.

Starting with Tomcat trunk revision 1484409, the Tomcat source includes Ant tasks to configure your source directory as a NetBeans Free-Form Project. After you have successfully run Ant with the default build target (deploy), you can then run the ide-netbeans target. This task will configure your sandbox copy of Tomcat so the source can be inspected, maintained and debugged under the NetBeans IDE. You should follow the detailed instructions in the README.txt file that will have been installed in your new nbproject directory.

Make sure that Tomcat is started in debug mode as described above, that your application is deployed, and that the sources are all defined as resources in your application. If you have a servlet or JSP file, set a breakpoint where you think a problem might be occurring. Go to "Run->Attach Debugger". A dialog pops up to let you specify the following options:

  • Debugger: JPDA Debugger
  • Connector: SocketAttach
  • Host: The IP address of the host your Tomcat installation is running on (127.0.0.1 if it is your local machine).
  • Port: The port of your Tomcat debugging interface, which should be 8000 if you've followed the instructions above.

When you press OK, you have a debugging connection very similar to local debugging.

Note that NetBeans has a second option -- you can debug JSP files and servlets locally using a Tomcat server that is bundled with the IDE. When you debug a JSP file or servlet in the IDE, the bundled Tomcat server automatically starts in debug mode, and the debugger connects to it.

The Tomcat NetBeans targets have not yet been back-ported to Tomcat 7. You can copy the files from your copy of the trunk and they should only require minimal editing of paths to work with older versions of Tomcat.


How do I remotely debug Tomcat using IntelliJ Ultimate?

The following screenshots show the cinfiguration to remotely debug a Tomcat Web Server.

Run-Debug Configurations 024.png

Run-Debug Configurations 025.png

Run-Debug Configurations 026.png

Run-Debug Configurations 027.png

JMX Monitoring via Java VisualVM

Zum (Remote) JMX Monitoring eines Tomcat müssen die folgenden JVM Parameter spezifiziert werden.

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.prefs
    • org.eclipse.jst.server.tomcat.core.prefs
      You can do it by the following command:
    • rm org.eclipse.wst.server.core.prefs
    • rm 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:

  1. Set URIEncoding="UTF-8" on your <Connector> in server.xml. References: HTTP Connector, AJP Connector.
  2. Use a character encoding filter with the default encoding set to UTF-8.
  3. 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).
  4. Change all your servlets to set the content type for responses and to include charset name in the content type to be UTF-8.
    Use response.setContentType("text/html; charset=UTF-8") or response.setCharacterEncoding("UTF-8").
  5. 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.
  6. 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>