reading some more on servlets...
starting to read/understand
QBit Microservices using Service Workers and sharded service workers
starting to read/understand
Using QBit Java microservice lib with Spring Boot
starting to read/understand
QBit Microservice Java Lib Working With CallBacks and Java 8 Lambda expressions
---------------------------------------------------------
servlets continued
- the most common CGI: SERVER_NAME=, SERVER_PORT, REQUEST_METHOD, SCRIPT_NAME, PATH_INFO, QUERY_STRING, CONTENT_TYPE, CONTENT_LENGTH.
-the main server is configured to recognize that if the requested URI starts with for example /servlet after the computer name and port; then connects to a specific port number. then passes the request body to that port number.the CGI variable are always available o any program that wants to retrieve them.
-a servlet container is a software program which after start up runs constantly in the background and listens on a specific port number.
-the servlet container acts like a java server to the main web server. it processes the request and outputs the results back to the main web server. the main web server can then decide what to do with the output. if it is html the server can just pass it the client's browser.
- when it detects an incoming data stream on the port. the servlet container retrieves the request information and stores it in a standard java object of type HttpServletRequest.
-an HTTP servlet gets the request object then does what ever with it and sends java's response back to the main web server.
-the main web server attaches additional response header and sends the response back to the client's browser
life cycle of a java servlet
-when the servlet container starts up it creates an instance of the the servlet class
-the container or java server calls that instance's init() method which is always allowed to complete before the service() doGet() or doPost() method can ever be called.
from now on whenever a browser requests a servlet, the server calls that instance's service() method. if multiple requests are received , the sever calls the same service() method in a separate thread unless the java server is maintaining instance pools.
-for GET and POST requests on HttpServlets, the service() method automatically calls doGet(), or doPost() method which you define in the servlet's code.
-if the server ever needs to be shut down or if too many servlets are loaded in memory and some of them are not being used, it calls the servlet instances's destroy() method. any data in memory that needs to be saved can then be written to disk in in the destroy() method.
- the server then forgets that the servlet instance ever existed. the instance is ultimately garbage collected to free up memory.
-when the server restarts or the net servlet is called the lifecycle starts all over again
how to process a request to a servlet
-the java server or container automatically:
process the string query from get methods and the the request body from post methods.
decodes the url encoded strings and the request body
stores the resulting name - value pairs in a request object
calls the servlet's doGet() or doPost() method depending on the incoming request method
-java's HTTP request object has a number of methods to access various parts of the incoming request. here are some the methods used often:
req.getServerName()
request.getServerPort()
someReq.getMethod()
requ.getServletPath()
req.getPathInfo()
req.getPathTranslated()
eq.getQueryString()
req.getContentType()
req.getContentLength()
-gets all request query string and request body name/value
Enumeration keys = req.getParameterNames();
while (keys.hasMoreElements() ) {
String key = (String)keys.nextElement(); //To retrieve a single value String value =
req.getParameter(key); // If the same key has multiple values (check boxes) String[] valueArray =
req.getParameterValues();
}
Note: If the parameter data was sent in the request body, for example in a POST request, then don't use getInputStream() or getReader(). Instead use the getParameter...() methods.
-gets a session object with a particular request (true means create one if non exictent. don't call getWrite() before this call
//Also makes a new session if none exists
HttpSession session = req.getSession(true);
-Gets the part of this request's URL from the protocol name up to the query string
String urlUptoQuestionMark = request.getRequestURI();
it can be done also by
StringBuffer buff = HttpUtils.getRequestURL(req);
the returned URL string contains a protocol server name port number and server path but it does not include query string parameters
-A servlet (or a full servlet web application) maintains same information across all the clients in a "ServletContext" object (unlike "HttpSession" objects which are specific to one client).
-The Java server administrator sets the servlet context of a web application (group of servlets)in the Java server by defining:
path prefix for the web application
class names
initialization parameters for servlets
request-URI-to-specific-servlet mappings
session parameters
mime types for static files
-Get a servlet web application's context path as configured in the java server (e.g. /myApp):
String cool = request.getContextPath();
-To work with a Servlet Context as a Java object, just call getServletContext() within a servlet. It is always available.
ServletContext sct = getServletContext();
-Get another "URL" object on the same server. The path to get must begin with a "/" and is interpreted as relative to the current context root.
URL gotten = getServletContext().getResource("/whatever/else")
-Convert Internet path to computer file path
String file = getServletContext().getRealPath("path string");
-Get Servlet Container make and model:
String serverInfo = getServletContext().getServerInfo();
-Get the names of the runtime initialization parameters for this servlet, if any. The Java server administrator may have specified these parameters as name=value pairs when configuring this servlet in the server. These are the counterparts of command line options passed to a command line application.
Enumeration enum
= getServletContext().getInitParameterNames();
String initString = getServletContext().getInitParameter("parameter name");
No comments:
Post a Comment