- Principles of coding issues
- The English alphabet does not appear garbled, all encoding tables contain Unicode encoding tables
- Character stream = byte stream + encoding table using the Tomcat default encoding table when using Tomcat server
- tomcat6.x tomcat7.x default is all iso-8859-1 tomcat8.x default use UTF-8
- In JSP programming, GetByte (); The default is to use the ANSI local encoding table GBK
- GetByte () has a construct with an encoded table that specifies what encoding the character will use to return a byte array
- HttpServletResponse
- getoutputstream and getwriter () cannot be used together????
These two streams do not need to be released manually, the servlet will automatically release????
response.getoutputstream (). Write ( "Chinese garbled" .getbytes ());
response.getwriter (). Print ( "Chinese garbled" )
- Common headers
- Refresh Response.setheader ("Refresh", "1"), refresh once a second
- Sets the specified time redirection response.setheader ("Refresh", "3; Url=/index.jsp "); URL can be omitted
Forwarding:/representing the current application
- Only one request, shared request
- Use forwarding when you need to share request
- The address bar does not change
- Internal behavior of the server
- Only internal addresses can be forwarded
Implement page jump by getting context object/represent the current project cannot use relative path
Getservletcontext (). Getrequestdispatcher ("/index.jsp"). Forward (request, response);
Get Request object, implement page jump
Request.getrequestdispatcher ("/test.html"). Forward (request, response);
Redirect:/represents the Tomcat root directory
-
-
- You can forward internal addresses, or you can access external resources
- do not write/represent relative paths
< Span style= "color:black; Font-family: Song body; Font-size:10pt ">//uses relative paths when pointing to is the current class path
response.sendredirect" myServlet_ OutputStream ");
response.setheader ("Refresh", "3; Url= class file in the current type Space ");
- Buffer Time Response.setdateheader ("Expires", System.currentimemillis () +1*1000*60*60); /1 hours
The Address bar direct enter key is used when the cache needs to be refreshed to update the page
A static web resource can be cached
Dynamic Web resources are not recommended for use with cached
Control without buffering
// Control No Cache
Response.setheader ("Expires","0");
Response.setheader ("Cache-control","No-cache");
Response.setheader ("Pragma","No-cache");
- the solution of character stream garbled
- Modify Browse parsing encoding
- Tell the browser that you are using UTF-8, and that byte arrays are also UTF-8 (the following three types) of bytes output
- Write the character label directly response.getoutputstream (). write (); è not recommended for use
Response.getoutputstream (). Write ("<meta http-equiv=\" content-type\ "content=\" text/html; charset=UTF-8\ " > ". getBytes ());
Response.getoutputstream (). Write (" Chinese garbled ". GetBytes ("UTF-8"));
When the header tag is in use, some browsers will directly recognize it as a string display.
-
response.setheader ( "Content-type" "Text/html;charset=utf-8"
response.getOut Putstream (). Write ( " Chinese garbled " .getbytes ( "UTF-8"
- directly set the value within the label Response.setcontenttype (); è recommended use of
response.setcontenttype (" Text/html;charset=utf-8 "
response.getOut Putstream (). Write ( " Chinese garbled " .getbytes ( "UTF-8"
- A solution to the character stream garbled
a , write the label directly in the character stream can not be used, the rest are available, recommended as follows!
Response.setcontenttype ("Text/html;charset=utf-8");
response.getwriter (). Print (" Chinese garbled d");
- HttpServletRequest
- Method of Request
- Java.lang.String Getcontextpath ( Apply name
- ???? Java.lang.String GetMethod () Gets the request method, gets the uppercase
- ???? Java.lang.String Getquerystrin () Gets the requested data
- ???? Java.lang.StringBufferget Requesturl () The access path to the current project, it is recommended to use ServletContext Getrealpath instead of
- ???? Java.lang.String Getrequesturi () Gets the access path that does not include the host name Port number
- ???? Getrealpath () Gets the absolute path
- ???? Getprotocol () Get protocol and version
- ???? GETREMOTEADDR () Gets the IP address of the caller
- Get form data (the most common method)
- Gets the form's value Request.getparameter ("") according to the form's name; Get a single value
- Gets the value array of the form based on the name of the form request.getparametervalues ("") is generally used for checkboxes and radio
- Gets all the name Request.getparameternames () in the form;
- Get duplicate form name can only get to the first value retrieved
- CheckBox and Radio if not selected, the client does not send data to the server
- Capture the internal mechanism of the data (reflection principle)
User user = new User ();
???????? // How much data does the form submit?
???????? enumeration<string> e = request.getparameternames ();
???????? While (e.hasmoreelements ()) {
???????????? // get each form's name
???????????? String fieldName = E.nextelement ();
???????????? String fieldvalue = Request.getparameter (fieldName);
????????????
???????????? try {
???????????????? // Introspection
???????????????? PropertyDescriptor PD = new propertydescriptor (fieldname,user. class );
???????????????? Method m = Pd.getwritemethod (); // that gets the specified field. Set method
???????????????? M.invoke (user, fieldvalue);
????????????} catch (Exception E1) {
???????????????? // TODO auto-generated catch block
???????????????? E1.printstacktrace ();
????????????}
????????????
????}
- A simple method of object encapsulation ( use apache
beanutils.populate (User, Request.getparametermap ());
import org.apache.commons.beanutils.BeanUtils;
can automatically get the key and value and encapsulate it into a given object, the principle is inside the province
- Chinese garbled question
get Way: username= new String ( Username.getbytes ("Iso-8859-1"), "UTF-8"); Use to return to the original form
using Getinpustream () must use post for file uploads!
- Jump and include
// Jump request.getrequestdispatcher ("/register.html"). Forward (request, response);
// include request.getrequestdispatcher ("/register.html"). Include (request, response);
HttpServletResponse and HttpServletRequest