简体   繁体   English

我应该如何将Servlet中的响应发送到前端?

[英]How should I send the response in Servlet to Front end?

I have written a Servlet something like this 我写了一个像这样的Servlet

public class ServletUtil extends HttpServlet {

private static final long serialVersionUID = 1571172240732862415L;

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String acInfo = request.getQueryString();
    someDao dao = new someDao();
     ArrayList<String> resultAutoComplete=dao.someResults(acInfo);
    out.close();
}

} }

I have an auto complete Object/wizard in front end and as the user types in it is making Ajax call to back end to grab the list of results. 我在前端有一个自动完成的对象/向导,因为用户输入它是对后端进行Ajax调用以获取结果列表。 So I have written a Servlet and I am pulling the user input and getting my results from DAO layer. 所以我编写了一个Servlet,我正在提取用户输入并从DAO层获取结果。

My question here is how should I send this list(resultAutoComplete) to Front end in the Servlet? 我的问题是如何将这个列表(resultAutoComplete)发送到Servlet的前端?

I'd expect you to serialise this in some fashion such that the client understands it. 我希望你能以某种方式将其序列化,以便客户理解它。 eg perhaps using JSON or similar. 例如,可能使用JSON或类似的。

I note that your response content type is text/html . 我注意到您的响应内容类型是text/html So why not simply write each element of your list to your Writer out , separated by (say) a <li> element (with the appropriate unordered/order list entities surrounding this) 那么为什么不简单地将列表的每个元素写入你的Writer ,用(例如)一个<li>元素(用相应的无序/顺序列表实体包围) 分隔

By serializing it to a String and writing it to out ... 通过将其序列化到字符串,并将其写入out ...

Seriously though, I wouldn't code at the low level Servlet spec. 说真的,我不会在Servlet规范的低级代码。 For this kind of return-this-pojo call I would use Spring 3's RESTful service libraries , or something similar. 对于这种返回 - 这个pojo调用我将使用Spring 3的RESTful服务库 ,或类似的东西。

Try this, 尝试这个,

for (String str : resultAutoComplete)
                {
            out.println(str);
        }

No json! 没有json! instead of going through a list in javascript, return a completed <li> lists and replace innerHTML of <ul> .The reason to do so is to give a better performance. 而不是通过javascript中的列表,返回一个完整的<li>列表并替换<ul> innerHTML 。这样做的原因是为了提供更好的性能。 Unless you want to do something more flexible, leave things to the back-end. 除非你想做一些更灵活的事情,否则将事情留给后端。

When do json, you gotta parse string into json object and then loop through and generate html, that's just an extra step. 什么时候做json,你必须将字符串解析为json对象,然后循环并生成html,这只是一个额外的步骤。 Keep things simple, plus, parsing string could be costly. 保持简单,加上解析字符串可能代价高昂。

If you don't loop through the list and instead do out.println the list object, you would likely see the address. 如果你不循环遍历列表而是在列表对象中执行out.println ,则可能会看到该地址。 also, you need to generate html, so: 另外,你需要生成html,所以:

StringBuilder sb = new StringBuilder();
for(String option: options){
sb.append("<li>").append(option).append("</li>");
}
out.println(sb);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM