Posted In: Maven

Maven + Eclipse + Servlet3 + Tomcat9 – Best way to create Servlet3 maven project using eclipse

There are multiple articles on Web on how to set up maven web project in eclipse. Most of these articles do it through external maven installation and use maven-archtype-webapp. Problem with this approach is you may not have external maven in organization and usually is not allowed with out approval. Then there is problem of servlet version in this archtype which is 2.3. Your app server may not even support this anymore. Changing it afterwards is very tedious. I am adding steps that I have taken to it through eclipse plugin and then changing it to higher JDK, Servlet facet.
Using correct archetype will resolve all of these issues.

Steps
1. Download eclipse Mars or Neon. This has already maven plugin installed
2. Select create new maven project
3. com.airhacks archetype
4. Select maven-archtype-webapp
5. Add Group id (example com.javausecase)
6. Add Artifact id (example ExampleProject)
7. Assign Tomcat 9 runtime
8. Check deployment assembly. Create java, test folders if required
8. Create simple Servlet
9. Deploy and run tomcat

package com.javausecase.example;

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;

@WebServlet("/Servlet1")
public class Servlet1 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public Servlet1() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}

Tags: , , , ,

by , on January 17th, 2017

  • Categories