这是我的 servlet,我成功地从数据库中读取数据并将其显示在浏览器中,但我想以 json 格式显示这些数据,谁能告诉我
package com.file;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GetDataTable extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
res.setContentType("application/json");
PrintWriter pw = res.getWriter();
Connection con;
Statement stmt;
ResultSet rs;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:java","java","java");
stmt = con.createStatement();
rs = stmt.executeQuery("Select * from SERIESCHART");
pw.println("DATA "+ "VALUE" + "<br>");
while(rs.next())
{
pw.println(rs.getString(1)+" "+rs.getString(2) + "<br>");
}
}
catch (Exception e){
pw.println(e);
}
}
}