简体   繁体   English

使用PrintWriter对象将List对象从Java Servlet发送到JSP?

[英]Send a List object from a Java Servlet to JSP using the PrintWriter object?

Before proceeding I realise that there's a similar question ( Passing a List from Servlet to JSP ) from a couple of years ago. 在继续之前,我意识到几年前有一个类似的问题( 将列表从Servlet传递到JSP )。 I realise that it is possible to set the list I'm trying to pass as a session attribute but out of curiosity I wondered if it's possible to use the PrintWriter object to send the data back to the JSP page. 我意识到可以将要传递的列表设置为会话属性,但是出于好奇,我想知道是否可以使用PrintWriter对象将数据发送回JSP页面。

JSP JSP

<script type="text/javascript">
        function getEngineSchemes(engineID) {
            $.get('SchemeTypeServlet', {
                action: "getSchemes",
                engineID: engineID
            },
            function(data, status){

            }).fail(function() {
                alert("Error obtaining schemes for engine with engine id: " + engineID);
            });
        }
    </script>
</body> 

Servlet Servlet的

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //Set type of response
    response.setContentType("text/html");
    response.setCharacterEncoding("UTF-8");
    //get parameters that may be passed from client to determine which methods should be called
    String action = request.getParameter("action");

    if(action.equalsIgnoreCase("getSchemesForEngine")) { 
        Integer engineID = Integer.parseInt(request.getParameter("engineID"));
        List<String> schemeNames = getSchemeNamesForEngine(engineID);

        response.getWriter(). //insert code to send object back to JSP
        response.getWriter().flush();
    }       

Options 选项

One solution I think may be reasonable would be to create a JSONObject and have something along the lines of 我认为可能合理的一种解决方案是创建一个JSONObject并按以下方式进行操作:

response.setContentType("application/json");
JSONObject json = new JSONObject();
     for(String name : schemeNames) {
            json.put(engineID, name);
         }
    response.getWriter().write(json.toJSONString());
    response.getWriter().flush();

I haven't tested the above code but I just want to know if that seems like the best solution to my problem or if I'm making it overly complicated? 我还没有测试上面的代码,但是我只是想知道这似乎是解决我的问题的最佳方法,还是让我变得过于复杂? Maybe there's a far simpler solution to my question. 对于我的问题,也许有一个简单得多的解决方案。

I think you are having things messed up in your head. 我认为您的想法混乱了。 First of all, you cannot sent text or any kind of data from a servlet to a jsp. 首先,您无法从servlet向jsp发送文本或任何类型的数据。 The proper way to do this is to use the session and use response.sendRedirect or RequestDispatcher.forward methods. 正确的方法是使用会话并使用response.sendRedirectRequestDispatcher.forward方法。 But this will ignore any calls made before to the servlet's Writer Object. 但这将忽略对Servlet的Writer对象的任何调用。

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

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