3. Understand fundamentals of the HttpServlet and related APIS
HttpServlet is an abstract class to be subclassed to create an HTTP servlet. Some methods that can be overriden to perform HTTP actions are: GET, POST, DELETE, HEAD, PUT, OPTIONS and TRACE.
- doGet()
- doPost()
- doDelete()
- doHead()
- doPut()
- doOptions()
- doTrace()
Servlet example:
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
this.getServletContext().getRequestDispatcher("/welcome.jsp");
forward(request, response);
}
}
The servlet container creates an HttpServletRequest and HttpServletResponse object and passes it as an argument to the servlet's service methods (
doGet
, doPost
, etc).HttpRequest
- Attributes: getAttribute(String name), getAttributeNames()
- Parameters: getParameter(String name), getParameterMap() getParameterValues()
- ContextPath: getContextPath()
- Cookies: getCookies()
- Headers: getHeader(), getHeaderNames()
HttpResponse
- PrintWriter: getWriter()
- ServletOutputStream: getOutputStream()
- Header: getHeader(String name), addHeader(String name, String value)
- Cookie: addCookie(Cookie cookie)
- ContentType: setContentType(String type)
- URL : sendRedirect(String location)
4. Write code that manages client sessions and cookies
The container can manage the client session with the object HttpSession.
Cookie is client related information stored in the client web browser.
Session
Cookie is client related information stored in the client web browser.
Session
Session contains a pair of key/value
- key --> string type- value --> object
Time limit can be configured in Deployment Descriptor: <session-timeout>
Every object in the server session must implement Serializable interface.
Methods in HttpSession:
- getAttribute(String names)
- getAttributeNames()
- removeAttribute(String name)
- setAttribute(String name, object value)
- getId()
- getLastAccesedTime()
- getMaxInactiveInterval()
- getServletContext()
- setMaxInactiveInterval(int interval)
- invalidate()
A cookie contains a pair of key/ value, where:
-key -->string type
-value -> string type
To retrieve cookies from the user
request.getCookies() - obtain Cookie[]
To add a cookie to the client browser
response.addCookie(Cookie)
Methods:
- getName()
- getValue()
- setValue(String value)
- getPath() -- > path in the server
- setPath(String uri)
- getMaxAge() -->max seconds
Session Vs Cookies
Both are methods to manage user info but not in the same way. Let's see a list of advantages and disavantages:
- Session stores information in the server side
- Cookies stores information in the client
- Session consumes resources in the server. It can be stored by a limited time.
- Cookies do not consume resources in the server, but they have a limited space defined by the browser. Although, it can have a longer duration (days, months..)
- Session can store objects, binary data, files.
- Cookies can only store text.
- Session, is safer because the data is stored in the sever.
- Cookies, the data is exposed to be modified by user, intecept by third party, so in any case should not contain sensible data.
OCEJWCD (SCWCD) - 1Z0-899 - Web Component Developer Certification
No comments:
Post a Comment