M362 - Unit 8 Flashcards
What are the advantages and disadvantages of servlets compared to applets for web pages?
Applets run on the client machine (very responsive, no network delays) but code must be downloaded from a server before running, making it less attractive to write large applets. Servlets are more scalable – additional servers can be used to cope with high demand or provide backup in case of server failure. Untrusted applets have severe security restrictions that limit what they are permitted to do on the client machine – although most of these can be avoided using trusted applets. Servlets are stored and run in a controlled environment on the server so do not need to have such security restrictions. Servlets can use the full power of the Java language and its libraries. Servlets can access other servers and invoke other programs.
What are the advantages and disadvantages of servlets compared to CGI scripts for web pages?
Each CGI script starts a separate process in response to an HTTP request and closes the process after responding. Servlets are more efficient as, once started up, they continue running on the server and can deal with a succession of HTTP requests, possibly from multiple clients. Servlets use threads rather than processes to service any concurrent requests. Servlets make it easy to store the state of a session and to maintain links to resources like a database.
How can Java servlets deal with a number of concurrent HTTP requests?
A servlet 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 a helper method such as doGet or doPost, corresponding to the type of request being processed.
What issues arise in respect of shared resources in servlets, and how can they be dealt with?
Data stored in instance variables or static variables of the servlet is shared between any active threads, and so special care (such as synchronisation) must be taken if such data is updated, to avoid data corruption. The same precautions apply to any resources such as network links or database connections shared between threads.
How do JSP pages differ from Java HTTP servlets?
Both can produce a response to an HTTP request. A JSP page can contain a mixture of static elements (text, HTML or XML) and dynamic elements (fragments of Java code). A Java HTTP servlet is entirely written in Java and is an instance of a class that inherits from the HttpServlet class.
What are the main categories of tag used in JSP, and what is each category used for?
The categories are directive tags, scripting tags and action tags. Directive tags (such as the page directive) provide information to the JSP container about the page, and affect how the JSP compiler generates the servlet. Scripting tags allow the inclusion of fragments of Java in a JSP page. There are three types of scripting tag: declaration, scriptlet and expression. Action tags specify some sort of action to be carried out by the JSP page. They may be standard actions such as including applet code in the JSP page or forwarding control to another JSP page. You can also create your own custom actions.
Explain the role of scriptlets in JSP and how they differ from JSP expressions.
A scriptlet is a fragment of Java code, consisting of one or more complete Java statements that will be executed when the JSP page runs. A JSP expression contains a Java expression that produces a value, and this value will be displayed as part of the output.
What is the difference between a JavaBean and an EJB (Enterprise JavaBeans) component?
JavaBeans are defined as special Java classes and can be used on any tier in a Java EE system or in ordinary Java applications. A JavaBean class must have a zero-argument constructor. Every readable property of the bean must have a getter method and every writeable property must have a setter method. In Java EE web tier programming they are useful for temporary storage of data and also for data transfer among servlets and JSP pages. Enterprise JavaBeans are server-based components for Java EE systems, running within an EJB container and used for the business tier of an application. EJB session beans are used to define the business logic of a Java EE application. They have a business interface that defines their publicly available methods, and this can be configured for either local access (within the same JVM) or remote access (from another JVM, possibly on a remote computer).
What are the main reasons suggested for using JSTL tags and JSP expression language in JSP pages in preference to Java scriptlets and expressions?
There are two main arguments for this. First, it helps to separate processing (Java code in servlets) from presentation (JSP pages using tags and EL). This may help maintenance in complex applications – for example, presentation aspects could be changed without any accidental changes to processing if the two are adequately separated. It can also be hard to read source files containing complex interleaving of tags and Java code. Second, it allows web designers familiar with HTML and XML, but not with Java, to produce and maintain JSP pages without any Java programming.
What are the three main components of the MVC approach?
They are the model, the view and the controller. The model represents business data and associated business logic. The view makes the contents of the model visible. The controller regulates the overall behaviour of the application.
How can the MVC approach be applied in web-based information systems using Java EE?
In a Java EE application the model typically uses EJBs and JavaBeans. In web applications the view component normally refers to constructing one or more dynamic web pages using JSP. The controller may receive user inputs directly or forwarded via view components. In a web application the user inputs are mainly HTTP GET or POST requests – typically the controller will consist of one or more servlets. As a result of these requests, it may interact with the model, potentially invoking actions by the model such as updating some data. It also decides which view components are displayed in response to requests and any resulting internal operations.
What is the primary role of servlets in a web-based information system?
Servlets are useful for more complex processing, where the static content of the response is very limited or is delegated to another component.
When should you use JSP?
JSP is particularly suited to constructing dynamic web pages in situations where the output is predominantly static HTML or XML.
How can servlets and JSP be combined in complex web-based systems?
Both technologies are used by a server to create a web page in response to a client request. Servlets and JSP are complementary in their strengths and weaknesses, and most systems of significant complexity would require a combination of both. Servlets can invoke JSP pages and vice versa so that both can carry out the aspects of the response to which they are best suited. The MVC pattern is usually recommended as a guideline for the roles of servlets and JSP in web applications.