我有想要获得有效响应的 servlet 代码。这是请求的原始布局
{
"function": "Check",
"teamId": "<teamId>",
"teamKey": "<teamKey>",
"requestId": "<request-id>",
"firstName": "<FirstName>",
"lastName": "<LastName>",
"ticketNumber": "<ticket-num>"
}
我在 Intellij 的 servlet 中有这个。
import com.google.gson.*;
import com.google.*;
import org.apache.*;
import org.apache.http.*;
@WebServlet(name = "Logincheck", urlPatterns = {"/Logincheck"})
public class Servlet extends HttpServlet {
String teamID = "IC106-2";
String teamKey = "1b3741ccf6d9ec5245055370125d901e";
String url="http://fys.securidoc.nl:11111/Ticket";
int Min = 1;
int Max = 100;
int REQ_ID = Min + (int)(Math.random() *((Max - Min)+1));
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//index.html form user input
String fname = request.getParameter("firstName");
String lastName = request.getParameter("lname");
String ticketNummer = request.getParameter("ticketnr");
JsonParser parser = new JsonParser();
URL object=new URL(url);
String ticketCheck = "{\"function:\"Check\",\"teamId\":\"IC106-2\",\"teamKey\":\"1b3741ccf6d9ec5245055370125d901e\",\"requestId\":\""+REQ_ID+"\",\"firstName\":\""+fname+"\",\"lastName\":\""+lastName+"\",\"ticketNumber\":\""+ticketNummer+"\"}";
}
}
现在,当我点击运行时,它会打开我的 index.html,当我单击登录按钮到 /Logincheck 时,它会响应(在 intellij 内):
Bad Request
Teamkey 和 TeamID 100% 正确,但我可能忽略了与 Json 相关的一些事情。一般来说,我对 Json 和 servlet 的经验很少。就像我制作硬编码的登录和票证凭据一样,还是用户的输入正确已经正确?我应该期待这样的回应:
{
"ticketStatus": "<ticket-status>",
"requestId": "<request-id>",
"result": "<result-code>",
"resultText": "<result-text>"
}
Cats萌萌
holdtom
相关分类