Server-side Technologies in Java

11,402 views
11,009 views

Published on

Covers a wide variety of technologies and solutions available to develop effective Java based server side HTTP applications.

Published in: Technology
1 Comment
12 Likes
Statistics
Notes
No Downloads
Views
Total views
11,402
On SlideShare
0
From Embeds
0
Number of Embeds
39
Actions
Shares
0
Downloads
0
Comments
1
Likes
12
Embeds 0
No embeds

No notes for slide

Server-side Technologies in Java

  1. 1. Web Technologies – Server-side
  2. 2. What’s in store… <ul><li>Basic Server concepts </li></ul><ul><li>HTTP Request, Response and Session </li></ul><ul><li>Understanding Servlets </li></ul><ul><li>The deal with JSP s </li></ul><ul><li>The quest for scriptless JSPs – Taglibs , JSTL </li></ul><ul><li>Security, Filters and Listeners </li></ul><ul><li>Understanding the web application descriptor – web.xml </li></ul><ul><li>The evolution of Web Application Architecture </li></ul>
  3. 3. R O L E of a S E R V E R <ul><li>Receives the request, finds the resource, returns the response </li></ul><ul><li>Of course, there’s 404 ! What are the other status codes? </li></ul><ul><li>Can be the physical m/c [hardware] or web server app [software] </li></ul><ul><li>Most of the communication is HTTP based </li></ul><ul><li>Provides a container within which server code can execute </li></ul>
  4. 4. Protocols P R O T O C O L S <ul><li>HTTP is the most commonly used </li></ul><ul><li>Other supported protocols are FTP, HTTPS, FILE </li></ul><ul><li>Each of these protocols operate on top of TCP/IP </li></ul><ul><li>TCP chunks data into blocks and makes sure all of it reaches dest </li></ul><ul><li>IP underlying protocol to move/route data packets between src & dest </li></ul>
  5. 5. HTTP R E Q U E S T <ul><li>method informs server about the type of request, how the message is formatted </li></ul><ul><li>GET and POST are most common, but there are others – PUT, HEAD, DELETE, etc </li></ul><ul><li>Total amount of info through GET is limited </li></ul><ul><li>Submitted request data is visible with GET. Passwords! </li></ul><ul><li>Bookmarking of <form> data submission not possible with POST </li></ul>
  6. 6. G E T Request Internals Request Line HTTP Method Path to the resource All the request parameters Protocol version for the request Request headers
  7. 7. P O S T Request Internals Request Line HTTP Method Path to the resource Protocol version for the request Request headers Message body – “payload”
  8. 8. Response Internals Protocol version used by server HTTP Response status code Response headers Text version of status code Body content – HTML or other Mime-type
  9. 9. HTTP S E S S I O N <ul><li>The conversational state </li></ul><ul><li>Both browsers and servers possess NO short/long term memory! </li></ul><ul><li>HTTP is stateless ! </li></ul><ul><li>In comes session management – </li></ul><ul><ul><li>URL rewriting - URL needs to be encoded – response.encodeURL(“…”) </li></ul></ul><ul><ul><li>Cookie handling </li></ul></ul>
  10. 10. S E R V L E T S … <ul><li>Live to service clients </li></ul><ul><li>Needs to know what and how to find data in request </li></ul><ul><li>Send back the response to the browser, or elsewhere </li></ul><ul><li>The servlet container transforms HTTP Request and Response to HTTPRequest and HTTPResponse objects </li></ul><ul><li>The container relies on web descriptor to determine which servlet to invoke </li></ul>
  11. 11. S E R V L E T L I F E C Y C L E <ul><li>init – only ONCE when servlet gets loaded </li></ul><ul><ul><li>So when all does the servlet get loaded ? </li></ul></ul><ul><li>service – every time a particular servlet is invoked </li></ul><ul><ul><li>HTTPServlet provides the doGet and doPost methods </li></ul></ul><ul><li>destroy – when the web application is brought down </li></ul><<interface>> Servlet GenericServlet HTTPServlet YourServletClass
  12. 12. S E R V L E T S – Important Methods <ul><li>HTTPRequest – </li></ul><ul><ul><li>getParameter(String) </li></ul></ul><ul><ul><li>get/set Attribute(String, Object) </li></ul></ul><ul><ul><li>getSession(boolean) </li></ul></ul><ul><ul><li>getCookies()… </li></ul></ul><ul><li>HTTPResponse – </li></ul><ul><ul><li>addCookie() </li></ul></ul><ul><ul><li>addHeader() </li></ul></ul><ul><ul><li>setStatus() </li></ul></ul><ul><ul><li>getOutputStream() </li></ul></ul><ul><ul><li>getWriter() </li></ul></ul>
  13. 13. J S P – Java Servlet Pages <ul><li>Container converts JSP -> Java source code -> compiles into a Servlet </li></ul><ul><li>Can directly write Java code in a JSP, but DO NOT! </li></ul><ul><li>Java code contained within <% javaCodeComesHere; %> </li></ul><ul><ul><li>Incase of expressions, never end with a semi-colon - <%= expression %> </li></ul></ul><ul><li>Certain Java objects are available implicitly </li></ul><ul><ul><li>request, response, out, session, config, application, page, … </li></ul></ul><ul><li>Taglibs or JSTL are much better options </li></ul>
  14. 14. J S P Structure <ul><li>Non-implicit Java classes can be imported using directive </li></ul><ul><ul><li><%@ page import=“org.abc.xyz.*” %> </li></ul></ul><ul><ul><li>“ ,” can be used to separate multiple imports </li></ul></ul><ul><li>Scriptlet - <% … %> </li></ul><ul><li>Directive - <%@ … %> </li></ul><ul><li>Expression - <%= … %> </li></ul><ul><li>Comments - <%-- … --%> </li></ul><ul><li>Declaration - <%! … %> </li></ul>
  15. 15. J S P Lifecycle <ul><li>Container first checks amongst the directives </li></ul><ul><li>Creates an HttpServlet sub-class </li></ul><ul><ul><li>In Tomcat, org.apache.jasper.runtime.HttpJspBase , extended </li></ul></ul><ul><li>Imports are resolved </li></ul><ul><li>A package declaration – org.apache.jsp.[folderStructure] – is added </li></ul><ul><li>Builds the service() method - _jspService() </li></ul><ul><li>The servlet class – JspName_jsp.java – is created </li></ul>
  16. 16. J S P - I M P L I C I T Objects <ul><li>JspWriter – out </li></ul><ul><li>HttpServletRequest – request </li></ul><ul><li>HttpServletResponse – response </li></ul><ul><li>HttpSession – session </li></ul><ul><li>ServletContext – application </li></ul><ul><li>ServletConfig – config </li></ul><ul><li>JspException – exception </li></ul><ul><li>PageContext – pageContext </li></ul><ul><li>Object -- page </li></ul>
  17. 17. Scriptless J S P - Taglibs & JSTL <ul><li>Taglibs are powerful – but non-standardised! </li></ul><ul><li>Multiple vendors – different Taglibs – chaos! </li></ul><ul><li>Operations requiring scripting – iteration, accessing scoped objects, etc </li></ul><ul><li>JSTL encapsulates core functionalities related to – </li></ul><ul><ul><li>Iteration & conditionals [ Core ] </li></ul></ul><ul><ul><li>XML [ XML ] – useful in case of XML content </li></ul></ul><ul><ul><li>Database access [ Database ] </li></ul></ul><ul><ul><li>Internationalised formatting [ Internationalization ] </li></ul></ul><ul><ul><li>Utility functions [ Functions ] </li></ul></ul>
  18. 18. Inside J S T L <ul><li>Core library – iterations, scope access, conditionals </li></ul><ul><ul><li><%@ taglib prefix=&quot;c&quot; uri=&quot;http://java.sun.com/jsp/jstl/core&quot; %> </li></ul></ul><ul><ul><li><c:set scope=“”>, <c:remove> - set/remove variables in defined ‘scope’ </li></ul></ul><ul><ul><li><c:forEach var=“” items=“” begin=“” end=“” varStatus=“”> - Iteration </li></ul></ul><ul><ul><li><c:if test=“”> - conditional if </li></ul></ul><ul><ul><li><c:choose> - condition if-else <c:when test=“”></c:when> <c:otherwise></c:otherwise> </c:choose> </li></ul></ul><ul><ul><li><c:import> - including another JSP file, no buffering </li></ul></ul>
  19. 19. Inside J S T L … <ul><li>Expression Language [EL] library – powerful set of functions </li></ul><ul><ul><li><%@ taglib prefix=&quot;fn&quot; uri=&quot;http://java.sun.com/jsp/jstl/functions&quot; %> </li></ul></ul><ul><ul><li><fn:length>, <fn:toUpperCase>, <fn:toLowerCase>, <fn:substring>, <fn:substringBefore>, <fn:substringAfter>… </li></ul></ul><ul><ul><li><%-- truncate name to 30 chars and display it in uppercase --%> ${fn:toUpperCase(fn:substring(name, 0, 30))} </li></ul></ul><ul><ul><li><%-- Display the name if it contains the search string --%> <c:if test=&quot;${fn:containsIgnoreCase(name, searchString)}&quot;> Found name: ${name} </c:if> </li></ul></ul>
  20. 20. Inside J S T L <ul><li>Core library – iterations, scope access, conditionals </li></ul><ul><ul><li><%@ taglib prefix=&quot;c&quot; uri=&quot;http://java.sun.com/jsp/jstl/core&quot; %> </li></ul></ul><ul><ul><li><c:set scope=“”>, <c:remove> - set/remove variables in defined ‘scope’ </li></ul></ul><ul><ul><li><c:forEach var=“” items=“” begin=“” end=“” varStatus=“”> - Iteration </li></ul></ul><ul><ul><li><c:if test=“”> - conditional if </li></ul></ul><ul><ul><li><c:choose> - condition if-else <c:when test=“”></c:when> <c:otherwise></c:otherwise> </c:choose> </li></ul></ul><ul><ul><li><c:import> - including another JSP file, no buffering </li></ul></ul>
  21. 21. The WEB D E S C R I P T O R <ul><li>More famously known as the web.xml </li></ul><ul><li>Defines almost all aspects of the deployed behaviour of the application </li></ul><ul><li>Context of web application – the logical root folder of deployed resources </li></ul><ul><li><welcome-file-list> - The list of lookup files when context root is accessed </li></ul><ul><li><servlet-mapping> - maps URL patterns to servlets </li></ul><ul><li><servlet> - maps servlet names to servlet classes </li></ul><ul><li><init-parameter> - Servlet specific initialisation parameter </li></ul><ul><li><context-param> - Application context parameters </li></ul>
  22. 22. The Evolution of Web Application Architecture <ul><li>Started with the servlets being the centre of the world </li></ul><ul><li>Then came JSP, but soon all the Java scriptlets ruined it! </li></ul><ul><li>Separation of Concerns! </li></ul><ul><li>In came MVC </li></ul><ul><ul><li>[M]odel - Simple POJOs represent datasets </li></ul></ul><ul><ul><li>[V]iew – JSPs without any or rare usage of Java scriptlets </li></ul></ul><ul><ul><li>[C]ontroller – The servlets determined the flow of control </li></ul></ul><ul><li>Greater configurability and modularity led to MVC2 – Struts2, Spring, etc </li></ul>
  23. 23. Some more Advanced Concepts <ul><li>Listeners – listen to any change events , provide hooks </li></ul><ul><ul><li>Typically have created and destroyed kind of methods </li></ul></ul><ul><ul><li>ServletContextListener </li></ul></ul><ul><ul><li>HttpSessionListener </li></ul></ul><ul><li>Filters – like a light-weight servlet that doesn’t create its own content </li></ul><ul><ul><li>Plugs into the request or response handling mechanism </li></ul></ul><ul><ul><li>Mapped to a particular URL pattern </li></ul></ul><ul><ul><li>If URL is mapped to servlet – filter will get executed before servlet </li></ul></ul><ul><ul><li>Typical use – generating user access statistics </li></ul></ul>
  24. 24. R E F E R E N C E S <ul><li>Head First Servlets And JSP </li></ul><ul><li>JSTL Presentation – Sang Shin, Sun Microsystems </li></ul><ul><li>Java Boutique </li></ul><ul><li>Wikipedia </li></ul>

×