Showing posts with label Tomcat. Show all posts
Showing posts with label Tomcat. Show all posts

Wednesday, December 14, 2016

Creating a Connection Resource in Tomcat

MySQL, like most other relational database vendors, supplies a JDBC driver for connecting to MySQL databases from Java. You need to download this driver (a JAR file) and place it in your Tomcat installation so that you can use it from your applications. This may strike you as odd because other JAR files you use are simply packaged with your applications in /WEB-INF/lib. You must never do this with a JDBC driver for two reasons:

- Most important, doing so can cause a memory leak. JDBC drivers automatically register themselves with the java.sql.DriverManager, which is part of the Java SE core libraries. If your application includes a JDBC driver in /WEB-INF/lib, the DriverManager holds on to the driver classes forever, even if your application is undeployed. The application server then cannot fully undeploy your application, resulting in a memory leak.

- It’s a best practice to have the application server manage your JDBC DataSources. Application servers have built-in systems for managing connection pools, improving the performance of database connections in your applications. For the application server to be able to manage these connections, you must load the JDBC driver in the application server class loader instead of the web application class loader.

Installing the MySQL JDBC driver in Tomcat is extremely easy. On the MySQL download website locate the Connector/J product. This is the JDBC driver. It is platform-independent, so all you need to do is download the ZIP or TAR archive (whichever is easiest for you to use on your computer). Extract the JAR file from the archive, and copy it to C:\Program Files\Apache Software Foundation\Tomcat 8.0\lib (or the equivalent directory for your Tomcat installation).
That’s all there is to it. The next time Tomcat starts, the MySQL JDBC driver becomes available to all web applications.


Creating a Connection Resource in Tomcat


Creating a pooled connection DataSource in Tomcat is quite straightforward and takes only a few minutes. Instead of creating a brand new resource every time and having dozens of resources by the end, it is also acceptable to simply create one resource and change it every time you need to.

1. To create a connection resource, open C:\Program Files\Apache Software Foundation\Tomcat 8.0\conf\context.xml (or the equivalent in your Tomcat installation) in your favorite text editor.

2. Add the following <Resource> element between the beginning and end <Context> elements:
     <Resource name="jdbc/DataSourceName" type="javax.sql.DataSource"
          maxActive="20" maxIdle="5" maxWait="10000"
          username="mysqluser" password="mysqlpassword"
          driverClassName="com.mysql.jdbc.Driver"
          url="jdbc:mysql://localhost:3306/databaseName" />

3. For each example you must replace jdbc/DataSourceName with the appropriate data source name (which should always start with jdbc/) and databaseName with the correct database name. mysqluser and mysqlpassword should be replaced with the user and password that you previously created to access MySQL.

This <Resource> definition causes Tomcat to expose the connection pool DataSource as a JNDI resource so that it can be looked up via JNDI by any application.

Monday, October 31, 2016

Debugging Tomcat from IntelliJ IDEA

If you use IntelliJ IDEA 14, you have just a few simple steps to take to get up and running with your web applications. The first thing you need to do is set up IntelliJ to recognize your local Tomcat or other container installation. This is a one-time-only step you set it up once in your global IDE settings, then you can use the application server for any web application project. Next, set up each web application project to use your configured container. Finally, you just need to start your application from IntelliJ and place breakpoints where you’d like to debug your application.

To start, you need to configure Tomcat in IntelliJ’s list of application servers.

1. Open up IntelliJ’s IDE settings dialog. With a project open you can go to File ➪ Settings, or click the Settings icon in the toolbar, or press Ctrl + Alt + S. If you don’t have a project open, you can click the Configure button and then the Settings button.

2. In the left pane of the Settings dialog, click Application Servers under IDE Settings. Initially, you have no application servers configured.



3. Click the green plus icon to add a new application server. Click the browse button next to the Tomcat Home field to browse for and select the Tomcat home directory (for example, C:\Tomcat 8.0). Then click OK. IntelliJ should automatically detect your Tomcat version.


4. Click OK again to complete adding Tomcat to your list of application servers, and change the name if you want.

5. Click Apply to save the changes and OK to close the Settings dialog.

After you create a project and are ready to deploy it to Tomcat from IntelliJ, you need to add a Tomcat run/debug configuration to your project.

1. Click the run/debug configurations icon (a down arrow) on the toolbar, then click Edit Configurations.


