JAVA SERVLETS Flashcards
(28 cards)
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.
Describe the doPut, doOptions and doTrace methods
• doPut() is invoked to respond to a PUT request. Such a
request is normally used to send a file to the server.
• doOptions() is invoked to respond to an OPTIONS
request. This returns information about the server, such
as which HTTP methods it supports.
• doTrace() is invoked to respond to a TRACE request.
Such a request is normally used for debugging. This
method returns an HTML page that contains appropriate
trace information.
What is the signature of the doPut, doOptions and doTrace methods
protected void doXxx(HttpServletRequest req,
HttpServletResponse resp)
Throws ServletException, java.io.IOException
What is the default implementation of the doPut, doOptions and doTrace methods
• The HttpServlet class provides default implementation for these methods. • One needs to override doGet(), doPost(), doDelete(), and doPut() if they want the servlet to process a GET, POST, DELETE, or PUT request. *By default, nothing will be done.
Can one create a class directly from the HttpServlet class? If yes/no explain why
- Although the methods in HttpServlet are all non- abstract, HttpServlet is defined as an abstract class.
- Thus one cannot create a servlet directly from HttpServlet. Instead you have to define your servlet by extending HttpServlet.
Give a diagrammatic depiction of the servlet app
*See notes for pic
Describe the HttpServletRequest parameter of the doXxxmethods in the HttpServlet class?
It is
an object that contains HTTP request information,
including parameter name and values, attributes, and
an input stream. HttpServletRequest is a subinterface of
ServletRequest.
Describe the ServletRequest
ServletRequest defines a more general interface to
provide information for all kinds of clients.
Describe the HttpServletResponse parameter of the doXxxmethods in the HttpServlet class?
It is
an object that assists a servlet in sending a response to the client.
•HttpServletResponse is a subinterface of ServletResponse.
Write some code to show a sample servlet creation
*See notes for code
*Q1 1D April 2019
*Q5 July 2017
Q? July 2018