1

我的 sql 中有一个图像,我想在网页中检索它,所以我开发了一个 servlet。现在,当我点击提交按钮时,它会调用 servlet,但不会显示图像,但如果我正在编写 localhost:8080/y/testimageServlet,则会在浏览器中显示图像。

imagetestServlet.java

import java.sql.*;
import DB.DataBaseConnection;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class imagetestServlet
 */
@WebServlet("/imagetestServlet")
public class imagetestServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public imagetestServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response) 
              throws IOException,ServletException {

                 Blob image = null;
                  Connection con = null;
                  Statement stmt = null;
                  ResultSet rs = null;
                  DataBaseConnection db= new DataBaseConnection();
                  ServletOutputStream out = response.getOutputStream();
                  try {
                  Class.forName("com.mysql.jdbc.Driver");
                  con=db.connet();
                  stmt = con.createStatement();
                  rs = stmt.executeQuery("select img from one where  id = '4'");
                  if (rs.next()) {
                  image = rs.getBlob(1);
                  } else {
                  response.setContentType("text/html");

                  out.println("<font color='red'>image not found for given id</font>");

                  return;
                  }
                  response.setContentType("image/gif");
             InputStream in = image.getBinaryStream();
              int length = (int) image.length();
              int bufferSize = 1024;
              byte[] buffer = new byte[bufferSize];
              while ((length = in.read(buffer)) != -1) {
              out.write(buffer, 0, length);
              }
              in.close();
              out.flush();

            } catch (Exception e) {
              response.setContentType("text/html");
              out.println("<html><head><title>Unable To Display image</title></head>");
              out.println("<body><h4><font color='red'>Image Display Error=" + e.getMessage() +
               "</font></h4></body></html>");
              return;
              } 
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

登录

<form id="f1" name="f1" action="imagetest" method="post" onsubmit="return ccheck()">

            <img  src="header.png"><br><br><br><br><br>
    <table >
        <tr>
            <td>
                <table class="table">
                    <tr>
                        <td>
                            <table class="table">
                                <tr>
                                    <td>
                                        <div class="content"><div class="signin-header"><h3>Welcome to DiaEmr</h3></div></div>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <div class="content">
                                        <div class="signin-box">
                                        <p class="one">
                                                Welcome to all at the <b>"Workshop on Ileal Interposition".</b><br>

                                                <b>Brazil</b> to inaugurate & launch this very important Data Registry<br>
                                                Key features of the solution-<br>
                                        </div>  
                                        </div>
                                    </td>
                                </tr>
                            </table>
                        </td>
                        <td>image</td>
                        <td>
                            <table class="table" >
                                <tr>
                                    <td>
                                        <div class="content">
                                        <div class="signin-header">
                                        <h3>Portal Login</h3>
                                        </div>
                                        </div>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <div class="content">
                                            <div align="center" class="signin-box">
                                                <table class="table">
                                                    <tr>
                                                        <td><b>User ID</b></td>
                                                        <td><input name="uid" type="text" /></td>
                                                    </tr>
                                                    <tr >
                                                        <td><b>Password</b></td>
                                                        <td><input name="cpass" type="password" /></td>
                                                    </tr>
                                                    <tr >
                                                        <td><input type="submit" 
                                                                class="button button-submit" value="Submit" /></td>
                                                        <td><input type="reset" class="button button-submit"
                                                                value="Reset" /></td>
<tr>New User<a href ="UserRegistration.jsp">Register</a></tr><br>
                                                        </tr>


                                                    </table>
                                                </div>
                                            </div>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
            </table>
        </td>
        </tr>
</table>

<div class="footer-bar">
    <img align="left" src="footer.png">
</div>
</form>
4

1 回答 1

1

您的表单发送post请求,

<form id="f1" name="f1" action="imagetest" method="post" onsubmit="return ccheck()">

但是您只在您的 servlet 中定义了一个 doGet 方法。如果您在浏览器中导航GETPOST

您将需要更改表单以使用或在方法get中实现您的代码doPost

于 2013-04-05T07:22:37.040 回答