[JAVA] Tomcat, Context, debug settings in IntelliJ Community

Introduction

A good environment will increase the efficiency of development.

Originally I was an Eclipse user, and the classpath and Tomcat settings were all done by Eclipse, but after I met IntelliJ, which works smoothly, I could not give up that crispness and I had to switch. I did. However, for IntelliJ, you need to buy IntelliJ IDEA Ultimate (paid version) to get the "convenience" of Eclipse. It's the power of money.

I'm currently IntelliJ Community (free), for the following reasons: ・ I want to purchase books and courses, device and hardware parts, etc. for the amount of money I invest in a paid license every month. ・ I want to set some bases myself

The latter may be similar to the feeling of occasionally trying to create a package with the vim and javac commands without using the IDE.

goal

Roughly configure Tomcat and debugger in IntelliJ Community. (1) With an app named sample-app (2) Use local MySQL for DB resources (3) Try to connect to ``` http: // localhost: 8080 / sample-app /` ``.

OS is mac.

abridgement

Since it only connects Tomcat, it is related to the initial setting of java Servlet, jsp, Spring frame 0. javax.servlet

  1. pom.xml
  2. `web.xml (bean setting)`
  3. `` `$ {servlet-name} -servlet.xml(orapplication-context.xml```)
  4. Settings such as component-scan, annotation-driven, View Resolver, static file mapping in `` `servlet.xml```

I'd like to omit things like that and organize the DB settings and debugging as the main.

Create a package for your web app in IntelliJ

First

Select and install the required version of apache tomcat from Official Home .

Occasionally, there is a problem that the latest version of tomcat and java do not match, but for example, I have java8 and java9 in a slightly old tomcat, It can be solved by lowering JAVA_HOME, which was upgraded to 9 in the environment variable settings, to 8.

File -> new -> Project... 1.png

I want to use maven, so I will use maven webapp.

Enter something Next, Next, Next ..

s1.png

I happened to cover it, but it doesn't have to be the application name.

According to Maven POM Reference

In short, groupId can be a name that includes an institution name that can be distinguished from other projects, and artifactId should be a project name.

I remember being told that I didn't have to be so particular about personal development.

Let's load pom into the library

Load only pom from the initial state where pom has not been read yet and is not ready to deploy. I think that JUnit is written by default, but I can not develop an application with this alone.

s2.png

I have already tried it and added java-servlet and reimported it. A java-servlet has also been added to the External Libraries and can be referenced.

Deploy

Convert .java files to executable .class files.

s3.png

During development, we will clean it once, but since there is nothing yet, press package to generate a viable target package. I think the name was the default name.

s4.png

It's done. Similar to the File-> Project Structure-> complier output setting.

The light bulb mark says that the Ultimate version is good lol ..

The troublesome thing is to let Run-> Edit Configuratons ... do it.

It's exactly the same story, but press + on the upper left to add Maven, and pass a command that can do clean and package at the same time as an argument command in advance.

s5.png

It's a Working directory, but it had to be the absolute path of the application's directory. In my case, I put it in my mac's home directory, so I wrote it that way. Also, the name sample-config is appropriate.

-Dmaven.test.skip = true is an option to omit the test when you want to shorten the deployment time. It seems that omitting the test is not recommended originally, but the deployment work itself becomes heavy due to the increase in files, so I use it a lot.

s6.png

And this has been added. It's easy to proceed with just one click.

The command line is the easiest

Get the mvn command from the official home, install it,

$ cd {$APP_HOME}
$ mvn clean package -Dmaven.test.skip=true

It is also convenient to hit.

Context and Resource (database) settings

{$catalina_home}/conf/catalina/${host_name}/ Create a $ {APP_NAME} .xml file directly under it.

{$CATALINA_HOME}Is your own apache-At the location of tomcat${HOST_NAME}Is localhost if it's local.



#### **`${APP_NAME}.Although it is xml, sample is used for the application created this time.-I named it app, so if you express it with an absolute path,`**
```Although it is xml, sample is used for the application created this time.-I named it app, so if you express it with an absolute path,



#### **`/My tomcat/conf/Catalina/localhost/sample-app.Will be xml.`**

If you want to put multiple apps in one Tomcat, create a configuration file for each context

{$CATALINA_HOME}/conf/Server directly below.There is xml, and it works even if I write it directly,


 This is a setting common to all applications, so if you want to specify settings for each application such as DB connection, do not touch Server.xml directly under conf and `` `{$ CATALINA_HOME} / conf / Catalina / $ {HOST_NAME } / `` `I think it is better to create a configuration file for each co-context (application) directly under`.

 For example, write the DB Connection (MySQL) settings in Resource and save them.