2. In the dialog that appears, click the green plus icon, scroll to the bottom of the Add New Configuration menu, hover over Tomcat Server, and click Local. This creates a run/debug configuration for running your project against a local Tomcat.


3. If Tomcat 8.0 is the only application server you have added to IntelliJ, it is automatically selected as the application server this run/debug configuration will use. If you have other application servers configured, one of those might be selected, in which case you need to click the “Application server” drop-down and select Tomcat 8.0 instead.

4. Name the run configuration something meaningful.

5. You’ll probably see a warning that no artifacts are marked for deployment. Correcting this is simple. Click the Deployment tab and then the green plus icon under the “Deploy at the server startup” heading. Click Artifact, and then click the exploded war file artifact. Click OK. Change the “Application context” name for the artifact deployment to the server relative URL you want it deployed to.



6. Click Apply and then OK to save the run/debug configuration and dismiss the dialog.

Now that you have set up Tomcat in IntelliJ and configured an IntelliJ project to run in Tomcat, you’re ready to start the application and debug it within your IDE.

1. Open your project with IntelliJ IDEA.

2. Make sure that its run/debug configuration is properly configured to use your local Tomcat 8.0 application server.

3. When opened, click the Debug icon on the toolbar (highlighted by the mouse pointer in the figure below) or press Shift + F9 to compile and start your application in debug mode. IntelliJ should launch your default browser, and you should immediately hit the breakpoints in index.jsp.


You should again see the webpage to indicate that your application successfully deployed.

Installing Tomcat on Your Machine as a Command-Line Application

Most application developers need to run Tomcat only as a command-line application and usually only from their IDE. To do this, follow these steps:

1. Download the architecture-appropriate Windows zip (if you use Windows) or the non-Windows zip (if you use anything else) from the Tomcat 8.0 download page and unzip the directory.

2. Place the contents of the Tomcat directory in this zip file into the folder C:\Tomcat 8.0 on your local machine (or into the appropriate directory for a server in your operating system). For example, the webapps directory should now be located at C:\Tomcat 8.0\webapps.

3. If you use Windows 7 or newer, you need to change some permissions to make Tomcat accessible from your IDE. Right-click the Apache Software Foundation directory in C: and click Properties. On the Security tab, click the Edit button. Add your user or the Users group, and give that entry full control over the directory.

4. To configure Tomcat for its first use, start by opening the file conf/tomcat-users.xml in your favorite text editor. Place the following tag between the <tomcat-users> </tomcat-users> XML tags:
<user username="admin" password="admin" roles="manager-gui,admin-gui" />

5. Open the conf/web.xml file. Search the file for the text org.apache.jasper.servlet.JspServlet. Below the tag that contains this text are two <init-param> tags. Add the following init parameters below the existing init parameters:
<init-param>
     <param-name>compilerSourceVM</param-name>
     <param-value>1.8</param-value>
</init-param>
<init-param>
     <param-name>compilerTargetVM</param-name>
     <param-value>1.8</param-value>
</init-param>
By default, Tomcat 8.0 compiles JavaServer Pages files with Java SE 6 language support even if it runs on Java SE 8. These new Servlet init parameters instruct Tomcat to compile JSP files with Java SE 8 language features, instead.

6. After you make these changes and save these files, you should now be ready to start up Tomcat and make sure that it runs properly. Open up a command prompt and change your directory to the Tomcat home directory (C:\Tomcat 8.0).

7. Type the command echo %JAVA_HOME% (or echo $JAVA_HOME on a non-Windows operating system) and press Enter to check whether the JAVA_HOME environmental variable is properly set to your Java Development Kit (JDK) home directory. If it is not, configure the environmental variable, and then log out and back in before proceeding. Tomcat cannot run without this variable properly set.

8. Type the command bin\startup.bat (or bin/startup.sh if you do not use Windows) and press Enter. A Java console window should open showing the output of the running Tomcat process. After a few seconds, you should see the message “INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 1827 ms” or something similar in the console window. This means Tomcat has started properly.

9. Open your favorite Web browser and navigate to http://localhost:8080/. You should see a page that looks like Figure below. This means that Tomcat is running and JSPs are compiling properly with Java SE 8. If this screen does not come up or you observe an error in the Java console, you need to check the preceding steps and possibly consult the Tomcat documentation.


When you finish using Tomcat, you can stop it by running the command bin\shutdown.bat (or bin/shutdown.sh) in the command prompt in the Tomcat 8.0 home directory. The Java console window should close, and Tomcat will stop.