You are on page 1of 15

AJP – 22517

CHAPTER 6 ~ SERVLET

3 Name the class that includes the getSession() 18 By default, how long does a cookie last?
method
HttpSession By default, a newly created cookie persists until
the browser exits.

4 A cookie contains _____________


19 By Default, How many variables are
State information of user like name, address available in JSP programs?
6

6 A Deployement descriptor
describes_________________ 20 By default, Tomcat runs on port
web component settings ___________.
8080

8 A JSP is translated to ________________


23 Choose correct scopes into JSP
Servlet
page,request,session,application

11 A servlet is an instance of___________.


24 choose correct syntax of directive:
the HttpServlet class
<%@ directive_name%>

12 A user types the URL


http://www.msbte.com/result.php. Which HTTP 27 Cookies can be deleted by
request gets generated? Select the one correct using………………………….. Method.
answer setMaxAge( )
GET
method
31 Dynamic interception of requests and
responses to transform the information is done
14 Abbreviate the term MIME? by Servlet filter

Multipurpose internet Mail Extension


35 GenericServlet implements the _______ and
_______.
15 Advantage of JSP over Servlet Servlet, servletConfig
is____________
JSP is web page scripting
language and servlets are Java programs 40 How can we get context init parameter and
run some code before rest of the application can
service a client?
17 An object of Which is created by the web
container at time of deploying the project? ServletContextListener
ServletContext
AJP – 22517
CHAPTER 6 ~ SERVLET

45 HTTP servlet uses methods 80 Name the interface which is used to get
like__________________. initialization parameters for Servlet
doPost(),doGet() ServletConfig

52 In Http request method which is non- 81 Name the location of compiled class files
idempotent? within a jar file?
POST method /WEB-INF/classes

57 In which file do we define a servlet mapping? 82 Name the method defined in the
HttpServletResponse class that may be used to
web.xml set the content type.

setContentType
59 In which phase of JSP life cycle ,JSP page
turns into servlet?
JSP Compilation 84 Parameters are present in URL while
using__________method
doGet()
62 javax.servlet package does not contain
following class or interface.
HttpServlet 89 The doGet() Extracts values of the parameter
type and number by using ___________
request.getParameter()
68 JSP includes a mechanism for defining
............................... or custom tags
dynamic attributes 91 The getSession() method with its parameter
[getSession(true)]. It will return the appropriate
session object when ____
74 Life cycle of a JSP page consist of following
steps. the session is existing
Translation,compilation,Loading and
initialization,Request Handling,Destroy
99 The major difference between servlet and
CGI is
78 Name the http method that sends the same Servlets are thread based and CGI is process
response as the request. based
TRACE method

100 The maximum size of data can be sent by


79 Name the http method used to send resources doGet() of HTTP request is
to the server 240 bytes
PUT method

106 These methods


doGet(),doPost(),doHead(),doDelete(),doTrace()
are used in? HttpServlet
AJP – 22517
CHAPTER 6 ~ SERVLET

107 The__________________package contains 146 Which method is called on every servlet


a number of interfaces and classes that are request ?
commanly used by servlet developers service()

javax.servet.http
149 Which method is used to extract cookies
from a request?
111 To get the servlet environment information getCookies()

ServletContext object is used


150 Which method is used to specify before any
lines that use the PrintWriter?
123 What method is used to specify a container's setContentType()
layout in JSP?
setLayout()
160 Which of the following are the session
tracking techniques?
129 Which method returns names of the request URL rewriting, using session object, using
parameters as Enumeration of String objects cookies, using hidden fields
getParameterNames

165 Which of the following is not a server-side


132 Which attribute is mandory while creating technology ?
cookie DHTML
Value

171 Which of the following packages are


133 Which cookie it is valid for single session required to create servlets in java?
only; it is removed each time when user closes javax.servlet, javax.servlet.http
the browser?

Non-persistent cookie 179 Which tag is used to declare one or more


variables or methods
<%!-----%>
137 Which interface define a getSession()
method?
HttpServletRequest 182 _____ Method returns true if the server
created the session and it has not yet been
accessed by the client.
144 Which method decides whether doGet() or isNew()
doPost() method to call?
service
186 ____________ is used to read data from a
client request.
145 Which method does not exist on ServletRequest
HttpServlet class
init()
AJP – 22517
CHAPTER 6 ~ SERVLET

187 ______________ method gives the current


session
getSession() 211 A deployment descriptor describes
________________
web component setting
188 ______________ class provides
functionality that makes it easy to handle
requests and responses. GenericServlet 215 A JSP page needs to generate an XML
file.Which attribute of page directive may be
used to specify that the JSP page is generating
an XML file
189 ______________Defines page-dependent contentType
attributes, such as scripting language, error page,
and buffering requirements.

<%@ page ... %> 221 All the ___________ data is kept at the
application server ___________ data is kept at
the web server?
191 ________________method is secure. dynamic, static
doPost()

192 …………………. Life cycle method


