JAVA SERVLETS Flashcards
What is a servlet?
- Servlets are java programs that run on a web server, acting as mediators between requests on the different ends of a web application.
- They can be used to process client requests or produce dynamic web pages.
Which IDE is best for developing Servlets?
Servlets can be developed using any IDE. However NetBeans greatly simplifies the development task because the tool automatically creates supporting directories and files.
Draw a diagram illustrating the position of a servlet in a web application
*See notes for diagram
List some of the tasks of servlets within web applications
● Reading explicit data sent by browsers (clients) from the client side, e.g form data.
● Reading HTTP requests sent by clients, e.g., cookies
● Processing data received and generating results
● Sending both implicit and explicit responses as processed to the client.
Explain the difference between how dynamic and static information are handled in a web server
- Web pages are created using html and stored as files on a web server. This works for static information that does not change regardless of who requests it.
- However, for the dynamic content, web servers, alongside data(generated by users) stored in DBs are used.
- Common Gateway Interface was proposed to generate dynamic web content
- Basically static-html and dynamic-CGI
What is CGI in this context?
Common Gateway Interface
What is the difference between GET and POST methods?
GET method requests data for a specified resource while the POST method submits data to be processed to a specified resource.
What is a query string
A URL used to issue the CGI request is known as a query string. It consists of location of the CGI program, the parameters and their values.
Give an example of a query string and explain what all of the symbols mean
E.g to getBalance() in an example application:
http:/www.webserverhost.com/cgi-bin/
getBalance.cgi?accountId=scott+smith&password=tiger
? Separates the program from the parameters
= associates the parameter name and value
& separates parameter pairs
+ denotes a space character
What is the servlet API?
The servlet Application Programming Interface provides the interfaces and classes that support servlets.
What are the groupings of the interfaces and classes in the java servlet
These interfaces and classes are grouped into two packages, javax.servlet and javax.servlet.http.
What methods are in the javax.servlet.Servletinterface/ the life-cycle methods
- The init() is called when the servlet is first created and is not called
again as long as the servlet is not destroyed. This resembles an
applet’s init(), which is invoked after the applet is created and is not
invoked again as long as the applet is not destroyed.
/** Invoked for every servlet constructed */
public void init() throws ServletException; - The service() is invoked each time the server receives a request for
the servlet. The server spawns a new thread and invokes service.
/** Invoked to respond to incoming requests *
public void service(ServletRequest request,
ServletResponse response)
throwsServletException, IOException;
3. The destroy() is invoked after a timeout period has passed or as the
Web server is terminated. This method releases resources for the
servlet.
/** Invoked to release resource by the servlet */
public void destroy();
Draw a diagram to show how the JVM uses the init(), service(), and destroy() methods to control the
servlet.
*See notes for pic
Describe The GenericServlet Class
The javax.servlet.GenericServlet class defines a generic, protocol-independent servlet. It implements javax.servlet.Servlet and javax.servlet.ServletConfig. All the methods in Servlet and ServletConfig are implemented in GenericServlet, except the service().
Describe the ServletConfig Interface
ServletConfig is an interface that defines four methods
(getInitParameter(),getInitParameterNames(),
getServletContext(), and getServletName()) for obtaining
information from a Web server during initialization.