Use Printwrite to error in jetty Java.lang.IllegalStateException:STREAM

Source: Internet
Author: User

1  Public voidDoFilter (servletrequest request, servletresponse response,2Filterchain chain)throwsIOException, servletexception {3         //TODO auto-generated Method Stub4HttpServletRequest req=(httpservletrequest) request;5HttpServletResponse res=(httpservletresponse) response;6HttpSession session=req.getsession ();7String URL =Req.getrequesturi (). toString ();8 Chain.dofilter (req, res);9String str = "Docheck";Ten         if(!url.endswith ("Login")){ One             if(!url.contains (str)) { A             if(Session.getattribute ("managerid") = =NULL){ -PrintWriter out =Res.getwriter (); -                     //Servletoutputstream out = Res.getoutputstream ();  theString loginpage = "/manageraction/login";  -StringBuilder Builder =NewStringBuilder ();  -Builder.append ("<script type=\" text/javascript\ ">");  -Builder.append ("window.location.href="));  + builder.append (LoginPage);  -Builder.append ("';");  +Builder.append ("</script>");  A Out.print (builder.tostring ()); at out.close (); -             }  -             } -         } -}

Be tortured by this problem is very uncomfortable, although it does not affect the normal use of the program, but still to solve, consult the relevant information, this is the Web container generated by the servlet code in the Out.write (""), this and call Response.getoutputstream () Create a conflict. That is, the servlet specification description, cannot call Response.getoutputstream (), and call Response.getwriter (), regardless of which one is called first, should be thrown at the second time of the call IllegalStateException.

After the printwriter changed to OutputStream, still unsuccessful, will error display Java.lang.IllegalStateException:WRITER

Occasionally I will outputstream changed to Servletoutputstream unexpectedly not error.

The difference between PrintWriter and Servletoutputstream

1. PrintWriter is the processing of all information in characters, and servletoutputstream only binary data.
2. PrintWriter in the output word literals, the internal need to convert the string into a character set encoding byte array, using his advantage is that you do not need to complete the conversion from string to byte array. The character set encoding of the conversion is implemented by means of setting Setcontenttpye or setcharacterencoding or setlocale, and is read directly from a byte input stream using a Servletoutputstream object. And then output to the customer service side intact.
3. The two methods are mutually exclusive, only one can be invoked, and if so, flush () is called before the method is swapped, and the buffer data is flushed out.
4. The PrintWriter auto-empty buffer function is enabled (AutoFlush in the constructor is true), and the buffer is cleared automatically only when the println () method is called, rather than a newline character that is encountered as PrintStream.

As for the OutputStream and Servletoutputstream output mode, one is write (byte ") and the other is print (StringBuilder) What is the difference between the detailed study.

Use Printwrite to error in jetty Java.lang.IllegalStateException:STREAM

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.