【问题标题】:Print from java to jsp从java打印到jsp
【发布时间】:2017-06-08 06:04:01
【问题描述】:

我正在尝试创建一个 Facebook oAuth 代码,该代码最终将在浏览器上打印用户的名字、姓氏、电子邮件等。这将使用 java 和 jsp 完成。所以这是我的java代码

public class MainMenu extends HttpServlet {

private static final long serialVersionUID = 1L;
private String code="";

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

    code = req.getParameter("code");
    if (code == null || code.equals("")) {
        throw new RuntimeException(
                "ERROR: Didn't get code parameter in callback.");
    }
    FBConnection fbConnection = new FBConnection();
    String accessToken = fbConnection.getAccessToken(code);

    FBGraph fbGraph = new FBGraph(accessToken);
    String graph = fbGraph.getFBGraph();
    Map<String, String> fbProfileData = fbGraph.getGraphData(graph);
    ServletOutputStream out = res.getOutputStream();
    out.println("<h1>Facebook Login using Java</h1>");
    out.println("<h2>Application Main Menu</h2>");
    out.println("<div>Welcome "+fbProfileData.get("first_name"));
    out.println("<div>Your Email: "+fbProfileData.get("email"));
    out.println("<div>You are "+fbProfileData.get("gender"));


}

}

我有一个空的 jsp 文件。

【问题讨论】:

  • 尝试使用response.setContentType("text/html"); PrintWriter out=response.getWriter();
  • getWriter 不适用于参数。(字符串)有什么想法吗?
  • 什么意思?
  • 我的意思是我收到了这个错误信息。
  • response.getWriter(); 不接受任何参数

标签: java jsp web


【解决方案1】:

是的,正如可怕的建议,尝试使用,

PrintWriter out = response.getWriter()

因为这基本上完成了 System.out.println 与 Java 中类似的工作。

【讨论】:

    【解决方案2】:

    使用HttpServletResponse res 来做到这一点:

    res.getWriter().write("<h2>Application Main Menu</h2>" +
                        "<h1>Facebook Login using Java</h1>" +
                        "<div>Welcome "+fbProfileData.get("first_name") +
                        "<div>Your Email: "+fbProfileData.get("email") + 
                        "<div>You are "+fbProfileData.get("gender"));
    res.getWriter().flush();
    res.getWriter().close();
    

    【讨论】:

    • getWriter 不适用于参数。
    猜你喜欢
    • 1970-01-01
    • 2019-03-05
    • 1970-01-01
    • 2016-08-04
    • 1970-01-01
    • 2012-04-23
    • 2010-12-05
    • 1970-01-01
    • 2012-05-13
    相关资源
    最近更新 更多