如何在浏览器中显示由Servlet提供的信息,而不是在控制台中显示?

huangapple go评论63阅读模式
英文:

How to show information given by the servlet in the browser instead of the console?

问题

我在Eclipse中创建了一个Servlet,并在Tomcat服务器上运行它。该Servlet使用用户名和API令牌从API获取一些数据。

我创建了一个名为index.jsp的文件。它有一个导航栏和一些按钮。

现在,当我在我的index.jsp中点击一个按钮时,我正在调用Servlet。点击按钮时,控制台会显示JSON数据,但在Tomcat上运行的浏览器上没有显示任何内容。

理想情况下,我希望浏览器能够显示JSON数据。是否有人能够帮我提供一些代码?

这是我在index.jsp中调用服务器的按钮部分:

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
      function callServlet() {
          document.forms[0].action = "testing";
          document.forms[0].submit();
      }
  </script>
</head>

<body>
..
.
.
<form id="WhatsNewFormID" name="WhatsNewForm" method="post">
  <div id="WhatsNewBtn"><input id="btn" type="submit" value="Hello World" style="float:right" class="w3-button w3-bar-item" onclick="callServlet();" /></div>
  </form>
  .
  .
  
 </body>

以下是我的Servlet的一部分代码:

String theUrl = "TheURLForTheApiZZZ";
URL url = new URL(theUrl);
// create a urlconnection object
URLConnection urlConnection = url.openConnection();

String userpass = "usernameXXX" + ":" + "apitokenYYY"; 
String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes());
urlConnection.setRequestProperty("Authorization", basicAuth);
 
// wrap the urlconnection in a bufferedreader
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

String line;

// read from the urlconnection via the bufferedreader
while ((line = bufferedReader.readLine()) != null) {
    content.append(line + "\n");
}
bufferedReader.close();
}

output = content.toString();
System.out.println(output);
英文:

I have made a servlet in Eclipse and am running it on Tomcat server. The servlet is getting some data from an API using username and apitoken.

I have made an file namely index.jsp. It has a navigation bar and few buttons.

Now I am calling the servlet when clicking one button in my index.jsp. On click of the button- the console displays the JSON data and nothing on the browser running on Tomcat.

Ideally I want the browser to display the JSON data on the browser. Would anybody be able to help me with some coding?

This is my button calling the server in index.jsp:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
      function callServlet() {
          document.forms[0].action = &quot;testing&quot;;
          document.forms[0].submit();
      }
  &lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
..
.
.
&lt;form id=&quot;WhatsNewFormID&quot; name=&quot;WhatsNewForm&quot; method=&quot;post&quot;&gt;
  &lt;div id=&quot;WhatsNewBtn&quot;&gt;&lt;input id=&quot;btn&quot; type=&quot;submit&quot; value=&quot;Hello World&quot; style=&quot;float:right&quot; class=&quot;w3-button w3-bar-item&quot; onclick=&quot;callServlet();&quot; /&gt;&lt;/div&gt;
  &lt;/form&gt;
  .
  .
  
 &lt;/body&gt;

<!-- end snippet -->

And this is a part of my servlet:

String theUrl=&quot;TheURLForTheApiZZZ&quot;;
	      URL url = new URL(theUrl);

// create a urlconnection object
URLConnection urlConnection = url.openConnection();

 String userpass = &quot;usernameXXX&quot; + &quot;:&quot; + &quot;apitokenYYY; 
			  String basicAuth = &quot;Basic &quot; + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes());
			  urlConnection.setRequestProperty (&quot;Authorization&quot;, basicAuth);
			 
	      
	      // wrap the urlconnection in a bufferedreader
	      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

	      String line;

	      // read from the urlconnection via the bufferedreader
	      while ((line = bufferedReader.readLine()) != null)
	      {
	        content.append(line + &quot;\n&quot;);
	      }
	      bufferedReader.close();
	    }
	    catch(Exception e)
	    {
	      e.printStackTrace();
	    }
	    
	 output= content.toString();
	 System.out.println(output);

答案1

得分: 0

你已经忽略了向响应对象(HttpServletResponse response)中写入内容,该对象负责将内容发送到浏览器。就在System.out.println(output)这行的前面,将以下代码放入你的Servlet中:

response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.write(output);

如果你不想在控制台上显示内容,只需删除这行代码:System.out.println(output);

英文:

You have missed writing the content to the response object (HttpServletResponse response) which is responsible for sending the content to the browser. Just before the line, System.out.println(output), put the following code in your servlet:

response.setContentType(&quot;application/json&quot;);
response.setCharacterEncoding(&quot;UTF-8&quot;);
PrintWriter out = response.getWriter();
out.write(output);

If you do not want to display the content on the console, simply remove the line, System.out.println(output);

huangapple
  • 本文由 发表于 2020年8月29日 05:44:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63641219.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定