0
votes

the process is controller(jump) -> jsp(grid) ->controller(Verification code) so this is my code
controller(jump):

/**
 * jump to login in jsp
 * @return
 */
@RequestMapping("/toLogin")
public String toLogin( ) {
    log.debug("to Sign In!---------------------------------------<UserinfoController>");
    return "sign-in";
}

jsp(there is no tag like this "<% ... %>"):

                    <div class="form-group">
                        <label for="code">验证码</label>
                        <input id="code" type="text" name="code" class="span3 form-control ">
                        <a>
                            <img alt="刷新验证码" src="${pageContext.request.contextPath }/code">
                        </a>
                    </div>

controller(Verification code):

    OutputStream os = resp.getOutputStream();  
    ImageIO.write(buffImg, "jpeg", os);
    os.close();

error is :

java.lang.IllegalStateException: getWriter() has already been called for this response at org.apache.catalina.connector.Response.getOutputStream(Response.java:644) at org.apache.catalina.connector.ResponseFacade.getOutputStream(ResponseFacade.java:196) at com.wyk.pmsys.controller.CodeController.getCode(CodeController.java:107) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

in servlet jsp convert to java used "out.write()",is this confilct with my code for "resp.getOutputStream()"? that's all ,how to solve it; if something else,what's that ? and how to solve it?

1
The problem is your jsp is already written to the output stream. Your controller method needs to set content type to jpeg and only return the imagesturcotte06
i have already set content type like this resp.setHeader("Pragma", "no-cache"); resp.setHeader("Cache-Control", "no-cache"); resp.setDateHeader("Expires", 0); resp.setContentType("image/jpeg");wykCN
how return the image don't use response.getOutputStream()?,the question is if i use the response.getOutputStream() in my controller it will get an error in my code.so i can't write image to jsp.wykCN
You need to separate the two requests. One for the jsp, one for the image.sturcotte06
yes there is two controller the one is "to jsp" and the other one "to write image",sorry i'm not post complete code in "controller(Verification code):"wykCN

1 Answers

0
votes

All you need is annotation ResponseBody for the method:

@RequestMapping("/code")
@ResponseBody //<---- here
public void getCode(HttpServletRequest req, HttpServletResponse resp)  
        throws IOException {