Java 2 Topic JSP

 Topic JSP


1. Which page directive should be used in JSP to generate a PDF page?

a) contentType

b) generatePdf

c) typePDF

d) contentPDF

View Answer

Answer: a


Explanation: <%page contentType=”application/pdf”> tag is used in JSP to generate PDF.


2. Which tag should be used to pass information from JSP to included JSP?

a) Using <%jsp:page> tag

b) Using <%jsp:param> tag

c) Using <%jsp:import> tag

d) Using <%jsp:useBean> tag

View Answer

Answer: a


Explanation: <%jsp:param> tag is used to pass information from JSP to included JSP.


3. _jspService() method of HttpJspPage class should not be overridden.

a) True

b) False

View Answer

Answer: a


Explanation: _jspService() method is created by JSP container. Hence, it should not be

overridden.


4. Which option is true about session scope?

a) Objects are accessible only from the page in which they are created

b) Objects are accessible only from the pages which are in same session

c) Objects are accessible only from the pages which are processing the same request

d) Objects are accessible only from the pages which reside in same application

View Answer

Answer: b


Explanation: Object data is available till session is alive.


5. Default value of autoFlush attribute is?

a) true

b) false

View Answer

Answer: a


Explanation: Default value “true” depicts automatic buffer flushing.


6. Which one is the correct order of phases in JSP life cycle?

a) Initialization, Cleanup, Compilation, Execution

b) Initialization, Compilation, Cleanup, Execution

c) Compilation, Initialization, Execution, Cleanup

d) Cleanup, Compilation, Initialization, Execution

View Answer

Answer: c


Explanation: The correct order is Compilation, Initialization, Execution, Cleanup.


7. “request” is instance of which one of the following classes?

a) Request

b) HttpRequest

c) HttpServletRequest

d) ServletRequest

View Answer

Answer: c


Explanation: request is object of HttpServletRequest.


8. Which is not a directive?

a) include

b) page

c) export

d) useBean

View Answer

Answer: c


Explanation: Export is not a directive.


9. Which is mandatory in <jsp:useBean /> tag?

a) id, class

b) id, type

c) type, property

d) type,id

View Answer

Answer: a


Explanation: The useBean searches existing object and if not found creates an object using class.


10. Which one of the following is correct for directive in JSP?

a) <%@directive%>

b) <%!directive%>

c) <%directive%>

d) <%=directive%>

View Answer

Answer: a


Explanation: Directive is declared as <%@directive%>.


11. Which of the following action variable is used to include a file in JSP?

a) jsp:setProperty

b) jsp:getProperty

c) jsp:include

d) jsp:plugin

View Answer

Answer: c


Explanation: jsp:include action variable is used to include a file in JSP.

12. Which attribute uniquely identification element?

a) ID

b) Class

c) Name

d) Scope

View Answer

Answer: a


Explanation: ID attribute is used to uniquely identify action element.

13. “out” is implicit object of which class?

a) javax.servlet.jsp.PrintWriter

b) javax.servlet.jsp.SessionWriter

c) javax.servlet.jsp.SessionPrinter

d) javax.servlet.jsp.JspWriter

View Answer

Answer: d


Explanation: JspWriter object is referenced by the implicit variable out which is initialized

automatically using methods in the PageContext object.


14. Which object stores references to the request and response objects?

a) sessionContext

b) pageContext

c) HttpSession

d) sessionAttribute

View Answer

Answer: b


Explanation: pageContext object contains information about directives issued to JSP page.


15. What temporarily redirects response to the browser?

a) <jsp:forward>

b) <%@directive%>

c) response.sendRedirect(URL)

d) response.setRedirect(URL)

View Answer

Answer: c


Explanation: response.sendRedirect(URL) directs response to the browser and creates a new

request.


16. Which tag is used to set a value of a JavaBean?

a) <c:set>

b) <c:param>

c) <c:choose>

d) <c:forward>

View Answer

Answer: a


Explanation: <c:set> is used to set a value of a java.util.Map object.

17. Can <!–comment–> and <%–comment–%> be used alternatively in JSP?

a) True

b) False

View Answer

Answer: b


Explanation: <!–comment–> is an HTML comment. <%–comment–%> is JSP comment.


18. Java code is embedded under which tag in JSP?

a) Declaration

b) Scriptlet

c) Expression

d) Comment

View Answer

Answer: b


Explanation: Scriptlet is used to embed java code in JSP.


19. Which of the following is not a directive in JSP?

a) page directive

b) include directive

c) taglib directive

d) command directive

View Answer

Answer: d


Explanation: command directive is not a directive in JSP.


20. What alternatives exist to embedding Java code directly within the HTML markup of

your JSP page?

A. Moving the code into your session manager.

B. Moving the code into scriptlets.

C. Moving the code into JavaBeans and servlets.

D. Moving the code into a transaction manager.

Ans: C


21. What type of scriptlet code is better-suited to being factored forward into a servlet?

A. Code that deals with logic that is common across requests.

B. Code that deals with logic that is vendor specific.

C. Code that deals with logic that relates to database access.

D. Code that deals with logic that relates to client scope.

