Servlets Flashcards

1
Q

What is a web server?

A

A Web server is a computer system that processes request by HTTP. This can refer to the hardware of the software that accepts the HTTP request. The web server store, process and deliver web pages to the clients.

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

What is an application server?

A

An application server is a software framework that provides both facilities to create web applications and a server environment to run them. The server behaves like an extended virtual machine for running applications, handling connections to the database on one side and often connections to the Web client on the other.

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

What is the difference between the application server and the web server?

A

Web server: serves content to the web using http protocol.

Application server: hosts and exposes business logic and processes.

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

What is a servlet?

A

A Java servlet is a Java program that extends the capabilities of a server. They are most often used to process or store a Java class in Java EE and are most often used in HTTP protocol. It is an object that receives a request and generates a response based on that request.

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

How do you deploy a servlet?

A

The servlet specification defines the element which can be specified in the deployment descriptor to make the servlet container load and initialize the servlet as soon as it starts up.

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

What is HTTP?

A

It is Hypertext Transfer Protocol, which is basically structured text.

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

What is a HTTP Request?

A

When your computer is on the browser and tries to fetch a file from a web server, it sends a request for some file.

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

What is a HTTP Response?

A

The Web Server sends back a response by sending back a file that can be used to display the web page.

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

What is Web.xml?

A

It is the configuration file of web application in Java. It instructs the servlet container which classes to load, what parameters to set in the context and how to intercept requests coming from the browsers.

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

What is HTTP GET

A

GET is used to retrieve remote data

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

What is HTTP Post

A

POST is used to insert/update remote data

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

Difference between HTTP GET and POST

A

GET is for retrieving data. It should have no side effects, you should be able to request the same URL over and over harmlessly.

POST is for writing data. It may have side effects. Making multiple identical write requests will likely result in multiple writes. Browsers typically give you warnings about this. POST is not secure. The data is in the body of the request instead of the URL but it is trivially simple to view/edit.

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

Servlet Class Hierarchy

A

Servlet Interface
Generic Servlet
HttpServlet
(User created Servlet)

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

What is the Servlet life cycle?

A

Init
Service
Destroy

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

What is the Servlet Init method?

A

The init() method simply creates or loads some data that will be used throughout the life of the servlet.

