如何响应JSP Servlet请求的自定义字符串

时间:2012-11-14 08:45:58

标签: jsp servlets

protected void doPost(HttpServletRequest request, 
            HttpServletResponse response) throws ServletException, IOException {
....
....
getServletContext().getRequestDispatcher("/output.jsp").forward(request, response);
}

我需要的是不要在doPost的末尾重定向到另一个JSP页面。我有一个字符串(平面文本),我需要响应它与Servlet请求。我怎么能这样做?

2 个答案:

答案 0 :(得分:4)

你可以这样做:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  out.append("Your string goes here");
  out.close();
}

答案 1 :(得分:0)

//getServletContext().getRequestDispatcher("/message.jsp").forward(request, response);
response.getWriter().println(output); // output is the custom string

可以从http://devzone.skillfusion.com/ajaxArticle8.php

中找到更多信息