--Create Maven project in Eclipse --Register Tomcat (Java Servlet API) and Square in Maven project --Registration page creation (JSP creation) --Create internal processing-1- (communication with JSP) --Create internal processing-2- (Communication with Square)
Reference site [Eclipse / Tomcat] Servlet + JSP in Maven webapp project
-(** Not selected ) Create a simple project (skip archetype selection) -( Select ) Use default workspace location -( not selected ) Add project to working space -( Not set **) Extended
To briefly explain
-** Group id ** is ** Group to which you belong ** -** Artifid id ** is ** project name **
You can think of it. If you don't know what the group id should be, "com. ○○" ← ○ should be the ID that skipped @ on Twitter. For those who want to know more ↓ 「Guide to naming conventions on groupId, artifactId, and version」 "Create a new Maven project in Eclipse (Java)"
Right-click ** JRE system library [JavaSE-1.7] ** in your Maven project
Click ** Properties **
Click ** Java SE-1.7 (java7) ** in the execution environment
Click ** Java SE-11 (java11) **
Success if it becomes Java SE-11!
Right-click ** JRE system library [JavaSE-11] ** in your Maven project
Click ** Build Path ** to ** Build Path Configuration **
Select ** Source ** (Top "** Source **" "Project" "Library" "Order and Export" "Module Dependencies")
Select ** Adapt and Close **
Success if the missing folder is created!
Reference site [Eclipse / Tomcat] Servlet + JSP in Maven webapp project
https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api ↓ xml code Ver: 4.0.1 at the time of article creation (April 26, 2020)
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
https://search.maven.org/search?q=g:com.squareup%20AND%20a:square ↓ xml code Ver: 5.2.2.20200422 at the time of article creation (April 26, 2020)
<dependency>
<groupId>com.squareup</groupId>
<artifactId>square</artifactId>
<version>5.2.2.20200422</version>
</dependency>
Inserted between line 36 (</ dependency>) and line 37 (</ dependency>) at the time of article creation ↓ Inserted from the 37th line to the 41st line
Thank you for waiting! Now that the preparations are complete, I'm going to program!
I made it MainServlet.
MainServlet.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!--↓ Site name-->
<title>square customer registration page</title>
</head>
<body>
<!--↓ Destination Java-->
<form action="./Main">
<p>name</p>
<a>sex</a>
<!--↓ Box to put the surname-->
<input type="text" name="name1">
<a>Name</a>
<!--↓ Box to put your name-->
<input type="text" name="name2">
<br>
<!--↓ Send button to Jsp-->
<input type="submit" value="Send">
</form>
</body>
</html>
I get angry when I do this in my actual work ...
Be careful not to be killed by Ahagon. Headshot attention
Changed (added) location ↓
Main.java
//Forward to JSP (specify file location)
String view = "/WEB-INF/MainServlet.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(view);
dispatcher.forward(request, response);
All ↓
Main.java
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Main
*/
public class Main extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Main() {
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());
//Forward to JSP (specify file location)
String view = "/WEB-INF/MainServlet.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(view);
dispatcher.forward(request, response);
}
/**
* @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);
}
}
↓ Success!
↓ If the characters are garbled
Main.java
response.getWriter().append("Served at: ").append(request.getContextPath());
↑ Let's comment this out!
Description: Put the characters in the Jsp "id = name1" textbox into "String name1"
Main.java
//Received from jsp
String name1 = request.getParameter("name1");//name(sex)
String name2 = request.getParameter("name2");//name(Name)
Description: Run if name1 and name2 are not null
Main.java
if (name1 != null || name2 != null) {
}
Description: Run if name1 and name2 are not blank. If it is blank, an error will be issued.
Main.java
if (!name1.equals("") && !name2.equals("")) {
}else{
System.out.println("Please enter your gender and name");
}
Description: Send sex and name to Square.java
Main.java
//Send to square
Square square = new Square();
square.main(name1, name2);
Main.java
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import square.SquareMain;
/**
* Servlet implementation class Main
*/
public class Main extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Main() {
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());
//Received from jsp
String name1 = request.getParameter("name1");//name(sex)
String name2 = request.getParameter("name2");//name(Name)
if (name1 != null || name2 != null) {
if (!name1.equals("") && !name2.equals("")) {
//Send to square
Square square = new Square();
square.main(name1, name2);
System.out.println("Done:");
} else {
System.out.println("Please enter your gender and name");
}
}
//Forward to JSP (specify file location)
String view = "/WEB-INF/MainServlet.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(view);
dispatcher.forward(request, response);
}
/**
* @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);
}
}
Referenced site "Java client library for the Square API"
Also pay attention to Eclipse ...
↓ For testing
Square.java
SquareClient client = new SquareClient.Builder()
.environment(Environment.SANDBOX)
.accessToken("YOUR_SANDBOX_ACCESS_TOKEN")
.build();
↓ For production
Square.java
SquareClient client = new SquareClient.Builder()
.environment(Environment.PRODUCTION)
.accessToken("ACCESS TOKEN HERE")
.build();
4.CustomersAPI Details: "Customers"
Square.java
CustomersApi api = client.getCustomersApi();
Details: "Create Customer"
Square.java
CreateCustomerRequest createCustomerRequest = new CreateCustomerRequest.Builder()
.idempotencyKey("unique_idempotency_key")
.givenName(name1)//sex
.familyName(name2)//Name
.address(null)//Not used this time
.build();
Square.java
try {
CreateCustomerResponse response = api.createCustomer(createCustomerRequest);
} catch (ApiException e) {
List<Error> errors = e.getErrors();
int statusCode = e.getResponseCode();
HttpContext httpContext = e.getHttpContext();
// Your error handling code
System.err.println("ApiException error when calling API");
e.printStackTrace();
} catch (IOException e) {
// Your error handling code
System.err.println("IOException error when calling API");
e.printStackTrace();
}
Square.java
import java.io.IOException;
import java.util.List;
import com.squareup.square.Environment;
import com.squareup.square.SquareClient;
import com.squareup.square.api.CustomersApi;
import com.squareup.square.exceptions.ApiException;
import com.squareup.square.http.client.HttpContext;
import com.squareup.square.models.CreateCustomerRequest;
import com.squareup.square.models.CreateCustomerResponse;
import com.squareup.square.models.Error;
public class Square {
public void main(String name1, String name2) {
SquareClient client = new SquareClient.Builder()
.environment(Environment.PRODUCTION)
.accessToken("ACCESS TOKEN HERE")
.build();
CustomersApi api = client.getCustomersApi();
CreateCustomerRequest createCustomerRequest = new CreateCustomerRequest.Builder()
.idempotencyKey("unique_idempotency_key")
.givenName(name1)//sex
.familyName(name2)//Name
.address(null)//Not used this time
.build();
try {
CreateCustomerResponse response = api.createCustomer(createCustomerRequest);
} catch (ApiException e) {
List<Error> errors = e.getErrors();
int statusCode = e.getResponseCode();
HttpContext httpContext = e.getHttpContext();
// Your error handling code
System.err.println("ApiException error when calling API");
e.printStackTrace();
} catch (IOException e) {
// Your error handling code
System.err.println("IOException error when calling API");
e.printStackTrace();
}
}
}
I will be careful from the next time
PS insertion is erotic, isn't it?
Recommended Posts