I have created a servlet like this given below:
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>");
}
}
Since the image is dynamically generated(i.e decrypted)I can't save it as a file in filesystem.So I used OutputStream
and response.setContentType("image/png")
for displaying image.
But I also need to generate a input and button using html.So I used another response.setContentType("text/html")
for displaying html.
My problem is I am getting error which says java.lang.IllegalStateException: getWriter() has already been called for this response
.So the content type response are conflicting each other.
Pls help me to resolve this error!!!