Exporting project and war files when creating server-side Java in Eclipse

From the conclusion, it seems good to make it with "Dynamic Web Project".

The other day, I called a simple Servlet + JSP project "Tomcat project" under the title Create a memo app with Tomcat + JSP + Servlet + MySQL using Eclipse. I created it with Maven, but even if I select [Tomcat project]-> [Create WAR file according to project settings] in the right-click menu of the project, a file that is a solidified file in the project can be created. It was a little subtle, so I did some research.

Also, [Convert to Maven Project](https://qiita.com/zaki-lknr/items/32690b071abf202281d6#tomcat%E3%83%97%E3%83%AD%E3%82%B8%E3%82% A7% E3% 82% AF% E3% 83% 88% E3% 82% 92maven% E3% 81% AB% E5% A4% 89% E6% 8F% 9B% E3% 81% 99% E3% 82% 8B) Regarding, Maven project has a recommended directory structure, and Eclipse conversion function will not be done until that configuration conversion, so if you plan to use Maven, it seems better to create it as a Maven project from the beginning. ..

Creating a project

Created in a dynamic web project

In addition to creating a Tomcat project, there is a "Dynamic Web Project" and select it.

image.png

Created with Maven's maven-archetype-webapp

If you want to manage libraries and builds using Maven, select "Maven project" instead of "Dynamic web project" and do not check "Skip archetype selection" and "maven-archetype-webapp" Create a project by specifying

image.png

image.png

This project will be a dynamic web project that can use Maven

Create war file

For tomcat project

You can create a war file from [Tomcat project]-> [Create WAR file according to project settings] in the right-click menu of the project (* Enter "WAR export settings" in the Tomcat settings of the project properties in advance.

image.png

image.png

However, when creating this war file, external jar files (JDBC, etc.) were not included even if the export settings of the build path were done, and as I wrote at the beginning, the .settings directory and other unnecessary things were not included. Included and very subtle. (If you deploy to Tomcat, it works) If the external jar is included in the project configuration (/WEB-INF/lib/***.jar etc.), it will be included in the war file (but in that case, it will be double-managed as a maven project). I feel like

[Export] of the normal menu is not possible. (Not included in the target project)

For dynamic web projects

Since it is not a Tomcat project in Eclipse, it cannot be created from the Tomcat menu (there is no menu). However, you can export in war format from [File (F)]-> [Export (O)] in the normal menu.

image.png

image.png

Here, set the destination (save destination of the war file) appropriately by clicking the [Browse] button, and click [Finish] to create the war file. In the case of Maven's maven-archetype-webapp specified project, it exports to a war file including the external library such as JDBC introduced by Maven. In the case of a dynamic web project, it is only a file in the project configuration like the war file of the Tomcat project (if it refers to a file outside the project, it is not included in the war file)

Creating a new Servlet class

For tomcat project

[Create from the creation of "Class" because the creation of "Servlet" in the new creation menu cannot be used](https://qiita.com/zaki-lknr/items/32690b071abf202281d6#%E3%82%B5%E3%83% BC% E3% 83% 96% E3% 83% AC% E3% 83% 83% E3% 83% 88% E3% 81% AE% E4% BD% 9C% E6% 88% 90)

For dynamic web projects

Create from "Servlet" in the new menu

image.png

image.png

Specifying URL mapping (corresponding to the source @ WebServlet annotation)

image.png

You can also choose which method to override when creating the source file.

image.png

Output source

ExampleServlet.java


package jp.example.www;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class MainServlet
 */
@WebServlet(description = "Main screen", urlPatterns = { "/" })
public class MainServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MainServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

For maven-archetype-webapp

Same for .java files as for dynamic web projects. In this project, URL mapping and description meta information are described in src / main / webapp / WEB-INF / web.xml, and if it is from the newly created" Servlet "menu, it will be updated automatically. Will do it.

Recommended Posts

Exporting project and war files when creating server-side Java in Eclipse
In Java 10, when you do gradle eclipse and JavaSE-1.10 comes out, ...
Reading and writing gzip files in Java
Add .gitignore when creating a project in Xcode
When the project is not displayed in eclipse
[Java] Eclipse shortcuts that make sense when you go back and forth between source code in a project
Eclipse ~ Java project creation ~
Read binary files in Java 1
Null-safe program in Java (Eclipse)
Read binary files in Java 2
First Java development in Eclipse
Project Facets in Eclipse does not have Java Server Faces (JSF)
"ClassNotFoundException" is displayed when tomcat is started in eclipse and cannot be started
Easily read text files in Java (Java 11 & Java 7)
Encoding and Decoding example in Java
<java> When "EXCEPTION_ACCESS_VIOLATION" appears in awt
Try running Selenuim 3.141.59 in eclipse (java)
Create a Java project using Eclipse
Hello World in java in eclipse now
StringBuffer and StringBuilder Class in Java
Reading and writing Java basic files
Understanding equals and hashCode in Java
Creating lexical analysis in Java 8 (Part 2)
JPA (Java Persistence API) in Eclipse
Creating lexical analysis in Java 8 (Part 1)
Hello world in Java and Gradle
Specify the order in which configuration files and classes are loaded in Java
How to set tabs and spaces to be visible by using the tab key to insert spaces in Java files in Eclipse