public void init() throws ServletException {
  // Initialization code...
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the Servlet Service method?

A

The service() method is the main method to perform the actual task. The servlet container (i.e. web server) calls the service() method to handle requests coming from the client( browsers) and to write the formatted response back to the client.

public void service(ServletRequest request,
ServletResponse response)
throws ServletException, IOException{
}

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

What is the servlet destroy method?

A

This method gives your servlet a chance to close database connections, halt background threads, write cookie lists or hit counts to disk, and perform other such cleanup activities.

public void destroy() {
    // Finalization code...
  }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What are the 4 scope in a servlet?

A

Page
Request
Session
Application

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

What is the Page scope?

A

Page: ​The page scope restricts the scpoe and lifetime of attributes to the same page where it was created.

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

What is the Request scope?

A

Request: Request scope start from the moment an HTTP request hits a servlet in our web container and end when the servlet is done with delivering the HTTP response.

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

What is the Session scope?

A

Session: A session scope starts when a client (e.g. browser window) establishes connection with our web application till the point where the browser window is closed.

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

What is the Application scope?

A

Application: Context scope or application scope starts from the point where a web application is put into service (started) till it is removed from service (shutdown) or the web application is reloaded. Parameters/attributes within the application scope will be available to all requests and sessions.

23
Q

What is instantiate?

A

Instantiation means defining or creating new object for class to access all properties like methods, fields, etc. from class.

24
Q

What is initialization?

A

Initialization means assigning initial value to variables after declaring or while declaring. All variables are always given an initial value at the point the variable is declared. Thus, all variables are initialized.

25
Q

What is RequestDispatcher.Forward?

A

A forward is performed internally by the application (servlet). the browser is completely unaware that it has taken place, so its original URL remains intact
any browser reload of the resulting page will simple repeat the original request, with the original URL

26
Q

What is HttpServletResponse.sendRedirect?

A

A redirect is a two step process, where the web application instructs the browser to fetch a second URL, which differs from the original
a browser reload of the second URL will not repeat the original request, but will rather fetch the second URL
redirect is marginally slower than a forward, since it requires two browser requests, not one
objects placed in the original request scope are not available to the second request.

27
Q

What is the difference between RequestDispatcher.Forward and HttpServletResponse.sendRedirect?

A

An important difference between RequestDispatcher.forward() and HttpServletResponse.sendRedirect() is that forward() is handled completely on the server side while redirect() sends a redirect message to the browser. In that sense, forward() is transparent to the browser while redirect() is not. Both javax.servlet.ServletContext and javax.servlet.ServletRequest have the method getReqeustDispatcher(path). Response.sendRedirect( ) does not forward the request and response objects, therefore request parameters will go out of scope. Response.sendRedirect can send a user outside of the context of the web application (to another site)

28
Q

What is a Web container?

A

A Web Container is the component of a web server that interacts with Java servlets. A web container is responsible for managing the lifecycle of servlets, mapping a URL to a particular servlet and ensuring that the URL requester has the correct access-rights.

29
Q

What is HTTP Servlet

A

HTTP Servlet: javax.servlet.http.HTTPServlet represents a HTTP capable servlet and is an abstract class that extends GenericServlet. It provides the support for HTTP protocol. It adds a new service () method with the signature: service (HttpServletRequest, HttpServletResponse). For every HTTP method, there is a corresponding method in the HTTPServlet class, e.g.; doGet (), doHead (), doPost (), doDelete (), doPut (), doOptions () and doTrace (). Depending on the HTTP method, the corresponding doXXX () method is called on the servlet.

30
Q

What is Generic Servlet?

A

GenericServlet: javax.servlet.GenericServlet class implements the Servlet interface. It is an abstract class that provides implementation for all methods except the service () method of the Servlet interface. We can extend this class and implement the service () method to write any kind of servlet.

31
Q

What is the Servlet API?

A
javax.servlet.Servlet is the central interface in the Servlet API. Every servlet class must directly or indirectly implement this interface. 
It has five methods: init (), service (), destroy (), getServletConfig () and getServletInfo (). The service (ServletRequest, ServletResponse) method handles requests and creates responses. The servlet container automatically calls this method when it gets any request for this servlet.
32
Q

What is the difference between Generic Servlet and HTTP Servlet?

A

GENERICSERVLET

  1. Can be used with any protocol (means, can handle any protocol). Protocol independent.
  2. All methods are concrete except service() method. service() method is abstract method.
  3. service() should be overridden being abstract in super interface.
  4. It is a must to use service() method as it is a callback method.
  5. Extends Object and implements interfaces Servlet, ServletConfig and Serializable.
  6. Direct subclass of Servet interface.
  7. Defined javax.servlet package.
  8. All the classes and interfaces belonging to javax.servlet package are protocol independent.
  9. Not used now-a-days.

HTTPSERVLET

  1. Should be used with HTTP protocol only (can handle HTTP specific protocols) . Protocol dependent.
  2. All methods are concrete (non-abstract). service() is non-abstract method.
  3. service() method need not be overridden.
  4. Being service() is non-abstract, it can be replaced by doGet() or doPost() methods.
  5. Extends GenericServlet and implements interface Serializable
  6. Direct subclass of GenericServlet.
  7. Defined javax.servlet.http package.
  8. All the classes and interfaces present in javax.servlet.http package are protocol dependent (specific to HTTP).
  9. Used always.
33
Q

How would you preload a servlet?

A

The servlet specification defines the element which can be specified in the deployment descriptor to make the servlet container load and initialize the servlet as soon as it starts up.

34
Q

What is a ServletConfig?

A

ServletConfig object is created by web container for each servlet to pass information to a servlet during initialization.This object can be used to get configuration information from web.xml file.

when to use : if any specific content is modified from time to time. you can manage the Web application easily without modifing servlet through editing the value in web.xml

It provides four methods: getInitParameter (String), Enumeration getInitParameterNames (), getServletContext () and getServletName ().

35
Q

Name some important tags in ServletConfig.

A

Inside web.xml and inside a servlet tag:

init-param
param-name paramName /param-name

param-value paramValue /param-value

/init-param

36
Q

What is a ServletContext?

A

ServletContext interface is a window for a servlet to view its environment. Every web application has one and only one ServletContext and it is available to all the servlets and filters of that application. It is also used by the servlets to share data with one another. A servlet can use this interface to get information such as initialization parameters for the web application or the servlet container’s version. This interface also provides utility methods for retrieving MIME type for a file, for retrieving shared resources, for logging and so forth.

37
Q

How would you forward a request from a Serlvet to a JSP?

A

RequestDispatcher dispatcher = httpServletRequest.getRequestDispatcher (url);

dispatcher.forward (httpServletRequest, httpServletResponse);

38
Q

How would you preload a JSP?

A

In the web.xml tag, instead of using , use to register the JSP. As with a servlet, you can use the tag within the servlet tag.

39
Q

What are some alternatives to using scripting elements in JSP?

A

● JSP Standard Tag Library (JSTL)
● JSP standard action tags (sometimes called “action commands”)
● Expression Language (EL)
● Custom tag libraries
● Tag libraries from an MVC framework such as Struts, Spring MVC, or JSF

40
Q

What are the various types of mark-up tags provided by JSP?

A

JSP provides four major categories of markup tags.
a. Directives – page, include and taglib.

b. Scripting elements - are used to embed programming instructions. JSP provides three types of scripting elements: declarations, scriptlets and expressions.
i. Declarations allow the developer to define variables and methods for a page, which may be accessed by other scripting elements. or declarations . You can declare both methods and variables. ii. Scriptlets are blocks of code to be executed each time the JSP page is processed for a request. iii. Expressions are individual lines of code.
c. Comments –
i. JSP comments - , - the body of the these comments are ignored by the JSP container. When the page is compiled in to a Servlet, anything appearing between these two delimiters is skipped while translating the page in to Servlet source code.
ii. Scripting language comments - - Like JSP comments, scripting language comments will not appear in the page’s output. Unlike JSP comments, though, which are ignored by the JSP container, scripting language comments will appear in the source code generated for the Servlet.
d. Actions

41
Q

What does MarkUp tag do?

A

Oracle JSP Markup Language (JML) tag library, which provides a set of JSP tags to allow developers to script JSP pages without using Java statements. The JML library provides tags for variable declarations, control flow, conditional branches, iterative loops, parameter settings, and calls to objects

42
Q

Can you name some of the implicit objects in JSP?

A

JSP Container exposes a number of internal objects to the page author. These are referred to as implicit objects, because their availability in a JSP page is automatic. These objects are automatically assigned to variable names and are accessible via JSP Scripting elements. Following are the nine implicit objects provided by JSP and they fall in to four main categories:

a. Objects related to a JSP Page’s Servlet
i. page – Page’s Servlet instance(represents the Servlet itself)
ii. config – Servlet Configuration Data

b. Objects concerned with page input and output
i. request – Request data including parameters
ii. response – Response data
iii. out – output stream for page content

c. Objects providing information about the context within which a JSP page is being processed
i. session – user specific session data
ii. application – Data shared by all application pages
iii. pageContext – Context data for page execution

d. Objects resulting from errors
i. Exception – Uncaught error exception

43
Q

What are implicit objects in JSP?

A

These objects are created by JSP Engine during translation phase (while translating JSP to Servlet). They are being created inside service method so we can directly use them within Scriptlet without initializing and declaring them. There are total 9 implicit objects available in JSP.

44
Q

Can you name some of the standard action tags (action commands) in JSP?

A

A standard action is an action tag that has been defined by the JSP standard. Standard actions are associated with the namespace jsp and appear with a prefix jsp:

a. jsp:forward page=”localURL” /
b. jsp:include page=”localURL” flush=”flag” /
c. jsp:plugin is used to generate browser specific HTML for specifying java applets which rely on the Sun Microsystems java plug-in.
d. jsp:useBean, jsp:setProperty and jsp:getProperty

45
Q

What are JSP action tags?

A

JSP Actions lets you perform some action. A standard action is an action tag that has been defined by the JSP standard. Standard actions are associated with the namespace jsp and appear with a prefix jsp:

46
Q

Difference between directives and action

A

Directives are used during translation phase while actions are used during request processing phase.
Unlike Directives Actions are re-evaluated each time the page is accessed.

47
Q

What is a Directive,

A

Directives control the processing of an entire JSP page. It gives directions to the server regarding processing of a page.Directives control the processing of an entire JSP page. It gives directions to the server regarding processing of a page. Directives are used to convey special processing information about the page to the JSP container. Directives are applied at page translation(compile) time. Directives are used to convey special processing information about the page to the JSP container. Directives are applied at page translation(compile) time.

48
Q

Can you name some of the directives in JSP?

A

a. Page directive: . There are a list of attributes supported by the page directive: info, language, contentType, pageEncoding, extends, import, session, buffer, autoFlush, isThreadSafe, errorPage, isErrorPage.
i. The errorPage attribute is used to specify an alternate page to display if an uncaught error occurs while the JSP container is processing the page.
ii. When the isErrorPage attribute is set to true, it indicates that the current page is intended for use as a JSP error page. As a result, this page will be able to access the exception implicity object.

b. Include directive:
%@ include %
c. Taglibrary directive:
%@ taglib uri=”” prefix=”” %

49
Q

What is the difference between a static and dynamic “include?”

A

The static “include” is the result of an include directive and occurs during page translation of the jsp lifecycle. The dynamic “include” is the result of a standard action tag occuring in the body of the jsp. The two jsps are loaded separately and the results summed together at runtime. Dynamic include can accept parameters using jsp:param, whereas static include cannot.

50
Q

What are the different ways of handling a session?

A

Cookies, URL rewriting, and the session object.

URL Rewriting – We can append a session identifier parameter with every request and response to keep track of the session. This is very tedious because we need to keep track of this parameter in every response and make sure it’s not clashing with other parameters.

Cookies – Cookies are small piece of information that is sent by web server in response header and gets stored in the browser cookies. When client make further request, it adds the cookie to the request header and we can utilize it to keep track of the session. We can maintain a session with cookies but if the client disables the cookies, then it won’t work.

HttpSession getSession() – This method always returns a HttpSession object. It returns the session object attached with the request, if the request has no session attached, then it creates a new session and return it.

51
Q

How can you pre-initialize a servlet?

A

Using the ServletConfig object or override the no-arg init( ) method. Never override the init(ServletConfig) method, as this is used by the servlet container to initialize a servlet using web.xml init-param’s.

52
Q

How can you override lifecycle methods of a JSP?

A

Inside a declaration scripting element, you can declare the jspInit( ) or jspDestroy( ) method and supply the initialization code. Example:

53
Q

How would you write a message back to the browser from inside a servlet?

A
PrintWriter out = response.getWriter();
    // Use out.println("…") to send content to the browser.