Public voiddoget (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException {//get a byte stream objectServletoutputstream out=Response.getoutputstream (); //get a character stream objectPrintWriter writer =Response.getwriter (); out. Write ("Image". GetBytes ()); Writer.write ("<br/>text"); }
The result of the operation is abnormal, the exception information is as follows:
Java.lang.IllegalStateException:getOutputStream () have already been called for this response
Summarize:
Because the output of the server can use either a byte stream or a character stream, only one of the streams needs to be fetched in the actual development.
- If the first stream data is flushed directly before another stream is fetched, the data is pushed to the browser but continues to throw the above exception.
- If the first stream is closed directly before another stream is fetched, the data is pushed to the browser but continues to throw the above exception.
- It is important to note that requests and response objects are shared when the request is forwarded, so if you have already obtained the byte stream on the response object before forwarding it, you must not get the character stream again after forwarding.
- Be sure not to manually close any output stream objects of response before the request is forwarded, if closing the default response ends.
HttpServletRequest interface
The interface inherits from the ServletRequest interface. The object of this interface is primarily used to obtain data from a user's submission request.
The object of the interface is created by the Web container (TOMCAT) to pass directly to the service () method.
Common methods of ServletRequest interface
Get user Request parameters
String GetParameter (string name) ? Gets a single request parameter string[] Getparametervalues (string name) ? Gets multiple request values enumeration Getparameternames () ? Get all parameter names Map Getparametermap () ? Gets the Map collection object for all parameters
Storing and retrieving data in objects
void SetAttribute (String name, Object o) ? Set Property Object GetAttribute (String name) ? Get Properties
Specifies how to encode the user request parameter data
String getcharacterencoding () ? Set the code to get the value of the request parameter
Common methods of HttpServletRequest interface
Cookie[] GetCookies () ? Gets the cookie array httpsession getsession () ? Gets the Reply object string Getcontextpath () ? Get the mapping path for a Web site
GET Request Parameters
In real development, the main task of the request object is to get the various types of parameters that the user passes over. Here we get the various request parameters in different ways.
1 get a single request parameter
Public voiddoget (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException {//get a single request parameterString value = Request.getparameter ("uname"); //get a byte stream objectServletoutputstream out=Response.getoutputstream (); String Data="value="+value; //Output out. Write (Data.getbytes ()); }
2 Getting multiple values
Public voiddoget (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException {//get a single request parameterString value[] = request.getparametervalues ("likes"); //get a byte stream objectServletoutputstream out=Response.getoutputstream (); String Data="value="+arrays.tostring (value). toString (); //Output out. Write (Data.getbytes ()); }
3 Get all request data
Public voiddoget (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException {//an iterator that gets all the name of the request parameterenumeration<string> nas =Request.getparameternames (); //get a byte stream objectServletoutputstream out=Response.getoutputstream (); //Iteration while(Nas.hasmoreelements ()) {String name=nas.nextelement (); if("uname". Equals (name)) {String value=Request.getparameter (name); out. Write (name+"="+value). GetBytes ()); } out. Write ("<br/>". GetBytes ()); if("likes". Equals (name)) {string[] Value=request.getparametervalues (name); out. Write (name+"="+arrays.tostring (value)). GetBytes ()); } } }
4 Package Request Parameters
The user's request data in the General registration page is much more, if the servlet obtains the data directly, but the servlet itself cannot process the data that needs to be passed to the underlying business layer code for processing. If one pass is so cumbersome. Therefore, after obtaining the user's request parameters, it is necessary to properly marshal the data to pass to the next layer to process the data.
Javabeanàbeanutils
Public voiddoget (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException {//gets the map object for all requested parametersmap<string,object> map =Request.getparametermap (); //Get Set CollectionSet<map.entry<string, object>>Set=Map.entryset (); //get Iterator ObjectIterator<map.entry<string, object>> it =Set. iterator (); //Create a JavaBean objectUser User =NewUser (); //Loops while(It.hasnext ()) {Map.entry<string, object> entry =It.next (); String name=Entry.getkey (); Object value=Entry.getvalue (); Try{beanutils.setproperty (user, name, value); } Catch(Exception e) {System. out. println ("Package failed! "); E.printstacktrace (); } } //output The data of the user objectSystem. out. println (user); }
Java Learning Note-httpservletresponse