创建一个返回JSON响应的简单页面

ric*_*mes 6 java jsp

在我的项目中在客户端工作的团队要求我编写一个示例测试页面,并为他们提供一个可以点击并返回200的工作URL.他们要求我也提供样本请求正文.这是我要给他们的请求主体.

{
"MyApp": {
    "mAppHeader": {
        "appId": "",
        "personId": "",
        "serviceName": "",
        "clientIP": "",
        "requestTimeStamp": "",     
        "encoding": "",
        "inputDataFormat": "",
        "outputDataFormat": "",
        "environment": ""
    },
   "requestPayload": {
        "header": {
            "element": ""
        },
        "body": {
            "request": {
                "MyTestApp": {
                    "data": {
                        "AuthenticationRequestData": {
                            "appId": "",
                            "appPwd": ""
                        }
                    }
                }
            }
        }
    }
    }
}
Run Code Online (Sandbox Code Playgroud)

要开发示例页面,我不知道从哪里开始.请放弃你的downvotes(如果问题似乎无关紧要),因为我没有使用JSP Servlets的经验.

这就是我现在所拥有的.它是一个简单的登录页面 -

    <%@ 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>
Run Code Online (Sandbox Code Playgroud)

这是我的servlet类 -

 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 {

    }

}
Run Code Online (Sandbox Code Playgroud)

我的应用程序是一个简单的Java类,它有appID,appPassword有getter和setter,所以我不打算在这里发布.

我的问题是 - 我这样做是对还是100%错了?请给我你的建议.

Shi*_*mar 1

在 doGet 方法中使用 Gson jarLibrary 生成 JSON 响应

像下面这样

Gson gson = new Gson();
HashMap map = new HashMap();

map.put("MyApp",object);

String jsonString = gson.toJson(map);
PrintWriter writer = response.getWriter();
writer.print(jsonString);
Run Code Online (Sandbox Code Playgroud)