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. ..
In addition to creating a Tomcat project, there is a "Dynamic Web Project" and select it.
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
This project will be a dynamic web project that can use Maven
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.
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)
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.
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)
[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)
Create from "Servlet" in the new menu
Specifying URL mapping (corresponding to the source @ WebServlet
annotation)
You can also choose which method to override when creating the source file.
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);
}
}
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.