The following are the notes for Week 5 Java (Web)
Web Tier - Server Side
edit- http, svn, https, ftp etc. converts the url into an IP address and then connect to a web server
- Browser sends a request to the Web Server, the Web Server will send a respond
- HTTP works using different error codes
- HTTP 404 error - e.g. if the http://www.google.co.uk/index2.html website doesn't exist
- GET and POST are the main request types (HEAD, DELETE, PUT, PUT)
- both pass over information
- GET accepts a maximum of 256 characters
- sending an index.html request is a GET request
- POST is a request to upload something (/index.html?search=fdm)
Web Container
edit- Where the Java code lives
- A request goes into a particular java class, and this class will decide what to do (e.g. check database etc)
- Classes that handle web requests are called Servlets
- Web Containers deal with threads automatically
- Responsible for Security, Concurrency, Lifecycle management, Application deployment
- Java EE (Enterprise Edition) gives standards to things that you'd want to use in the business (previously we dealt with Java SE)
- e.g. dealing with web applications, messaging, transactions...
- Java EE generally runs on a web container
Servlets
edit- Servlets process requests, and send responds based on requests
- can even send back HTML
- can send a simple HTML back, or something based on the request
- request will include IP, POST information
- similar to dependency injection
- concerns need to be separated out (web developer and java developer code shouldn't be mixed up)
- To make a Java servlet, you extend the HTTPServlet
- Servlet Lifecycle
- Container creates an instance of a server
- Calls the init() method
- Calls the service() method
- Calls the destroy() method (usually at shutdown)
- init() and destroy() method are only called once
Applying in Eclipse
edit- To make a Maven Project for web, you choose webapp instead of quickstart in New Maven Project
- packaging in the pom will be war instead of jar
- tomcat is the web container and web server that we use, so maven can deploy the application
- scope in dependencies determines when to use the dependency
- provided means it is needed during compile time, but don't want to include them with your installation (the server will give it to you)
- need to override the doGet method from the HTTPServlet interface
- url-pattern supports wildcards
- apache-tomcat-7.0.22 is required for this
Web Tier - Client-Side
editJavaServer Page (JSP)
edit- JSP are html pages sitting on the server with a little bits and pieces of java code in it
- promotes the MVC architecture
- JSP will be the View
- the Servlet is the Controller
- the Model will be whatever you'd want the model to be
- Lifecycle
- Client requests a JSP Page
- the browser sends the request to the web server and then to the web container
- JSP servlet converts the JSP page into a java servlet file
- A java servlet file is compiled
- the web container loads the servlet and calls the JSPInit() and JSPService() methods
- HTML file will be output to the browser
Scopes
edit- How widely an object is available
- Session is something that will live for multiple requests (usually for a given time)
- Like an Amazon basket
- Application scope is generally used for global variables (that don't change)
- Request scope is only for the duration of one request
- Page scope are only available within the actual JSP Page
- Anything inside Web-INF is protected
- Useful for loggedin page, so a user can't just open the logged in page using the URL
Listeners and Filters
edit- Listener is an interface
- Uses the Observer pattern
- you have something you listen for or observe, when a certain state changes, you call the Listener
- For things you only want to do one (e.g. set up log4j, start connections to DB, close files at the end,...)
General Notes
edit- <% %> is known as a Scriptlet
- <%: %> is known as a Directive
- <%! %> is known as a Declaration (for declaring primitives)
- <%= %> is for an Expression (e.g. <% 5 + 6 %> will show 11)