Ans: A


22. What is the initial contact point for handling a web request in a Page-Centric

architecture?

A. A JSP page.

B. A JavaBean.

C. A servlet.

D. A session manager.

Ans: A


23. What is the difference between doing an include or a forward with a


RequestDispatcher?

A. The forward method transfers control to the designated resource, while the include

method invokes the designated resource, substitutes its

output dynamically in the display, and returns control to the calling page.

B. The two methods provide the same functionality, but with different levels of

persistence.

C. The forward method is deprecated as of JSP 1.1 and the include method should be

used in order to substitute portions of a dynamic display at

runtime.

D. The include method transfers control to a dynamic resource, while the forward

method allows for dynamic substitution of another JPS pages

output, returning control to the calling resource.

Ans: A


24. JSP stands for __ ?

A. Java Server Pages

B. Java Service Page

Ans: A


25. What is a Hidden Comment in JSP?

A. <%– –%>

B. <%– %>

Ans: A


26. What is a Expression in JSP?

A. <%= %>

B. <% %>

Ans: A


27. pageContext is an implicit object in jsp.

A. TRUE

A. FALSE

Ans: A


28. config is an implicit object in jsp.

A. TRUE

B. FALSE

Ans: A


29. What is the result of adding following header?

response.addDateHeader(“Expires”,-1);

A. To ensure the page does not get cached at the client browsers.

B. To ensure the page is accessible on any date and page doesnt get expired.

Ans: A


30. Which of the following statements is true regarding the scope of ‘request’ in JSP?

A. Objects with request scope are accessible from pages processing the same request

where they were created.

B. All references to the object shall be released after the request is processed; in

particular, if the request is forwarded to a resource in the

same runtime, the object is still reachable.

C. References to objects with request scope are stored in the request object.

D. All of the above.

Ans: A


31. URL encoding is the method of replacing all the spaces and other extra characters

into their corresponding _______ Characters?

A. Hex

B. Binary

C. Octal

D. Decimal

Ans: A


32. Which of the scripting of JSP not putting content into service method of the

converted servlet?

A. Declarations

B. Scriptlets

C. Expressions

D. None of the above

Ans: A


1.What is Servlet?

A servlet is a Java technology-based Web component, managed by a container

called servlet container or servlet engine, that generates dynamic content and

interacts with web clients via a request/response paradigm.


2.Why is Servlet so popular?

Because servlets are platform-independent Java classes that are compiled to

platform-neutral byte code that can be loaded dynamically into and run by a Java

technology-enabled Web server.


3.What is servlet container?

The servlet container is a part of a Web server or application server that provides the

network services over which requests and responses are sent, decodes MIME-

based requests, and formats MIME-based responses. A servlet container also

contains and manages servlets through their lifecycle.


4.When a client request is sent to the servlet container, how does the

container choose which servlet to invoke?

The servlet container determines which servlet to invoke based on the configuration

of its servlets, and calls it with objects representing the request and response.

5.If a servlet is not properly initialized, what exception may be thrown?

During initialization or service of a request, the servlet instance can throw an

UnavailableException or a ServletException.


6.Given the request path below, which are context path, servlet path and path info?

/bookstore/education/index.html

context path: /bookstore

servlet path: /education

path info: /index.html


7.Request parameter How to find whether a parameter exists in the request object?

1.boolean hasFoo = !(request.getParameter(“t2”) == null ||

request.getParameter(“t2”).equals(“”));

2. boolean hasParameter = request.getParameterMap().contains(theParameter);

(which works in Servlet 2.3+)

.8.Can we use the constructor, instead of init(), to initialize servlet?

Yes , of course you can use the constructor instead of init(). There’s nothing to stop

you. But you shouldn’t. The original reason for init() was that ancient versions of

Java couldn’t dynamically invoke constructors with arguments, so there was no way

to give the constructur a ServletConfig. That no longer applies, but servlet containers

still will only call your no-arg constructor. So you won’t have access to a

ServletConfig or ServletContext.


9.What mechanisms are used by a Servlet Container to maintain session

information?

Cookies, URL rewriting, and HTTPS protocol information are used to maintain

session information


10.Difference between GET and POST

In GET your entire form submission can be encapsulated in one URL, like a

hyperlink. query length is limited to 260 characters, not secure, faster, quick and

easy.

In POST Your name/value pairs inside the body of the HTTP request, which makes

for a cleaner URL and imposes no size limitations on the form’s output. It is used to

send a chunk of data to the server to be processed, more versatile, most secure.


11.What is session?

The session is an object used by a servlet to track a user’s interaction with a Web

application across multiple HTTP requests.


12.What is servlet mapping?

The servlet mapping defines an association between a URL pattern and a servlet.

The mapping is used to map requests to servlets.


13.What is servlet context ?

The servlet context is an object that contains a servlet’s view of the Web application

within which the servlet is running. Using the context, a servlet can log events,

obtain URL references to resources, and set and store attributes that other servlets

in the context can use.

.

14.Which interface must be implemented by all servlets?

Servlet interface.


15. Explain the life cycle of Servlet.

