Summary nine four built-in objects and domain objects

https://blog.csdn.net/liufeifeihuawei/article/details/92434886

 

Nine built-in object
refers to the object in <% =%> and <%%> can be directly used in the JSP: not specified the default switch may be turned

the pageContext (important)
1, a domain object itself: it can operate the other three domains objects (request session application) data
void the setAttribute (String name, Object O);
Object the getAttribute (String name);
void removeAttribute (String name) ;
method of operation of the other domain objects
void the setAttribute (String name, object O, the Scope int);
object the getAttribute (String name, int the Scope);
void removeAttribute (String name, int the Scope);
value of scpoe:
PageContext.PAGE_SCOPE
PageContext.REQUEST_SCOPE
PageContext.SESSION_SCOPE
PageContext.APPLICATION_SCOPE
findAttribute (String name);
automatic order to find the page request session application, found on the value, the end of the search.
2, it can create additional 8 implicit objects
may be acquired by other JSP implicit object PageContext regular classes. When you use a custom label.
3, provides a simple method
pageContext.forward(“2.jsp”);
pageContext.include(“2.jsp”);

Four domain objects
PageContext: pageConext data stored in the currently active page. Use less development time.
ServletRequest: a data request stored in the request (forward) within the valid. Use very much.
HttpSession: session data stored efficiently in a session. Use more. Such as: store the user's login information, shopping cart functionality.
ServletContext: storing application data are valid over the entire range of applications. Because the range is too big, it should be used sparingly.

Added: Specific examples

HttpServletRequest
object represents the client's request, the client when accessing the server using the HTTP protocol, all HTTP request header is encapsulated in the object, this object by providing a method, it is possible to obtain all the information requested by the client.
Obtain client information
  getRequestURL method returns the full URL when the client makes a request.
  The method returns the requested resource name getRequestURI section line.
  The method returns the requested parameter getQueryString section line.
  getPathInfo method returns to request additional information path in the URL. Additional path information is content after a request path located Servlet in the URL and query parameters before it to "/" at the beginning.
  getRemoteAddr method returns the IP address of the client issuing the request.
  Full hostname getRemoteHost method returns the requesting client.
  getRemotePort method returns the network port number used by the client.
  getLocalAddr method returns the IP address of the WEB server.
  getLocalName method returns the host name of the WEB server.
/ ------------------ gorgeous dividing line --------------------- /
Package gacl.request. Study;
Import java.io.IOException;
Import java.io.PrintWriter;
Import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**

@author gacl

Obtain client information request by a request object
* /
public class RequestDemo01 the extends the HttpServlet {

void the doGet public (the HttpServletRequest Request, the HttpServletResponse Response)
throws ServletException, IOException {
/ **
* 1. obtain client information
* /
String requestUrl = request.getRequestURL () toString ();. // get the URL address request
String requestUri = request.getRequestURI (); // get the requested resource
String queryString = request.getQueryString (); // get the URL parameters included with the request in the
String remoteAddr = request.getRemoteAddr (); // get the visitor's IP address
String = request.getRemoteHost remoteHost ();
int remotePort request.getRemotePort = ();
String remoteUser as request.getRemoteUser = ();
String method request.getMethod = (); // get the methodology used in the URL requested
String pathInfo = request. getPathInfo ();
String localAddr = request.getLocalAddr (); // obtain the IP address of the WEB server
String localName = request.getLocalName (); // get the host name of the WEB server
response.setCharacterEncoding ( "UTF-8") ; // set the character to "UTF-8" output code to the client browser
// by setting control the browser in response to the display data head to encode UTF-8, and if not the sentence, then the browser will display distortion
response.setHeader ( "content-type", "text / html; charset = UTF-8" );
the PrintWriter response.getWriter OUT = ();
out.write ( "client information acquired as follows:");
out.write ( "

");
Out.write (" the URL of the requested address: "+ requestUrl);
out.write ("
");
out.write (" the requested resource: "+ requestUri);
out.write ("
");
OUT. write ( "accompanying URL address request parameters:" + queryString);
out.write ( "
");
out.write ( "IP address of the visitor:" + remoteAddr);
out.write ( "
");
OUT .write ( "visitors host name:" + remoteHost);
out.write ( "
");
out.write ( "port number:" + remotePort);
out.write ( "
");
out.write ( "remoteUser:" + remoteUser);
out.write ( "
");
out.write ( "request method used:" + method);
out.write ( "
");
out.write ( "pathInfo:" + pathInfo) ;
out.write ( "
");
out.write(“localAddr:”+localAddr);
out.write("
");
out.write(“localName:”+localName);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}

}

Obtain client request header
getHeader (string name) Method: String
the getHeaders (String name) Method: the Enumeration
getHeaderNames () method

Obtaining a client request parameters (data submitted by the client)
the getParameter (String) Method (common)
the getParameterValues (String name) Method (common)
the getParameterNames () method (not used)
getParameterMap () method (used when writing the frame)

HttpServletResponse
Web http server receives the client's request, will be for each request, each request to create a response object objects represent requests and responses for representatives.
request and response objects now that representatives of requests and responses, then we want to get the data submitted by the client over, only to find the request object on the line. Output data to the clients, only to find the response object on the line
session
a, in WEB development, the server can create a session objects (session objects) for each user's browser, note: a browser exclusively a session object (default in the case). Thus, when the need to save user data, the server program may be written to the user data of the user exclusive browser session, when a user uses a browser to access other programs, other program can be taken out from the user data in the user's session, as customer service.
Two, Session and Cookie main difference
Cookie is the user data addressed to the user's browser.
Technical Session of the user data is written to a user exclusive session.
Session object is created by the server, developers can call the request object's method getSession get the session object.
Three, session The principle
server is how to achieve a user's browser session as a service?
After the server creates a session out of the session will id number, back in the form of a cookie addressed to the client, so long as the client browser does not turn off, when to go to access the server, you will go with a session id number, server found that the client browser with session id over, and it will use memory in the corresponding session to serve

 

 


----------------
Disclaimer: This article is CSDN bloggers "to fly the clouds to see the rainbow 'original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/liufeifeihuawei/article/details/92434886

Guess you like

Origin www.cnblogs.com/jishumonkey/p/12122664.html