January 6, 2017

Beginning to Java Web App Developement

1. Website/Web Application

Website is a collection of web pages that may contain text, images, audio and video.
Web Application is a website which focuses more on providing some useful functions to users.

Static Website : Static website displays static/fixed information
Dynamic Website : Dynamic website displays information that keep changing.


Static WebsiteDynamic Website
Prebuilt content is same every time the page is loaded.Content is generated quickly and changes regularly.
It uses the HTML code for developing a website.It uses the server side languages such as PHP,SERVLET, JSP, and ASP.NET etc. for developing a website.
It sends exactly the same response for every request.It may generate different HTML for each of the request.
The content is only changes when someone publishes and updates the file (sends it to the web server).The page contains "server-side" code it allows the server to generate the unique content when the page is loaded.
Flexibility is the main advantage of static website.Content Management System (CMS) is the main advantage of dynamic website.


2. Hyper Text Transfer Protocol (HTTP)


HTTP
Data communication protocol used to establish communication between client (internet browser) and web server.

HTTP Request
The information send by the client (browser) to a web server that 'requesting' for some response (data, file, info etc) from the web server using HTTP protocol

GET vs POST (Both are type HTTP Request)

GETPOST
is used to retrieve data from web serveris used to insert/update data into web server.
is not secured because data is exposed in URL bar.is secured because data is NOT exposed in URL bar.
is used typically for viewing something, without changing itis used for changing something
data is sent through the header (can be seen in URL bar)
www.ask.com/register.jsp?name1=value
data is sent in body (can NOT be seen in URL bar)
www.ask.com/registerDao.jsp


3. Front End (client-side) vs Back End (server-side)

Front EndBack End
Refers to the stuff that you actually see on the browser aka the UI Interface of the website.Refers to the "brain" of the application which live on the server 
Use HTML, CSS, and JavaScript that are supported by the browser (client)Use other languages such as Java and PHP that are supported by the server 



4. Java Servlet 

Servlet is a java program that run at the back end (server-side). Servlet is the program that listen and response to HTTP Request. Servlet is the one who retrieve data from the database by performing queries.

Basically, Servlet is a java class (that extends HttpServlet class from Java API).
For example, MyServlet.java (which is then compiled to MyServlet.class)



5. Web Server & Servlet Container

Web Server is a program that processes request via HTTP..
Web Server is responsible to store, process and deliver web pages to clients.

Example : Apache HTTP Server (Apache2, httpd)

A Servlet Container is a type of Web Server that host and process Java program called Servlet. Servlet Container is responsible for managing the lifecycle of servlets, mapping a URL to a particular servlet etc. Servlet Container also works as Web Server for non-java programs.

Example : Tomcat

NOTE : All Servlet Containers are Web Server. However, not all Web Server are Servlet Container. Apache HTTP Server is only a Web Server and it cannot run Java Servlet at all.

Servlet Container can host many Java Servlets.

For normal Java Servlet, one Java Servlets are "mapped" to one specific URL/page.
For example,
When user open www.ask.com/register.jsp, regServlet  is called.
When user open www.ask.com/logout.jsp, logoutServlet  is called.

This is called Servlet Mapping and is done via Web Deployment Descriptor or using WebServlet annotation (see next)


6. Web Deployment Descriptor (web.xml)

This is a XML file where Servlet Container finds out which Servlet are mapped to which URL/page.



Example : web.xml



7. Servlet Annotation vs web.xml

'@WebServlet' annotation can be used to declare a servlet instead of web.xml (especially if you have many Servlets). This annotation is processed by the Servlet Container at deployment time, and the corresponding servlet made available at the specified URL patterns (value=URL Pattern).



8. Web Application Structure