2014-01-29 37 views
6

在我的項目中,在客戶端工作的團隊要求我編寫一個樣本測試頁面,併爲他們提供一個他們可以打開並返回200的工作URL。他們有請我也給一個樣品申請機構。 這是我將要給他們的請求主體。創建一個返回JSON響應的簡單頁面

{ 
"MyApp": { 
    "mAppHeader": { 
     "appId": "", 
     "personId": "", 
     "serviceName": "", 
     "clientIP": "", 
     "requestTimeStamp": "",  
     "encoding": "", 
     "inputDataFormat": "", 
     "outputDataFormat": "", 
     "environment": "" 
    }, 
    "requestPayload": { 
     "header": { 
      "element": "" 
     }, 
     "body": { 
      "request": { 
       "MyTestApp": { 
        "data": { 
         "AuthenticationRequestData": { 
          "appId": "", 
          "appPwd": "" 
         } 
        } 
       } 
      } 
     } 
    } 
    } 
} 

要開發樣本頁面,我不知道從哪裏開始。請在你的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> 

這是我的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 { 

    } 

} 

我的應用是具有應用標識,並appPassword的getter和setter一個簡單的Java類,所以我不打算在這裏發佈。

我的問題是 - 我是這樣做的還是100%的錯?請給我你的建議。

+0

如果你可以點我的鏈接在那裏我能得到什麼,需要一個更好的想法做這很好太。或者如果你能舉一些例子,那也沒關係。我只是在這裏澄清我的方法。我覺得我沒有把握,所以需要一些建議。 – rickygrimes

+0

如果他們要求使用JSON數據進行200響應,他們可能打算使用AJAX請求。這意味着可能有用戶已經登錄的期望,可能是你想要檢查的東西。 –

+0

我沒有任何答案,因爲我面臨類似的問題(雖然這是爲了個人學習);我可以告訴你最近我學到的一件事:servlet是無狀態的(所以如果你要檢查那些_appID_和_appPwd_,那麼你將無法區分兩個不同的應用程序用戶)。我想你會更好地將支票和json代移交給另一個bean。 – watery

回答

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); 
0

你說你想從客戶端向服務器傳遞一些參數,但同時顯示一個JSON消息,然後你的servlet代碼看起來好像參數通過請求傳遞。

如果您打算在請求正文中傳遞JSON,服務器將不會自動爲您解析,因此您的調用request.getParameter("appID")request.getParameter("appPwd")將不起作用。

相反,你將需要解析的身體像這樣的(基於示例消息以上):

JsonObject jsonRequest = Json.createReader(request.getInputStream()).readObject(); 

JsonObject authenticationRequestData = jsonRequest 
    .getJsonObject("MyApp") 
    .getJsonObject("requestPayload") 
    .getJsonObject("body") 
    .getJsonObject("request") 
    .getJsonObject("MyTestApp") 
    .getJsonObject("data") 
    .getJsonObject("AuthenticationRequestData"); 

App app = new App(); 
app.setAppID(authenticationRequestData.getJsonString("appID")); 
app.setAppPassword(authenticationRequestData.getJsonString("appPwd"));  

一旦你的servlet有你要用來創建響應的對象,你可以簡單地生成在JSON並將其寫入使用HttpServletResponse.getWriter()這樣的:

SomeObject someObject = app.doSomething(); 

JsonObject jsonObject = Json.createObjectBuilder() 
.add("someValue", someObject.getSomeValue()) 
.add("someOtherValue", someObject.getSomeOtherValue()) 
.build(); 

response.setContentType("application/json"); 
Json.createJsonWriter(response.getWriter()).writeObject(jsonObject); 
2

你可以創建一個相同的格式格式化,然後只用內置的方法的Java對象

@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"); 
     } 
    } 
} 

然後,你只需要讓你的MyObject與你的檢索值相同的設置。 要麼讓一個對象設置相同,要麼可以使用與發送它相同的Java類,如果可能的話。

爲了您的你就必須有一個類MyApp的,然後有對象mAppHeader和requestPayload

public class mAppHeader(){ 
    String appId = ""; 
    String personId = ""; 
    String serviceName = ""; 
    String clientIP = ""; 
    String requestTimeStamp = "";  
    String encoding = ""; 
    String inputDataFormat = ""; 
    String outputDataFormat = ""; 
    String environment = ""; 
}