#### **`sample-app.xml`**
```xml

<Context docBase="{$APP_HOME}/target/sample-app" path="/sample-app" reloadable="true">

  <Resource 
    name="jdbc/${jdbc_NAME}"
    type="javax.sql.DataSource"
    auth="Container"
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://${DB_SERVER}/${DB_NAME}?userUnicode=true&amp;charactorEncoding=UTF-8&amp;autoReconnect=true"
    username="${USER}" password="${PASS}" 
    validationQuery="select CURRENT_TIMESTAMP"
  />

</Context>

Context --Application configuration information

{$APP_HOME}Is the absolute path of the directory where the app is being developed.


 It is ``` {$ APP_HOME} / target / sample-app}` `` directly under, but it is the place where the class file is ejected every time you deploy, which is set in IntelliJ.
 I set it to ``` / target / $ {APP_NAME}` ``. If I can specify the location where the class file will be saved, I'd like to name it.
 The standard is a relative path from / webapp, but this time I specified it as an absolute path.

 * <b> path </ b> <br> Context path. The default is the value of the docBase attribute, so it should be optional if you want to sample-app like the value of the docBase attribute like this time ...
 * <b> reloadable </ b> <br> If there are updates to .class or .jar files, this is a flag that determines if Tomcat will check if they are reloaded automatically. Set to true during development. However, I think it is better for performance to rewrite it to false when all development is finished.

## Resource --Set up DB connection

 Add `` `<Resource> ... </ Resource>` `` for the number of databases you use.

 * <b> driverClassName </ b> <br> Fully qualified class name for the JDBC driver.
 * <b> type </ b> <br> Resource type. When using a data source, specify `` `javax.sql.DataSource```.
 * <b> auth </ b> <br> Resource control method. When using a data source, specify `` `Container```.
 * <b> name </ b> <br> This is the name given to the data source.

 > * Example of jndi (Java Naming and Directory Interface) and Servlet configuration in Spring Framework <br>
 It's like giving an object a name (binding). <br>

#### **`web.Of xml<servlet-mapping>Specified in<servlet-name>If is a sampleApp, I think I would write a configuration file with a name like this. application-context.It may be set using xml.`**
``` > `` `bean id``` specified the name of the DataSource. Later, when adding transaction processing, etc., ``` `` ` It is used in place of `` `ref```.

If you put name =" jdbc / sampleDB " in Resource earlier, you will get `` `java: comp / env / jdbc / sampleDB```.

  • url </ b>
    If both the server and the DB server are local and you want to connect to a DB called sample_DB in MySQL, you can use something like `` `jdbc: mysql: // localhost / sample_DB```. .
    If you want to use an external DB server, enter the connection information of that server.
    The? Parameter at the end is any option for encoding or sustaining the MySQL connection. is.
  • username and password </ b>
    Enter the name and password you used to log in to MySQL.
  • validationQuery </ b>
    This is an arbitrary low-load query to check if the connection is alive when getting a connection from the connection pool if there is no connection for a long time.

This does not cover all attributes ... for example Assuming personal use of local resources instead of external DB, connection pool idle settings maxIdle </ b> and I've omitted maxActive </ b> for the maximum number of connections in the pool.

  • Connection pool The pool here has the meaning of holding. A connection pool can hold multiple connections between the application and the DB server. It is a function to reuse the connection and manage the connection status of the database. Close (release) is a process to return the used connection to the pool for the next connection.
  • idle This is a pooled (reserved) connection.

Start tomcat

$ cd ${apache-location of tomcat}/bin
$ sh startup.sh

At the end

$ sh shutdown.sh

Occasionally, it accidentally double-starts, and the process remains, so in that case

$ ps -ef | grep tomcat

Then give the appropriate number `` `kill```.

Check with browser

s7.png

What you see is the jsp provided by default here. This is the target.

s8.png

If you try deleting target / sample-app / index.jsp, you'll see that the page is missing 404. You can do it again by deploying it again.

Debugger settings

bin/setenv.sh Directly under the bin directory directly under the directory of apache-tomcat earlier Create setenv.sh.

$ cd ${apache-location of tomcat}/bin
$ vi setenv.sh

The contents are as stated below.

export JPDA_ADDRESS=9999
export JPDA_TRANSPORT=dt_socket

9999 doesn't make sense, because it's a port number that doesn't seem to be assigned ... It's like deploying and running on port 8080 and remote debugging on port 9999.

Run -> Edit Configurations... スクリーンショット 2018-02-08 2.58.41.png

Change only the port number to 9999

s9.png

Start tomcat with jpda option

$ cd ${apache-location of tomcat}/bin
$ sh catalina.sh jpda start 

It's a startup with options, so you don't have to do `` `$ sh startup.sh```.

Set and make sure the debugger is set

s10.png

If you put a breakpoint as usual and pop the bug, it's okay.

The end

Since the initial settings are not done on a daily basis, I sometimes had a hard time in case of emergency, so I wrote it as a memo.

I think the best way to build an environment is to suit you. For that purpose, it may be the first to try various things including the paid version.

that's all

Correction log

All sample names have been unified to sample for easy understanding.