Unit 8 - The Web Tier - Servlets, GET & POST, JSPs, JavaBeans, MVC architecture Flashcards
What is the main function of the web tier?
The code associated with the web tier runs on the server machine. It sits between the business and client tier.
Received requests from clients and responds by dynamically contsructing web pages using servlets and JSP. Communication is made using HTTP.
What are the advantages and disadvantages of servlets compared to applets in providing dynamic content for web pages?
- Applets run on the client machine and can be very responsive as there are no network delays.
- Applet code must be downloaded from the server before running, this makes it less attractive to write large applets.
- Servlets are more scalable - more servers can be added to provide back up or to cope with heavy demand.
- Untrusted applets have severe security restrictions which limits what they can do on the client machine, though trusted applets can get around this.
- Because servlets are run and stored in the controlled server environment they don’t need such severe security restrictions. Also they have access to the full power of Java and its libraries. They can access other servers and invoke programs.
What are the advantages and disadvantages of servlets compared to CGI scripts?
Each CGI script starts a separate process in response to an HTTP request which it closes after responding.
Servers are more efficient: -
- once started they run on the server and can deal with a succession of HTTP requests possibly from many clients
- they use threads rather than processes to service concurrent requests
- they make it easy to store the state of a session and maintain links to resources like a database.
How are servlets run?
They run in a web container that handles security, concurrency and the servlet lifecycle. The preferred approach is to use a multithreaded servlet (handles concurrent requests using a thread for each request) with control over access to shared resources.
How is a servlet programmed?
import javax.servlet.*;
this gives you access to the Servlet interface
HTTPServlet implements init(initialises) service(handles requests), destroy(clean up before closing servlet)
so,
public class exampleServlet extends HttpServlet
then override the service methods, most commonly
doGet and doPost
When a servlet receives an HTTP request it invokes the service method and passes it references to two objects of classes HttpServletRequest and HttpServletResponse. What do these classes do?
HttpServletRequest - contains details of the request, including the method name (eg GET) and any paramaters or data
HttpServletResponse - constructs the response to be returned to the client.
What further expectations does the Java EE API place on the behaviour of HTTP servlet methods that respond to GET and POST requests?
Get should be safe i.e. not change any stored data in databases. Get should be idempotent - safely repeated, it should leave the system in the same state.
doPost - does not have to be idempotent, ops reqeusted through post can have side effects for which the user can be held accountable e.g. updating stored data, or buying something.
How can Java servlets deal with a number of concurrent HTTP requests?
A server is automatically multithreaded - it creates a new thread for each HTTP request it receives, so it can deal with a number of concurrent requests.
Which method is run by a servlet in response to each user request and what is the purpose of this method?
Each thread runs the service method which in turn invokes helper methods such as doGet or doPost, depending on the type of request being processed.
What issues arise in respect of shared resources and how can they be dealt with?
Data stored in instance variables or static variables should be synchronised if such data is updated. The same precautions apply to any resources such as network links or database connections shared between threads.