英文:
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 -->
<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>
<!-- end snippet -->
And this is a part of my 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();
}
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("application/json");
response.setCharacterEncoding("UTF-8");
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论