[英]displaying Java output in a web browser
我有以下Java代碼:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class License {
public static void main(String[] args) {
Connection connection = null;
ResultSet resultSet = null;
Statement statement = null;
try {
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:C:/license_tracker/usage.db3");
statement = connection.createStatement();
System.out.println("Connection Successful!!!!");
String query = "SELECT USR.id,USR.name, ST.license FROM users USR, status ST, upd_ate UD WHERE UD.upt_id = (select max(p2.upt_id) from upd_ate p2) AND ST.id = USR.id AND ST.upt_id = UD.upt_id ORDER BY ST.license,USR.name";
resultSet = statement.executeQuery(query);
while (resultSet.next()) {
System.out.printf("%8s %-50s %-7s%n",resultSet.getString("cymer_id"), resultSet.getString("name"), resultSet.getString("license"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
resultSet.close();
statement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
我想以表格格式在Web瀏覽器(本質上是IE9)中顯示resultSet的內容。 我已經搜索過這個問題,並且我認為有兩種選擇,使用JAVA applet或JSP。 由於我必須在服務器上運行代碼,因此我應該使用JSP嗎?我對JSP不太熟悉,因此,如果有人可以幫助我,我將不勝感激。 我正在使用Eclipse Indigo和Tomcat ver 7。
我在網頁布局上有一個html代碼。 我想把信息放在桌子上。
<!DOCTYPE html>
<html>
<head>
<!-- FORMAT OF THE BOXES //-->
<style type="text/css">
div.ex
{
width:360px;height:300px;
padding:10px;
border:5px solid gray;
margin:0px;
background-color:linen;}
div.graph
{
width:800px;height:300px;
padding:10px;
border:5px solid gray;
margin:0px;
background-color:linen;}
</style>
</head>
<body>
<!--h1=> LOGO & h2=> Heading //-->
<h2 style="text-align:center;font-family:century schoolbook;color:black";><b>Pro/Engineer And Intralink License Logs</b></h2>
<!-- License summary bar //-->
<div id="content" style="background-color:mediumpurple;clear:both;text-align:center;"><big>License Summary</big></div>
<!-- TABLES //-->
<center>
<table cellspacing="5" border="0" cellpadding="0">
<td><p><div class="ex"><center>DESIGNER LICENSES</center><br/></p></td>
<td width="2" bgcolor=black><BR></td>
<td>
<p><div class="ex"><center>LEAD LICENSES</center><br/></p>
</td>
<td width="2" bgcolor=black><BR></td>
<td>
<p><div class="ex"><center>CABLING LICENSES</center><br/></p>
</td>
<td width="2" bgcolor=black><BR></td>
<td>
<p><div class="ex"><center>LICENSES</center><br/></p>
</td>
</table>
</center>
<!-- GRAPH SPACE //-->
<p><div class="graph"><center>GRAPHICAL REPRESENTATION</center></div></p>
</body>
<hr/>
<center><style=padding:100px;>
<!-- FOOTER //-->
<footer>
<div id="donotremove">
<abbr title="License Availability (v1 on 32)">License Availability</abbr> © 2011-2351 All rights reserved.</a>.</div></footer>
</center>
</html>
也許您可以執行以下操作:
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LicenseServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.print("<html><body><table>");
// ... pseudocode
while (resultSet.next()) {
out.print("<tr><td>");
out.print("%8s %-50s %-7s%n",resultSet.getString("cymer_id"), resultSet.getString("name"), resultSet.getString("license"));
out.print("</td></tr>");
}
out.print("</table></body></html>");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.