handles all client request. 224 An object of Which is created by the web
service( ) container at time of deploying the project?
ServletContext
193 import java.io.*; import javax.servlet.*;
import javax.servlet.http.*; public class
HelloWorld extends HttpServlet { public void 229 At run time if HttpServletResponse send
doGet(_______________, code 200 using sendError method that means
____________________) throws OK
ServletException, IOException {
res.setContentType("text/html"); PrintWriter out
= res.getWriter(); out.println(""); out.println(" 248 choose the classes of javax.servlet package
"); out.println("
"); out.println("Hello World"); out.println(" ServletException GenericServlet
"); } } What are the parameters for doGet()?

(HttpServletRequest req & HttpServletResponse 253 Choose the correct sequence steps for
res) running the servlet
• Write the servlet source code. We need to
204 1. Find out correct syntax of service() import the javax.servlet package and the
method of servlet class javax.servlet.http package in your source file.
void service(ServletRequest req, • Compile your source code.
ServletResponse res)
• Create a deployment descriptor.
• Run Tomcat.
205 1. Select correct scopes into JSP
• Call your servlet from a web browser.
page, request, session, application
AJP – 22517
CHAPTER 6 ~ SERVLET

303 Cookies and Sessions can be created and


used with: A. Generic servlets B. Http servlets
Select correct option from following. 339 Generic servlet implements the
_________&_______ interface.
Only B Servlet & servlet config

312 Dynamic Interception of request and 340 getSessioin() is a method of __________


responses to transform the information is done class.
by _________ HttpServletRequest

Servlet filter
341 getSession(true) method will return
appropriate session object if……
323 Find the Syntactally Correct Statement the session is existing
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws
ServletException, IOException 342 getWriter method is accessed as
___________
response.getWriter()
330 Following statement means: HttpSession
getSession( )
It will return existing session object, If not it will 360 Identify the correct sequence in Lifecycle of
create a new sesssion. JSP
Compile, Initialize, Execute, Destroy

331 For a given ServletResponse response, 366 In a URL query string, the parameter name
which retrieve an object for writing text data? and value are associated using the ____ symbol.
response.getWriter() E
=

332 For a HttpSession, say session, how do you


get its attribute? 394 In web application running in a webserver
session.getAttribute("lastName") who is responsible for creation request &
response object.
Container
336 For the following code identify the correct
syntax of service () method of servlet class.
import java.io.*; import javax.servlet.*; public 395 In which advantage of servlet, Servlets are
class HelloServlet extends GenericServlet { managed by JVM so no need to worry about
public void memory leak, garbage collection etc.?
service(___________________)throws Robust
ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw = response.getWriter(); 396 In which file do we define a servlet
pw.println("Hello"); pw.close(); } } mapping?
void service(ServletRequest web.xml
req,ServletResponse res)
AJP – 22517
CHAPTER 6 ~ SERVLET

397 In which folder we have to save java file for 464 The getAttribute(String attr) returns
servlet? Returns value of the server attribute
Classes

469 The include() method of RequestDispatcher


398 In which of the following cases the ______.
request.getAttribute() will be helpful.? sends a request to another resource like servlet,
If a servlet or jsp is invoked using forword jsp or html
method

473 The main difference between Generic


399 In which technology, we mix our business Servlet and HTTP servlet is
logic with the presentation logic? Generic servlet is protocol platform independent
Servlet & HTTP servlet having built in HTTP protocol
support

409 JSP includes a mechanism for defining ------


--------- or custom tags. 474 The major difference between servlet and
dynamic attributes CGI is
Servlets are thread based and CGI is process
based
422 public abstract class HttpServlet extends
GenericServlet
478 The Status code _____ shows “Resource
Temporarily Moved”
445 Servlets becomes thread safe by 302
implementing the javax. Sevlet. Single
ThreadModelinterface ___________
as every request is handled by separate instances 481 The _______________method on the
of the Servlet. session object is used to remove a set attribute. ?
removeAttribute(""attributeName")

449 Steps in JSP Page execution


1. Compilation 2. Initialization 3. Execution 4. 482 These methods
Cleanup doGet(),doPost(),doHead,doDelete(),deTrace()
are used in?
HttpServlets
450 Suppose the servlet class named Test does
not have the package statement, by default, you
use ________ to invoke it 483 Through which tag you can define servlet
http://localhost:8080/examples/servlet/Test config in web.xml?
init-param

456 The authentication mechanism in the servlet


specification uses a technique called? 484 To declare the variables in JSP which tag is
Role Based Authentication used
Declaration Tag
AJP – 22517
CHAPTER 6 ~ SERVLET

485 To get the servlet environment information? 535 Which classes are present in
ServletConfig object is used. javax.servlet.http package?
HttpServlet Cookie

486 To Send binary output in the response ,the


following method of HttpServletResponse may 539 Which file will start tomcat Server?
be used to get the appropriate Writer/Stream startup.bat
object ?
getOutputStream()
551 Which method of the request object is used
to extract values of the input fields in a form
487 What are the methods available in when it is submitted?
ServletContextListener interface? getParameter()
contextInitialized((),contextDestroyed()

560 Which of the following are the session


503 What is the difference between cookies and tracking techniques?
Httpsession? URL rewriting,using session object,using
Cookies works at clientside whereas HttpSession cookies,using hidden fields
works at serverside.

572 Which of the following JSP variables are


506 What is the limit of the datato be passed not available within a JSP expression. Select the
from HTML when doGet() is used? one correct answer. ?
2kb Httpsession

514 What the ServletContainer does? 576 Which of these statements are false about
Provides the Runtime environment for servlets. GenericServlet?
Genric Servlet extends HttpServlet and override
doGet() method
528 When using HTML forms which of the
folowing is true for POST method
POST method sends data in the body of the 578 Which retrieves all cookies sent in a given
request HttpServletRequest request?
request.getCookies()

533 Which authentication mechanism employs a


base64 encoding scheme to protect user 588 Which tag is used to execute java source
passwords? code in JSP
HTTP Basic Authentication Scriptlet Tag

534 Which class provides stream to read binary 602 Apache Tomcat is a ________.
data such as image etc. from the request object? Web server that is capable of running Java
ServltInputStream programs.
AJP – 22517
CHAPTER 6 ~ SERVLET

• Servlets are the java programs that basically runs on the severs.
• It is managed by the Servlet container.
• Working of Servlet.
• Client requests servlets via web browser.
• Request is sent to web server.
• Server finds requested servlet.
• Servlet gathers relevant information.
• Server builds web page.
• Web page is displayed to client.
• Client's request is satisfied by servlets.

• Lifecycle of Applet:
- Init() : This method is called only when the servlet is loaded into memory for the
first time.
- Service(): The servlets can then read the data provided by the HTTP request with
the help of service() method.
- Destroy(): This method unload the program from the memory.

• Creating Simple Servlet:


- Either we create a servlet implementing Servlet interface or extending a class that
implements Servlet interface.
- For implementing we must include ‘import.javax.servlet.*’
• GenericServlet class: Predefined Servlet interface implementation.
• HttpServlet class: Child class of GenericServlet class.
• Both can be extended in servlet program writing.
- The servlet program receives a client's request for a service, processes it, and
sends a response back to the client using ‘HttpServletRequest’ and
‘HttpServletResponse’
- Its methods are as follows:
1) doGet(): It requests data from the source.
2) doPost(): It submits the processes data to the source.
• The GET request is more efficient than the POST request.
• The GET request is less secure than the POST request.
- This throws ServletException and IOException.
AJP – 22517
CHAPTER 6 ~ SERVLET

• It uses javax.servlet and javax.servlet.http packages.

a) javax.servlet Package
- This is used to implement the servlet.
- Servlet is the most important interface in this package and every servlet
must implement this interface.
- The interfaces are as follows:-

1) Servlet Interface:
- This interface defines all the lifecycle method of servlet. i.e. init(),
service(), destroy().

