Create Maven Web Project

Introduction

If you create a Web Project for testing and delete it when you're done, you don't have to be Maven, but I'll make it available for a while.

Preparation of Eclipse Plugin

If you can't find the menu in New> Other> Maven> Maven Project, the plugin is missing and you should install it. In Help> Install New Software, select and install as follows. image.png

Create Maven Project

image.png

GropuId: qiita.keniooi // In the sense for this page, ArtifactId: sampleWeb // As a project name Version: Default Package : sample.web

image.png

Press Finish and you're done.

Edit pom.xml

Write the required library in pom.xml so that you can use it. Here, add the JSON library.

JSON In Java Click 20180130 on this site and copy from the following part. image.png

Open the pom.xml in your sampleWeb project and add the copied text inside the dependencies tag.

pom.xml


...
	<dependencies>
		<dependency>
			<groupId>net.wasdev.maven.tools.targets</groupId>
			<artifactId>liberty-target</artifactId>
			<version>RELEASE</version>
			<type>pom</type>
			<scope>provided</scope>
		</dependency>
		
	<!-- https://mvnrepository.com/artifact/org.json/json -->
		<dependency>
			<groupId>org.json</groupId>
			<artifactId>json</artifactId>
			<version>20180130</version>
		</dependency>
	</dependencies>
...

With this setting, you can see that it has been added to Maven Dependencies.

image.png

Servlet creation

I am planning to create a Servlet that receives JSON by POST and processes it, but here, I will create a Servlet that processes String by JSONArray and JSONObject in the Servlet. Since it is written in doPost (), doGet () for testing is just calling doPost ().

TestServlet.java


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

	public TestServlet() {
		super();
	}

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

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("application/json;charset=UTF-8");
		String json = "[{\"name\":\"Satoh Taro\",\"id\":\"123\",\"mail\":\"[email protected]\"},"
				+ "{\"name\":\"Suzuki Hana\",\"id\":\"124\",\"mail\":\"[email protected]\"}]";
		JSONArray ja = new JSONArray(json);
		ArrayList<Person> list = new ArrayList<Person>();
		for(Object o : ja) {
			JSONObject jo =(JSONObject) o;
			Person p = new Person();
			p.setName(jo.getString("name"));
			p.setId(jo.getString("id"));
			p.setMail(jo.getString("mail"));
			list.add(p);
			System.out.println(p.getId() + "," + p.getName() + "," + p.getMail());
		}
		response.getWriter().println(json);

	}

}

class Person {
	String name;
	String id;
	String mail;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getMail() {
		return mail;
	}
	public void setMail(String mail) {
		this.mail = mail;
	}
}


When you access this Servlet with Firefox, it looks like this: image.png

Liberty's log shows

stdout


123,Satoh Taro,[email protected]
124,Suzuki Hana,[email protected]

Is output.

reference

From installation to project creation, a convenient development environment called Eclipse + Maven [Java] How to handle JSON data with standard API, Jackson, JSON in Java

Recommended Posts

Create Maven Web Project
Create Maven Project
Create a Maven project with a command
maven
Create a Jetty project using Eclipse
CI for Maven project on AppVeyor
CI for Maven project at CircleCI
Create a tomcat project using Eclipse
Create a Java project using Eclipse
How to make a Maven project
Create a web environment quickly using Docker
Create a simple web application with Dropwizard
War deploy to Azure Web Apps (maven)
How to create a Maven repository for 2020
Added slf4J + logback to Eclipse Maven project
Create Java Spring Boot project in IntelliJ
Quick build maven project using maven docker container
CI for Maven project at Github Actions
CI for Maven project in Azure Pipelines