The team that works on the client side in my project has asked me to write a sample test page, and give them a working URL that they can hit and get back 200. They have asked me to also give a sample request body. Here is the request body I will be giving to them.
{
"MyApp": {
"mAppHeader": {
"appId": "",
"personId": "",
"serviceName": "",
"clientIP": "",
"requestTimeStamp": "",
"encoding": "",
"inputDataFormat": "",
"outputDataFormat": "",
"environment": ""
},
"requestPayload": {
"header": {
"element": ""
},
"body": {
"request": {
"MyTestApp": {
"data": {
"AuthenticationRequestData": {
"appId": "",
"appPwd": ""
}
}
}
}
}
}
}
}
To develop the sample page, I have no idea where to start. Please go easy on your downvotes (in case the question seems irrelevant) as I have no experience working on JSP Servlets.
Here is what I have as of now. Its a simple login page -
<%@ page language="java"
contentType="text/html; charset=windows-1256"
pageEncoding="windows-1256"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
<title>Login Page</title>
</head>
<body>
<form action="TestClient">
AppId
<input type="text" name="appID"/><br>
AppPassword
<input type="text" name="appPwd"/>
<input type="submit" value="submit">
</form>
</body>
</html>
And this is my servlet class -
package com.test;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestClient extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TestClient() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/json");
App app = new App();
app.setAppID(request.getParameter("appID"));
app.setAppPassword(request.getParameter("appPwd"));
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
My App is a simple Java class that has appID, and appPassword with getters and setters, so I am not going to post that here.
My question is - Am I doing this right or 100% wrong? Please give me your advice.
If you want to return JSON, you need to have the JSON string as the only text returned. This is not possible using client side JavaScript for the reason you've seen. You should amend your JSON feed to be a server-side resource.
json() returns a JSON object of the result (if the result was written in JSON format, if not it raises an error). Python requests are generally used to fetch the content from a particular resource URI. Whenever we make a request to a specified URI through Python, it returns a response object.
print(response. json()) should give the the data formatted as JSON for this response.
json() The json() method of the Response interface takes a Response stream and reads it to completion. It returns a promise which resolves with the result of parsing the body text as JSON .
You could create a Java Object that is formatted in the same format and then just use the built in methods
@WebServlet(urlPatterns = {"/BasicWebServices"})
public class BasicWebServices extends HttpServlet{
private static final long serialVersionUID = 1L;
private static GsonBuilder gson_builder = new GsonBuilder().serializeNulls().setDateFormat("MM/dd/yyyy");
public BasicWebServices(){
super();
}
@Override
public void destroy(){
super.destroy();
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
doPost(request, response);
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
try{
Gson gson = BasicWebServices.gson_builder.create();
MyObject myObject = new MyObject();
response.getWriter().write(gson.toJson(myObject));
}
catch(Exception e){
response.getWriter().write("ERROR");
}
}
}
Then you just have to make your MyObject the same setup as your retrieved values. Either make an object that is setup the same, or if you can use the same java class as the one sending it if possible.
For your's you would have to have a class MyApp and then have the objects mAppHeader and requestPayload
public class mAppHeader(){
String appId = "";
String personId = "";
String serviceName = "";
String clientIP = "";
String requestTimeStamp = "";
String encoding = "";
String inputDataFormat = "";
String outputDataFormat = "";
String environment = "";
}
In doGet Method use Gson jarLibrary to generate JSON response
like below
Gson gson = new Gson();
HashMap map = new HashMap();
map.put("MyApp",object);
String jsonString = gson.toJson(map);
PrintWriter writer = response.getWriter();
writer.print(jsonString);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With