Loaded(by the container for first request or on start up if config file suggests load-on-

startup), initialized( using init()), service(service() or doGet() or doPost()..),

destroy(destroy()) and unloaded.


16.When is the servlet instance created in the life cycle of servlet? What is the

importance of configuring a servlet?

An instance of servlet is created when the servlet is loaded for the first time in the

container. Init() method is used to configure this servlet instance. This method is

called only once in the life time of a servlet, hence it makes sense to write all those

configuration details about a servlet which are required for the whole life of a servlet

in this method.


17.Why don’t we write a constructor in a servlet?

Container writes a no argument constructor for our servlet.


18.When we don’t write any constructor for the servlet, how does container create

an instance of servlet?

Container creates instance of servlet by calling

Class.forName(className).newInstance().


19.Once the destroy() method is called by the container, will the servlet be

immediately destroyed? What happens to the tasks(threads) that the servlet might

be executing at that time?

Yes, but Before calling the destroy() method, the servlet container waits for the

remaining threads that are executing the servlet’s service() method to finish.


20.What is the difference between callling a RequestDispatcher using

ServletRequest and ServletContext?

We can give relative URL when we use ServletRequest and not while using

ServletContext.


21.Why is it that we can’t give relative URL’s when using

ServletContext.getRequestDispatcher() when we can use the same while calling

ServletRequest.getRequestDispatcher()?

Since ServletRequest has the current request path to evaluae the relative path while

ServletContext does not.


22. Whats the advantages using servlets over using CGI?

CGI programs run outside the webserver. So a new process must be started to

execute a CGI program. CGI programs are designed to handle a single request at a

time. After that they return the result to the web server and exit.

On the other hand servlets can handle multiple requests concurrently. They run

within web servers. They generate dynamic content that is easier to write and faster

to run.


23. What is Servlets and explain the advantages of Servlet life cycle?

Servlets are the programs that run under web server environment. A copy of Servlet

class can handle numerous request threads. In servlets, JVM stays running and

handles each request using a light weight thread.

Servlets life cycle involve three important methods, i.e. init, service and destroy.

Init()

Init method is called when Servlet first loaded in to the web server memory.

Service()

Once initialized, Servlets stays in memory to process requests. Servlets read the

data provided in the request in the service() method.

Destroy()

When server unloads servlets, destroy() method is called to clean up resources the

servlet is consuming.


24. What is the GenericServlet class?

GenericServlet makes writing servlets easier. To write a generic servlet, all you need

to do is to override the abstract service method.


25. What is the difference between an Applet and a Servlet?

Applets

• Applets are applications designed to be transmitted over the network and executed

by Java compatible web browsers.

• An Applet is a client side java program that runs within a Web browser on the client

machine.

• An applet can use the user interface classes like AWT or Swing.

• Applet Life Cycle Methods: init(), stop(), paint(), start(), destroy()

Servlets

• Servlets are Java based analog to CGI programs, implemented by means of

servlet container associated with an HTTP server.

• Servlet is a server side component which runs on the web server.

• The servlet does not have a user interface.

• Servlet Methods: doGet(), doPost()


26. List out the difference between ServletConfig and ServletContext?

Both are interfaces in the package javax.servlet.

• ServletConfig is a servlet configuration object. It is used by a servlet container to

pass information to a servlet during initialization.

• The ServletConfig parameters are specified for a particular servlet and are

unknown to other servlets.

• The ServletContext object is contained within the ServletConfig object. It is

provided by the web server to the servlet when the servlet is initialized.

• ServletContext is an interface which has a set of methods like getServletName(),

getServletContext(), getInitParameter(), getInitParameterNames(). The servlet uses

to interact with its servlet container.

• ServletContext is common to all servlets within the same web application. So,

servlets use ServletContext to share context information.


27. What is the difference between using getSession(true) and getSession(false)

methods?

getSession(true) will check whether a session already exists for the user. If yes, it

will return that session object else it will create a new session object and return it.

getSession(false) will check existence of session. If session exists, then it returns

the reference of that session object, if not, this methods will return null.


28. Define servlet mapping?

Servlet mapping controls how you access a servlet. It is recommended that you

don’t use absolute URLs. Instead usage of relative URLs should be done.

If you try to deploy the application with a different context root, you might have to

change all the urls used in all the JSP programs. Relative URLs is the solution so

that you can deploy your application with different context root with out changing the

URLs.


29. What is Servlets and explain the advantages of Servlet life cycle?

Servlets are modules that run within the server and receive and respond to the

requests made by the client. Servlets retrieve most of the parameters using the input

stream and send their responses using an output stream.

Servlets are used to extend the server side functionality of a website. They

communicate with various application on the server side and respond to the request

made by the client.


30. List out difference between a JavaBean from a Servlet?

Servlets are Java based analog to CGI programs, implemented by means of a

servlet container associated with an HTTP server. Servlets run on the server side.

Beans are reusable code components written in Java that one can use in a variety of

programming environments. JavaBeans are to Java what ActiveX controls are to

Microsoft. Javabeans can run on server side, client side, within an applet etc.

So, both have nothing in common except Java.

     BEST OF LUCK

No comments:

Post a Comment