Search This Blog

Sunday 10 December 2017

Java Web Applications - Basics

1.       Basic components of Java Web Programming
a.       Java Web apps typically made with Servlets and Java Server Pages
b.       The frameworks like Struts built up on these fundamental structures like Servlets and Java Server Pages
c.       Java Server page itself a servlet
d.       There are other concepts like Filters and Listener
e.       Many frameworks today use Filters as their controllers in their MVC models
2.       What is Web Application
a.       A deployable unit of content (Java App) and configurations (Like Web.xml)
b.       Requested routed to an application using URL
c.       Web application may contain static content ex.. images, css, html pages
d.       Web application may also dynamically generated content ex.. Java, JSP, listener and filters
e.       Deployment
                                                               i.      Can be deployed as WAR (web archive ) file
                                                             ii.      Or simply copying entire folder structure and files
3.       Servlets
a.       Every servlet implements base interface called “Servlet”
b.       Servlet has 3 stages in its life cycle
                                                               i.      Initialize Servlet
                                                             ii.      Service Method (the sole purpose of Servlet)
1.       Servlet Service method take 2 parameters, servlet request object and servlet response object
2.       Servlet request object contains any data supplied with servlet request
3.       Servlet response object will have servlet return data
                                                           iii.      Destroy Servlet
c.       Though we can implement Servlet interface directly, its not recommended. We have helper classes that implemented Servlet interface..ex… GenericServlet or HttpServlet classes..
                                                               i.      GenericServlet is protocol agnostic
                                                             ii.      HttpServlet is Http protocol specific
4.       Servlet Mapping
a.       For webserver to see the Servlet and execute it, we need to add a Servlet Mapping
b.       We can add Servlet mapping using Web.xml or addition annotation within servlet itself
c.       Annotation example ... “@WebServlet(“/home”)”
d.       Request Routing is handled via Web.xml configuration.
e.       We can have multiple servlet mappings..
f.        The servlet mapping defines which URLs that will access servlets.. for example if Servlet is mapped to “/home” this means, servlet invoked when URL http://server/applicationname/home is hit... Here the application name is the name of application
g.       http://server/applicationname is called baseURL.. application name is what deployed in Tomcat or any webserver.. this is what we see in Tomcat webapps instance.. ex.. /webapps/{applicationname}/WEB-INF etc..
5.       Initializing a servlet
a.       We can set initial parameters for servlet as initial state
b.       In case of Annotation, initialization can be done using “initParams” collection
c.       Example of annotation with init is..
@WebServlet(urlPatterns={“/home”,”*.do”},
                           initParams={@WebInitParam(name=”Varname”,value=”server1”)}
                          )
d.       In case of web.xml
<init-param>
   <param-name> Varname</param-name>
   <param-name> server1</param-name>
</init-param>
e.       In servlet we can using init() method and getInitParameter(“Varname”) to get the parameter value
f.        Global servlet parameters can be defined in web.xml as <context-param>.. these can be retrieved from any servlet using getServletContext().getInitParameter(“Pname”)
6.       Java Server Pages
a.       JSP pages can be coded with HTML , CSS etc. <!DOCTYPE > tag define type of JSP code.. <!DOCTYPE  html> means it HTML code in jsp file
b.       JSP pages are indeed servlets, jsp file is converted to java class file during compilation
c.       Use Directive <%@include file=”filanme”> to include a file with in jsp file
d.       <%= classmethod() %> to execute a class methods like getName(), setName() etc
7.       MVC
a.       A common idiom in web applications today is using some thing called Model View Controller
                                                               i.      Model – which is data we like to display
                                                             ii.      View – which is the place where we display
                                                           iii.      Controller – Which is to bring Model and View together
8.       In MVC
a.       Servlet used as controller (usually!)
b.       JSP used as the view
c.       Beans used as  model (POJO – plain old java object)
d.       Beans could be just as simple as old core java classes
e.       In servlet, doGet (or in doPost) methods, we can create an instance of the Bean (an instance of class). We can then pass request on to JSP page to display this data. To do that, we need to something called requestDispatcher… requestDispatcher is available through  servlet context… i.e. getServletContext().gerRequestDispatcher()  
f.        We passed the control to JSP page via gerRequestDispatcher object. We also need to tell JSP page how to get the Bean. This can be done request.setAttribute(“paramvalue”,Paramname)
g.       To hide JSP pages from publicly available, we need to create JSP pages in WEB-INF folder
9.       Expression Language
a.       Used in JSP pages.. is very easy use.. do not need Java skills
b.       Allows more dynamic pages to be created
c.       Syntax .. ${expression}… ex.. ${user.name}
d.       Expression language has some operators  like +, - etc and relational operators && and || but it do not have full pledged programing language features like looping, if-else etc
10.   JSTL (Java Standard Tag Library)
a.       Its standard tags used in JSP pages to control the output of page display
b.       Conditional tags could be used to display menu options based on user profile for example.


No comments:

Post a Comment