Saturday, December 3, 2011

JSP Servlet interview questions

  • What is a JSP?


Java Server Pages (JSP) is a platform independent presentation layer technology
JSPs are normal HTML pages with Java code pieces embedded in them
A JSP compiler is used in the background to generate a Servlet from the JSP page
The goal of JSP is the simplified creation and management of dynamic Web pages
Make use of Java as a server-side scripting language



  • What are the lifecycle of JSP?

When presented with JSP page the JSP engine does the following 7 phases.
Page translation: -page is parsed, and a java file which is a servlet is created.
Page compilation: page is compiled into a class file
Page loading : This class file is loaded.
Create an instance :- Instance of servlet is created
jspInit() method is called
_jspService is called to handle service calls
_jspDestroy is called to destroy it when the servlet is not required.



  • What is difference between custom JSP tags and JavaBeans?


You define how a tag, its attributes and its body are interpreted, and then group your tags into collections called tag libraries that can be used in any number of JSP files
JavaBeans are Java utility classes you defined.
Beans have a standard format for Java classes
Custom tags can manipulate JSP content; beans cannot



  • What are the implicit objects?

Implicit objects are objects that are created by the web container and contain information related to a particular request, page, or application. They are:
–request
–response
–pageContext
–session
–application
–out
–config
–page
–exception



  • How can I enable session tracking for JSP pages if the browser has disabled cookies?

We know that session tracking uses cookies by default to associate a session identifier with a unique user.
if cookies are disabled, you can still enable session tracking using URL rewriting.
URL rewriting essentially includes the session ID within the link itself as a name/value pair. However, for this to be effective, you need to append the session ID for each and every link that is part of your servlet response. Adding the session ID to a link is greatly simplified by means of of a couple of methods: response.encodeURL() associates a session ID with a given URL, and if you are using redirection, response.encodeRedirectURL() can be used by giving the redirected URL as input. Both encodeURL() and encodeRedirectedURL() first determine whether cookies are supported by the browser; if so, the input URL is returned unchanged since the session ID will be persisted as a cookie

  • What are the common mechanisms used for session tracking?

Cookies
SSL sessions
URL- rewriting
HttpSession
Hidden form fields



  • Explain the life cycle methods of a Servlet.

public void init(ServletConfig config) throws ServletException
public void service( ServletRequest req, ServletResponse res) throws ServletException, IOException
public void destroy()
First the servlet is constructed, then initialized wih the init() method.
Any request from client are handled initially by the service() method before delegating to the doXxx() methods in the case of HttpServlet
The servlet is removed from service, destroyed with the destroy() methid, then garbaged collected and finalized.





  • What is the difference between doGet() and doPost()?

A doGet() method is limited with 2k of data to be sent, and doPost() method doesn't have this limitation.
In GET your entire form submission can be encapsulated in one URL, like a hyperlink.
query length is limited to 255 characters, not secure, faster, quick and easy.
The data is submitted as part of URL
A request string for doGet() looks like the following:
http://www.allapplabs.com/svt1?p1=v1&p2=v2&...&pN=vN

doPost() method call doesn't need a long text tail after a servlet name in a request.
All parameters are stored in a request itself, not in a request string, and it's impossible to guess the data transmitted to a servlet only looking at a request string.
In POST data is submitted inside body of the HTTP request.
The data is not visible on the URL and it is more secure.


  • What is the difference between JSP and Servlets ?

JSP is used mainly for presentation only.
A JSP can only be HttpServlet that means the only supported protocol in JSP is HTTP.
But a servlet can support any protocol like HTTP, FTP, SMTP etc


  • 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. The session is stored on the server

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


  • Explain ServletContext.

Every web application has one and only one ServletContext and is accessible to all active resource of that application.
The servlet context is an object that contains a information about the Web application and container.
 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



  • What is a servlet ?

servlet is a java program that runs inside a web container.

http://faq.programmerworld.net/programming/jsp-interview-questions-answers.html
http://www.allapplabs.com/interview_questions/servlet_interview_questions.htm
http://www.javacertificate.net/jsp_iqns.htm




  • How can I pass variable from one servlet to another?


Add the string into HttpSession instance.Other servlet will be able retrieve it from there.

add the string to http servlet request
HttpServletRequest.getSession() .setAttribute(attrName, value)
HttpServletRequest.getSession() .getAttribute(attrName)

If it is a short lived value, the request is a better place for this information but can expose it to cross site scripting security issues

The session is good if it is state or authentication information.

If it is simply data that needs to be shared, then either a database field or working cache may be the answer

unlike the request, the session lives until the session times out,
so if it is a value that is only needed between two servlets once, the value will continue to reside in the session until it is manually removed or the session is terminated.


If the user name has been entered from a form which is then submitted as an HTTP request, you don't have to do anything except forward the request/response to the second servlet. The user name is still held as a request parameter and can be retrieved by the second servlet using the request's getParameter() method.

if the variable is created by the first servlet, it can be made available to a second ( and subsequent) servlet(s) by attaching it to the request attribute table using the setAttribute() method

Attaching it to the session attribute table is only necessary if the life of the variable needs to extend across more than one request

if you're simply moving the variable from one servlet to another, there's less housekeeping involved if you use the request object to transport it.


  • JavaServer Pages (JSP) Technology Quizzes

http://java.sun.com/products/jsp/learning/quizzes/index.html



  • Jsp Interview Questions -1

http://www.scribd.com/doc/57136912/89-Jsp-Interview-Questions-1



  • M.C.A Computer Aplications Enterprise Java (MC335) : October 2008 Question paper

http://discuss.itacumens.com/index.php?topic=56229.0

No comments:

Post a Comment