我已经创建了一个如下所示的servlet:
def tqdm_redirect(*args, **kwargs):
with redirect_to_tqdm():
for x in tqdm.tqdm(*args, **kwargs):
yield x
for i in tqdm_redirect(range(20)):
time.sleep(.1)
print(i)
由于图像是动态生成的(即解密的),我无法将其保存为文件系统中的文件。所以我使用import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class otpcheck extends HttpServlet {
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out =response.getWriter();
Crypting c=new Crypting();
BufferedImage imgKey;
BufferedImage imgEnc;
imgKey = ImageIO.read(new File("E:/Key.png"));
imgEnc=ImageIO.read(new File("E:/E.png"));
response.setContentType("text/html");
out.println(
"<!DOCTYPE html>\n" +
"<html>\n" +
" <head>\n" +
" \n" +
" <title>Online Banking</title>\n" +
" \n" +
" <link rel=\"stylesheet\" href=\"newcss.css\">\n" +
" </head>\n" +
"<!DOCTYPE html>\n" +
"\n" +
" <div class=\"wrapper\">\n" +
" \n" +
" <div class=\"header\">\n" +
" <img src=\"header_1.jpg\" height=\"100%\" width=\"100%\"/>\n" +
" </div>\n" +
" <div class=\"navbar\">\n" +
" \n" +
" <ul>\n" +
" <li><a href=\"index.jsp\">Home</a></li>\n" +
" <li><a href=\"features.jsp\">Features</a></li>\n" +
" <li id=\"last\"><a href=\"contact.jsp\">Contact Us</a></li>\n" +
" </ul>\n" +
" </div>" +
"\n" +
"<div class='content'>\n" +
"<div class=\"user_login\">");
out.println("<p>Scan the QR Image to get OTP</p>");
BufferedImage imgDec=Crypting.decryptImage(imgKey,imgEnc);
response.setContentType("image/png");
OutputStream os=response.getOutputStream();
ImageIO.write(imgDec,"png",os);
out.println("<form action=\"otpvalidate\" method=\"POST\" enctype=\"multipart / form - data\">Enter OTP:<input type=\"password\" name=\"otp\"/>\n<input type=\"submit\" value=\"SUBMIT\" name=\"submit\" /></form >");
out.println("</div>\n" +
"</div>\n" +
"<!DOCTYPE html>\n" +
" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF- 8\">\n" +
" <div class=\"footer\">\n" +
"<ul>\n" +
" \n" +
" <div class=\"footer_content\">\n" +
" <li><a href=\"features.jsp\">Features </a></li>\n" +
" <li><a href=\"contact.jsp\"> Contact</a></li>\n" +
" \n" +
" <li><a href=\"safeonlinebanking.jsp\">Safe online Banking tips</a></li>\n" +
" \n" +
" <li style=\"padding-left:450px;\">Copyright©2017 onlinebanking.com</li>\n" +
" \n" +
" </div>\n" +
" </ul> \n" +
" \n" +
" \n" +
" \n" +
" </div>\n" +
" </div> \n" +
" </body>\n" +
"</html>\n" +
"\n" +
"</html>");
}
}
和OutputStream
来显示图像。
但是我还需要使用html生成一个输入和按钮。所以我使用了另一个response.setContentType("image/png")
来显示html。
我的问题是我收到的错误是response.setContentType("text/html")
。所以内容类型的响应相互冲突。
请帮我解决这个错误!!!
答案 0 :(得分:0)
在HTTP协议中,您可以访问资源。每种资源都有自己的类型。
在您的示例中,您至少有3个资源,一个用于HTML页面,一个用于key.png,另一个用于e.png
怎么办?
您可以让您的servlet仅为您的HTML页面提供服务,并且在HTML页面中您可以将图像标记指向图像的资源
<html>
...
<img src="http://miserver/key.png">
<img src="http://miserver/e.png">
对于图像,您可以将它们放在war文件中,服务器提供图像。
但是如果您的图像像数据库一样存储在战争之外,您可以创建其他服务于您图像的servlet。在IMG标记的SRC属性中,放置该servlet的URL。
答案 1 :(得分:0)
首先,谢谢大家的贡献。我找到了答案及其工作。
我创建了一个servlet来返回图像。然后我创建了一个jsp文件,通过将URL设置为<img src="${pageContext.request.contextPath}/otpcheck" />
来获取该图像。
而且它的工作性很好。
这是代码。
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class otpcheck extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Crypting c=new Crypting();
BufferedImage imgKey;
BufferedImage imgEnc;
imgKey = ImageIO.read(new File("E:/Key.png"));
imgEnc=ImageIO.read(new File("E:/upload/E.png"));
BufferedImage imgDec=Crypting.decryptImage(imgKey,imgEnc);
response.setContentType("image/png");
OutputStream os=response.getOutputStream();
ImageIO.write(imgDec,"png",os);
}
}
jsp如下:
<img src="${pageContext.request.contextPath}/otpcheck" />
<form action="otpvalidate" method="POST" enctype="multipart/form-data">
Enter OTP:<input type="password" name="otp"/><input type="submit" value="SUBMIT" name="submit" />
</form>
我提到了BalusC给出的一些答案。这就是我如何使用代码。