2) ServletConfig Interface
- This is used to get various initialization parameters.
AJP – 22517
CHAPTER 6 ~ SERVLET

3) ServletContext Interface
- This is used to get info about the servlet environment.

4) ServletRequest Interface
- This interface is used to get information about the client.
AJP – 22517
CHAPTER 6 ~ SERVLET

5) ServletResponse Interface
- It formulates the response for the clients request.

6) Classes:
- Javax.servlet package contains the following classes
AJP – 22517
CHAPTER 6 ~ SERVLET

7) GenericServletClass
- This is used for implementing the lifecycle of the servlet. It implements
servlet and ServletConfig interfaces.
- It is called "Generic" because it does not assume that the protocol it will
process will be HTTP.

b) Javax.servlet.http package
- This servlet works with HTTP request and response.
- The interfaces and methods are as follows.

1) HTTPServletRequest Interface
- It is use to obtain info from clients http request.
AJP – 22517
CHAPTER 6 ~ SERVLET

2) HttpServletResponse Interface
- It is used to formulate an HTTP response to the client.

3) HttpSession Interface
- The servlet can read and write the information using HTTPsession
interface.

4) Class Cookie.
- A cookie is a small piece of information that is stored in the client’s
machine.
- Sometimes cookies are useful to keep track of the user using the client
machine.
AJP – 22517
CHAPTER 6 ~ SERVLET

5) HttpServlet Class
- It extends GenericServlet and is used when developing servlets that receive
and process HTTP requests

6) HttpSessionEvent class

7) HttpSessionBindingEvent class
1. Object getValue() : This function returns the value of bounded or unbounded
attribute.
2. String getName() : This function returns the name being bound or unbound.
3. HttpSession getSession() : This function returns the session to which the listener
can be bound or unbound.
AJP – 22517
CHAPTER 6 ~ SERVLET

• Cookies
- Cookies are some little information that can be left on your computer by the
other computer when we access an internet.

- public void setMaxAge(int expiry) – Sets the maximum age of the cookie
in seconds.

You might also like