A JSP built-in object is a set of objects created by a Web container that can be used directly without using the New keyword. Use script implementation in the previous chapter to print out objects in the 99 multiplication table
<%--script: An Out object is an instance of the JspWriter class used to output content to the client--%><% for (int i = 1; I <= 9; i++) {
for (int j = 1; J <=i; J + +) { + "*" + j + "=" + (i*j) + " " ); } Out.println ("</br>"); } %>
JSP has nine large built-in objects
Common (5): Out, request, response, session, application
Infrequently used (4): page, PageContext, execption, config
Buffers (buffer)
The so-called buffer is an area of memory used to hold temporary data. , you can think of a grain of rice as a byte and think of the container of rice as a buffer
Out Object
An Out object is an instance of the JspWriter class that outputs content to the client
Common methods are as follows:
1. void println () prints a string to the client
2, void Clear () clears the contents of the buffer and throws an exception if called after flush
3. void Clearbuffer () clears the contents of the buffer if the call does not throw an exception after flush
4. void flush () outputs the buffer contents to the client
5, int getbuffersize () returns the buffer in the size of the number of bytes, or 0 if no buffer is set
6, int getremaining () returns how much of the buffer is remaining available
7, Boolean Isautoflush () returns when the buffer is full, is automatically emptied or throws an exception
8. void close () to close the output stream
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding= "Utf-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >Out.println ("); Out.println ("The Moon light <br> in front of the bed"); Out.println ("The Ground frost <br>"); Out.println ("Jutou Moon <br>"); Out.println ("The best place to think of <br>");%>Get buffer size:<%=out.getbuffersize ()%> size; <br>Get buffer Available size:<%=out.getremaining ()%> size; <br>whether the buffer is automatically emptied:<%=out.isautoflush ()%></body>Page Output results:
Flush is added to the middle of the silent Night think, although the page output is not affected, but the remaining buffers can be larger. Then try to run Out.clear () and Out.clearbuffer () after each
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding= "Utf-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >Out.println ("); Out.println ("The Moon light <br> in front of the bed"); Out.println ("The Ground frost <br>"); Out.flush (); //out.clear ();//error:attempt to clear a buffer that ' s already been flushed//Out.clearbuffer ();Out.println ("Jutou Moon <br>"); Out.println ("The best place to think of <br>");%>Get buffer size:<%=out.getbuffersize ()%> size; <br>Get buffer Available size:<%=out.getremaining ()%> size; <br>whether the buffer is automatically emptied:<%=out.isautoflush ()%></body>Request Object
The client's request information is encapsulated in the requests object to understand the customer's needs and respond. Request is an instance of the Httpserverletrequest class.
The request object has a requesting domain, which is valid until the client's request is completed. Common methods are as follows:
- String GetParameters (string name): Returns the parameter value of name specified parameter
- String[] Getparametervalues (String name): Returns an array of all values that contain the parameter name
- void Setatrribute (String, Object): Stores the properties in this request
- Object getattribute (String name): Returns the property value of the specified property
- String getContentType (): Gets the MIME type of the request body
- String Getprotocol (): Returns the protocol type and version number of the request
- String getservername (): Returns the host name of the server that accepted the request
- int Getserverport (): Returns the port number used by the server to accept this request
- String getcharacterencoding (): Returns the way character is encoded
- void Setcharacterencoding (): Sets the character encoding of the request
- int getcontentlength (): Returns the length of the request body (in bytes)
- String getremoteaddr (): Returns the IP address of the client that sent this request
- String Getrealpath (String path): Returns the true path of a virtual path
- String Request.getcontextpath (): Returns the context path
Create a simple user registration form: login.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding= "Utf-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >User name:<input type= "text" name= "username" ><br>Hobbies:<input type= "checkbox" Name= "Favorite" value= "reading" >Read<input type= "checkbox" Name= "Favorite" value= "singing" >singing<input type= "checkbox" Name= "Favorite" value= "basketball" >Basketball<input type= "checkbox" Name= "favorite" value= "swimming" > Swimming <br> <input type= "Submit" value= "Submit" >&L T;/form></body>Create a request.jsp that processes data
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding= "Utf-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >request.setcharacterencoding ("Utf-8");//solve Chinese garbled problem%>User name:<%=request.getparameter ("username")%><br>Hobbies:<%string[] Favorites= Request.getparametervalues ("Favorite"); for(String favorite:favorites) {out.println (favorite); }%></body>Page Display effect
Now look at the next through a link jump page, do not walk the form form submission, the login.jsp slightly changed
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding= "Utf-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >User name:<input type= "text" name= "username" ><br>Hobbies:<input type= "checkbox" Name= "Favorite" value= "reading" >Read<input type= "checkbox" Name= "Favorite" value= "singing" >singing<input type= "checkbox" Name= "Favorite" value= "basketball" >Basketball<input type= "checkbox" Name= "favorite" value= "swimming" > Swimming <br> <input type= "Submit" value= "Submit" >&L T;/form><a href= "request.jsp?username=kobe" > Test URL Pass Parameters </a></body>Due to the parameter of a link only test username, so favorite is empty case will be error, to request.jsp slightly modified
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding= "Utf-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >request.setcharacterencoding ("Utf-8");//solve Chinese garbled problem%>User name:<%=request.getparameter ("username")%><br>Hobbies:<%if(Request.getparametervalues ("favorite")! =NULL) {string[] Favorites= Request.getparametervalues ("Favorite"); for(String favorite:favorites) {out.println (favorite+ " "); } } %> </body>To see how the page works:
However, when a link parameter is passed to Chinese, garbled characters will appear.
Though Request.setcharacterencoding () is utf-8, it is limited to the request scope. To solve the URL Chinese garbled problem, we need to modify the Tomcat Server.xml.
<ConnectorPort= "8080"Protocol= "http/1.1"ConnectionTimeout= "20000"Redirectport= "8443" />----> Add uriencoding Properties<ConnectorPort= "8080"Protocol= "http/1.1"ConnectionTimeout= "20000"Redirectport= "8443"uriencoding= "Utf-8" />
Right-Remove the servers under Eclipse console server and restart the Tomcat server and run again:
Next, test the other request methods:
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding= "Utf-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >request.setcharacterencoding ("Utf-8");//solve Chinese garbled problemRequest.setattribute ("Password", "123456");%>User name:<%=request.getparameter ("username")%><br>Hobbies:<%if(Request.getparametervalues ("favorite")! =NULL) {string[] Favorites= Request.getparametervalues ("Favorite"); for(String favorite:favorites) {out.println (favorite+ " "); } } %><br>Password:<%=request.getattribute ("password")%><br>MIME type:<%=request.getcontenttype ()%><br>protocol type and version number:<%=request.getprotocol ()%><br>Server Host Name:<%=request.getservername ()%><br>Port number:<%=request.getserverport ()%><br>character encoding method:<%=request.getcharacterencoding ()%><br>Request Body Length:<%=request.getcontentlength ()%>bytes<br>Client IP Address:<%=REQUEST.GETREMOTEADDR ()%><br>True path of the virtual path:<%=request.getrealpath ("request.jsp")%><br>Context Path:<%=request.getcontextpath ()%></body>Page Output results:
Response Object
The Response object contains information that responds to a customer request, but it is seldom used directly in the JSP, which is an instance of the HttpServletResponse class.
The response object has a page scope, that is, when a page is accessed, the response object within that page is valid only for this access, and the response object on the other page is not valid for the current page.
Common methods:
- String getcharacterencoding (): What character encoding is returned for the response
- void setContentType (String type): Sets the MIME type of the response
- PrintWriter getwriter (): Returns an object that can output characters to the client (note comparison: PrintWriter differs from built-in out objects)
- Sendredirect (java.lang.String location): Redirecting requests from clients
Import= "java.io.*" contenttype= "text/html; Charset=utf-8 " pageencoding=" Utf-8 "%> <% response.setcontenttype (// Sets the MIME type of the response out.println ("); Out.println ("); // get the output stream object Outer.println ("See where my position is"); %>
Output a String object to the client, the PrintWriter output is always ahead of the JspWriter built-in out object
If you want to make jspwriter better than printwriter to output a string object to the client, you can use the Out.flush () method, which is to force the preceding content to be entered
<%@ page language= "java" import = "java.io.*" contenttype= "text/html; Charset=utf-8 " pageencoding =" Utf-8 "%> <% Response.setcontenttype ( "text/html; Charset=utf-8 "); // Sets the MIME type of the response Out.println ( ); Out.println ( // Note the order of the page strings printwriter outer = Response.getwriter (); // Get the output stream object outer.println ("See where my location is" %>
Note the output order of the page string compared to the above:
Then look at the Redirect method:
Import= "java.io.*" contenttype= "text/html; Charset=utf-8 " pageencoding=" Utf-8 "%> <% response.setcontenttype (// Sets the MIME type of the response out.println ("); Out.println ("); // get the output stream object Outer.println ("See where my position is"); Response.sendredirect (// request redirect %>
The page jumps:
Differences in Request redirection and request forwarding
Request Redirection:
- Client behavior
- Response.sendredirect ()
- Essentially, the equivalent of two requests, the previous request object will not be saved, the Address bar URL will change
Request Forwarding:
- Server behavior
- Request.getrequestdispatcher (). Forward (req, res)
- is a request, the request object will be saved after forwarding, the Address bar URL address will not change
JSP built-in objects (top)