JAVA SERVLETS Flashcards

1
Q

What is a servlet?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which IDE is best for developing Servlets?

A

Servlets can be developed using any IDE. However NetBeans greatly simplifies the development task because the tool automatically creates supporting directories and files.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Draw a diagram illustrating the position of a servlet in a web application

A

*See notes for diagram

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

List some of the tasks of servlets within web applications

A

● 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Explain the difference between how dynamic and static information are handled in a web server

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is CGI in this context?

A

Common Gateway Interface

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the difference between GET and POST methods?

A

GET method requests data for a specified resource while the POST method submits data to be processed to a specified resource.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a query string

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Give an example of a query string and explain what all of the symbols mean

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the servlet API?

A

The servlet Application Programming Interface provides the interfaces and classes that support servlets.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the groupings of the interfaces and classes in the java servlet

A

These interfaces and classes are grouped into two packages, javax.servlet and javax.servlet.http.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What methods are in the javax.servlet.Servletinterface/ the life-cycle methods

A
  1. 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;
  2. 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();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Draw a diagram to show how the JVM uses the init(), service(), and destroy() methods to control the
servlet.

A

*See notes for pic

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Describe The GenericServlet Class

A
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().
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Describe the ServletConfig Interface

A

ServletConfig is an interface that defines four methods
(getInitParameter(),getInitParameterNames(),
getServletContext(), and getServletName()) for obtaining
information from a Web server during initialization.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Describe the doPut, doOptions and doTrace methods

A

• 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.

17
Q

What is the signature of the doPut, doOptions and doTrace methods

A

protected void doXxx(HttpServletRequest req,
HttpServletResponse resp)
Throws ServletException, java.io.IOException

18
Q

What is the default implementation of the doPut, doOptions and doTrace methods

A
• 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.
19
Q

Can one create a class directly from the HttpServlet class? If yes/no explain why

A
  • 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.
20
Q

Give a diagrammatic depiction of the servlet app

A

*See notes for pic

21
Q

Describe the HttpServletRequest parameter of the doXxxmethods in the HttpServlet class?

A

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.

22
Q

Describe the ServletRequest

A

ServletRequest defines a more general interface to

provide information for all kinds of clients.

23
Q

Describe the HttpServletResponse parameter of the doXxxmethods in the HttpServlet class?

A

It is
an object that assists a servlet in sending a response to the client.
•HttpServletResponse is a subinterface of ServletResponse.

24
Q

Write some code to show a sample servlet creation

A

*See notes for code
*Q1 1D April 2019
*Q5 July 2017
Q? July 2018

25
Q

Describe HTML forms

A

• HTML forms enable one to submit data to the
web server in a convenient form.
• It may contain text fields, radio buttons,
combo boxes, lists, check boxes, text areas
and buttons.

26
Q

Describe the process of obtaining parameter values from HTML forms.

A

• Servlets can be used to obtain parameter values
from HTML forms and display their values.
• Each GUI component in the form has a name
attribute.
• To obtain the values of these, a servlet is created
and uses the name attribute in the
getParameter(attributeName) method to
return the parameter value as a string

27
Q

Describe Database Programming in Servlets

A
  • Many dynamic web applications use databases to store and manage data.
  • Servlets can connect to any relational database via JDBC.
  • Connecting a servlet to a database is no different from connecting a Java application or applet to a database.
  • Servlets, in most cases, therefore implement databases for dynamism with regard to the functions of each application.
28
Q

Explain how the use of servlets in building web applications achieves better performance

A

Java Servlets often serve the same purpose as programs implemented using the Common Gateway Interface (CGI). But Servlets offer several advantages in comparison with the CGI.

Performance is significantly better.

Servlets execute within the address space of a Web server. It is not necessary to create a separate process to handle each client request.

Servlets are platform-independent because they are written in Java.

Java security manager on the server enforces a set of restrictions to protect the resources on a server machine. So servlets are trusted.

The full functionality of the Java class libraries is available to a servlet. It can communicate with applets, databases, or other software via the sockets and RMI mechanisms that you have seen already.