如何在HttpServletResponse中解析对象?

时间:2014-12-14 00:15:41

标签: java google-app-engine servlets jdo

当我尝试在http响应中编码对象时,我遇到了问题。我不知道怎么做。我会用标题吗?

public class Download extends HttpServlet{


    public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException{


        PersistenceManager pm = PMF.get().getPersistenceManager();

        String method = req.getParameter("method");

        if(method.equals("view")){
            Query query = pm.newQuery(Article.class);
            List<Article> articles=null;

            try {
                articles=(List<Article>) query.execute();
                }
                finally {
                query.closeAll();
                }
                Article article= art.get(0);
                res.setContentType("application/octet-stream");//??
                //problem here



        }
    }

}

1 个答案:

答案 0 :(得分:0)

HttpServletResponse类上有一个setHeader()方法。例如,您可以使用以下语句设置内容类型:

response.setHeader("Content-Type", "text/html"); 

以下链接提供了有关该主题的优秀教程:http://tutorials.jenkov.com/java-servlets/httpresponse.html

如果您需要更多参数,这是该类的JavaDoc:

http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponse.html