在Tomcat中为.war文件在HTML文件中引用servlet。

huangapple 未分类评论45阅读模式
英文:

Referencing servlet in HTML file for .war file in Tomcat

问题

我已经为部署在Tomcat上的Java(.war)应用程序创建了一个index.html文件。

我已经有一个Java Servlet文件,用于查询远程数据库服务器中存储的图片。

Java Servlet文件名为ImageServlet.java。

我需要HTML页面显示ImageServlet.java查询到的图片。

我只是不知道如何引用这个Servlet,因为它会检索我的图片。

以下是ImageServlet文件的内容:

package test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.DatatypeConverter;

public class ImageServlet extends HttpServlet {
  Connection con = null;

  public Connection getConnection() {
    // ... // 在这里是获取数据库连接的代码
  }

  public static byte[] getImageById(Connection con) {
    // ... // 在这里是查询并获取图片数据的代码
  }

  public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
    PrintWriter out = res.getWriter();

    Connection con = null;
    con = getConnection();
    byte[] data = getImageById(con);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    baos.write(data);

    out.println("<!DOCTYPE html><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title>PI_SQLogo.png</title></head><body><img src='data:image/png;base64,"
      + DatatypeConverter.printBase64Binary(baos.toByteArray()) + "'></body></html>");
    out.close();
  }
}
英文:

I have created an index.html file for a Java (.war) application that I deployed on tomcat.

I already have a Java Servlet file that queries a remote db server for a picture i stored there.

The java servlet file is called ImageServlet.java.

I need the HTML page to display that image that the ImageServlet.java queries.

I just don't know how I'm able to reference to the servlet as it retrieves my picture.

Below is the ImageServlet file:

package test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.DatatypeConverter;

public class ImageServlet extends HttpServlet
{
  Connection con = null;

  public Connection getConnection()
  {
    try {
      Class.forName(&quot;com.mysql.jdbc.Driver&quot;);
      String url = System.getenv(&quot;FUJI_MYSQL_URL&quot;);
      return DriverManager.getConnection(url,             
System.getenv(&quot;FUJI_MYSQL_USER&quot;), System.getenv(&quot;FUJI_MYSQL_PASS&quot;));
    }
    catch (Exception ex)
    {
      System.out.println(ex.getMessage());
    }return null;
  }

  public static byte[] getImageById(Connection con)
  {
    String query = &quot;select photo from photos where picid = 1&quot;;
    try
    {
      PreparedStatement stmt = con.prepareStatement(query);

      stmt.execute();
      ResultSet resultSet = stmt.getResultSet();
      resultSet.first();
      Blob blob = resultSet.getBlob(1);
      stmt.close();
      con.close();
      return blob.getBytes(1L, (int)blob.length());
    } catch (Exception ex) {
      System.out.println(ex.getMessage());
    }return null;
  }

  public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
  {
    PrintWriter out = res.getWriter();

    Connection con = null;
    con = getConnection();
    byte[] data = getImageById(con);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    baos.write(data);

    out.println(&quot;&lt;!DOCTYPE html&gt;&lt;html xmlns=\&quot;http://www.w3.org/1999/xhtml\&quot;&gt;&lt;head&gt;&lt;title&gt;PI_SQLogo.png&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&lt;img src=&#39;data:image/png;base64,&quot; + DatatypeConverter.printBase64Binary(baos.toByteArray()) + &quot;&#39;&gt;&lt;/body&gt;&lt;/html&gt;&quot;);
    out.close();
  }
}

huangapple
  • 本文由 发表于 2020年4月6日 23:18:28
  • 转载请务必保留本文链接:https://java.coder-hub.com/61063101.html
匿名

发表评论

匿名网友

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

确定