第二步:servlet代码:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("application/json;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
String questionStr = request.getParameter("q");
String n = request.getParameter("n");
int topN = -1;
if(n != null && StringUtils.isNumeric(n)){
Question question = null;
List candidateAnswers = null;
if (questionStr != null && questionStr.trim().length() > 3) {
question = SharedQuestionAnsweringSystem.getInstance().answerQuestion(questionStr);
if (question != null) {
candidateAnswers = question.getAllCandidateAnswer(); } } try (
PrintWriter out = response.getWriter()) {
String json = JsonGenerator.generate(candidateAnswers, topN);
String jsonpCallback=request.getParameter("callbackparam");
out.println(jsonpCallback+"("+json+")"); } }