The Request/respone of Javaweb (Iv.)

Source: Internet
Author: User

Servletresponse ObjectBrief introduction
today is javaweb the third day, the code is a bit more. Today we are learning two objects in the servlet engine, and perhaps today's theoretical knowledge cannot be mastered. I want to say how to knock the code, there will be a harvest. The learning principle is the same, keep in mind the knowledge point and the small case application, the constant practice code. Foundation to play solid, to face the next study ah. Not much to say, practice ~ to present oneself to say a word, refuels ~
1. Servletresponse Introduction
1). The secondary servlet sends the response content to the client's object. The servlet container creates the Servletresponse object and passes it as an argument to the Servlet's service Method 2). HTTP protocol Response message Header---> Object-oriented Developer---> encapsulates all the response message headers and body--->servletresponse (protocol Independent)--->httpservletresponse ( Protocol-related) when the user first requests, the server generates a response object, and a new object is generated on the second visit
2. How to send the body to the client
2.1 How to get the byte stream 1)//Get used to send the word stream object OutputStream os = Response.getoutputstream ();//Send bytes Flow os.write ("Hello". GetBytes ());// Output Chinese problem, output Chinese by default no garbled, can change the server output data encoding format//See garbled, because the client also uses GBK display, so to use code adjustment, the client display data encoding <meta http-equiv= "Content-type" context= "Text/html;charset=utf-8" >response.setheader ("Content-type", "text/html;charset=utf-8"); Response.setcontenttype ("Text/html;charset=utf-8");//The best way to solve server control browser display data using what coding os.close (); no need to write, Because the servlet engine will automatically turn off 2) How to solve the Chinese garbled//First solution: META tag//os.write ("<meta http-equiv= ' content-type ' content= ' text/html; CHARSET=GBK ' > '. getBytes ());//The second solution: the use of response object to add the response message header//response.setheader ("Content-type", "text/html; Charset=utf-8 ");//The Third solution: setContentType (); Response.setcontenttype (" Text/html;charset=utf-8 ");(recommended) Summary: What encoding the browser uses to write out the data, must be corresponding to the declaration header encoding format 2.2 How to output a character stream printwriter pw = Response.getwriter (); The default output data uses Iso-8859-1 to change the encoding of the server's output data to the client: response.secharacterencoding ("UTF-8"), and the notification client to display the same encoding Response.setcontenttype ("Text/html;charset=utf-8"); Summary: For the server output character stream data is, as long as the addition of response.setcontentType ("Text/html;charset=utf-8"), there will be no garbled problem, because there are two effects 1. Tells the browser what encoding 2 is displayed for the data. What encoding is used for server output character data
3. URL notation Summary
URL Writing Summary: For the server, do not need to write the application name Getservletcontext (). Getrealpath ("/web-inf/classes"); The application name to be written to the browser <a href= "" >location <link href= "><script src=" >

ServletRequest Object1. ServletRequest Introduction
1. It is an object provided by the server that encapsulates the client request information and provides Servlet2. Sub-interface HttpServletRequest It is related to the Protocol 3. Why use it? As an object-oriented developer, the idea is to encapsulate client 4 with a HttpServletRequest object. What happens when this object (its life cycle) is generated each time a request is requested, the server generates a new Request object
2. Methods used to get header information
GetHead (name): Gets the value that the client sends over the request header according to the specified name accept-encoding the request header is used to inform the server what data compression format the client can receive Getheaders (String name) according to the specified name , gets the set of values that the client sends over the request header Getheadernames () is used to get the client to send over the request header all values string clientip = Request.getremoteaddr ();//Get Client ipstring ClientHost = Request.getremotehost (); String uri = Request.getrequesturi ();//resource path StringBuffer URL = request.getrequesturl ();//protocol + host address + resource address + Port number System.out.println (ClientIP + ">>" + clienthost + ">>" + uri + ">>"); System.out.println ("protocol + host address + resource address + port number:" +url.tostring ());//It is possible to apply a name different from the project name System.out.println ("Get the app name dynamically:" + Request.getcontextpath ());//Dynamically get the application name System.out.println ("The port number used to get the client this request:" +request.getremoteport ());// Port number will be different on each request
3. Request Parameters
<input type= "text" name= "username"/>string value = Request.getparameter ("username");//Receive a value for the specified name// A set of values for receiving a specified name string[] getparametervalues (java.lang.String name) enumeration<string> Paramnames = Getparameternames ();//returns all the request parameter names in a form map map = Getparametermap ();//Returns a map collection Key:paramname value:paramvalues in order to simplify the encapsulation parameter Number of processes? Introduce a framework BeanUtils1. Use of the Framework 1.1 introduction JAR (Commons-beanutils--xxx.jar,commons-logging-xxx.jar) 2. Using Beanutils.setproperty ( Bean,propertyname,propertyvalue) 3. Garbled processing (post,get) 1.post mode request.setcharacterencoding ("UTF-8") in the request process; 2.get mode String Usernamevalue = Request.getparameter ("username");//username is to take the value of the text box's Name property as Byte []OLDSTR =
 Usernamevalue.getbytes ("iso-8859-1");//revert back to Usernamevalue = new String (oldstr, "UTF-8"); 

 

Small Knowledge Point literacy
*serializable is designed to implement this interface, meaning that the class can be serialized * implements this interface, which means that the object can be transferred directly on the network * * What is serialization? It supports one-time depositing of an object into a file * Support Objects for network transmission * * javabean* 1. is a public class * 2. Provide private fields * 3. For these private fields, the getter is provided with setter* 4. Provides a public non-parametric construction * 5. General implementation of Serializable interface

The Request/respone of Javaweb (Iv.)

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.