Saturday, June 21, 2014

Servlet Interview Questions

1.What is an web application ?

Ans: A web application is a server side application runs on a server and provides services to unlimited clients across the internet.
(OR)
Aweb application is server side application which process a client request and provides response in the form of a web page on the browser.

2.What are the types of web resources?
        1.static /passive web resource
         2.dynamic /active web resource

3.what is static and dynamic web resources give examples?
Which resources are executed in browser  that resources are called static web resources EX:HTML,javaScript………
Which resources are executed under server that resources are called dynamic web resources Ex.servlets and jsp

4.What is directory strcture of web application


5.Why servlet technology is intruduced?

                                      
Before servlet technology there was a technology called CGI(commn gateway interface)to develope server side web application.it has two following disadvantages

1,CGI technology is failed to provide security for the business of the application
2.CGI technology increased burdon of a server

SERVLET technology
To overcome the above two problems servlet technology is developed

7.what is a SERVLET?

Servlet  is a java class ,it runs at server and provide services to multiple number of clients across the internet
(OR) A servlet is a java class ,which knows how to process the request and how produce a response for a request

8.how many ways java  applets are divided?
1.client side applets
2.server side applets
·          Client side applets are used for developing GUI application
·         Server side applets are used for developing application logic

9.What is the difference between APPLET and SERVLET?     
APPLET                                                                      SERVLET
1.An applet runs on browser OR    1.servlet runs on server
An apletviewer
2.an applet increases browser          2.servlet increases  server functionality
Functionalities
3.an applet visiable on a brows           3.a servelet invisiable in server because
Because an applet as heigh and weidh      servlet doesn’t contain heigh and width     

10.What is servlet API?        1.javax.Servlet
                                               2.javax,Servlet.http

11.differentv ways of creating a servlet?
1.create a java class by implementing servlet interface
EX; public class MyServlet implements Servlet
2.create a java class by extending GenericServlet
EX; public class MyServlet extends GenericServlet
3.create a java class by extending HttpServlet



12.Why  GenericServlet is an abstract class?
GenericServelet is an abstract class because it has one abstract method and it is implemented from Servlet interface .so a class contains at least one abtract method that is we hava define abstract
Note ;an abstract class may or may not contain abstract methods.it means an abstract class contain concret methods or all abstract methods or mix of both
13,life cycle methods of servlet?and non life cycle methods of servlet
Life cycle methods
1.public void init(ServletConfig config) throws ServletException
2.public void service(ServletRequest req,ServletResponse res) ) throws ServletException,IOException
3.public void destroy()
Non life cycle methods
                                                                                                      1.init()
                                                                                                      2.doXXX() methods like doGet() and doPost()………..

14.where the life cycle methods are implemented?
·         The three life cycle methods are provided by javax.servlet.Servlet interface
·         The life cycle methods are implemented in Servlet interface

Note ;every servlet class is either directly or indirectly implements servlet interface

15.how many times the  init(),service(),destroy() methods called to multiple requests?

·          Init(),destroy() methods are called only once  for multiple requests
·         Service() method called by each request means multiple times

16. servlet is a singleton class or not?

Servlet is not a singleton class  because it will create more then one object .in case of singleton class only one object is created
Note
In a server ,for a servlet class their can be more than one object also created.another object of a servlet is created,whenever a simultaneous thread limlt is crossed
EX;in tomcat server maximum of 150 threads limit.ofter this limit one more object is created

17.when  servlet object is created?

Whenever a servlet gets first request from browser
Note
Actually a servlet class can be created in oe way that is either by directly or indirectly implementing servlet interface

18 what is servlet configuration?

Servlet configuration is nothing but to manage the xml fle in web container.we use folloing tags
1.<servlet>
2.<servlet-mapping>
Every servlet class must be registerd with the following three names in web.xml file
1.fully qualified class name of servlet
2 alias name of a servlet
3.url pattern of servlet

19 what is deployeement descriptor?
Web.xml file is called deployeement descriptor

20.what is default port number of tomcat and hoe to change it?

Tomcat default port number is 8080 .to change this port no go to following path
C;/program files/apche s/w foundation/toncat7/conf/server.xml

21 what are the steps to execute a servlet application?

1.create a web application directory strcture
2.develope a  servlet java program
3.configure a servlet using web.xml file
4.compaile the servlet java program
5 deployee the web application in to  web apps folder in  server
6 start the server
7open the browser type the request

22 why second  request response is faster than first request response to a servler?
When first request is made then a  web container do’s the following
1creates the object
2.intilize the object
3.provide a service to object
This above steps takes some time
When the second request is made then already container object is available in the server so directly provides service to request  it will take less time so second request gets faster response then first request

23 why every servlet class must be public?
In a server a container is creates an objects of a servlet class by calling newi] Instance()
New Instance() can create an object for a class,if that class is public class so every servlet class must be  a public class
24 what are the kind of deployeements in web application?

We have three kinds of deployeements in web application
1 hard deployeement
2.console deployeement
3 tool-based deployeement

25 HTML to servlet communication?

1 communicating through an hyperlink(staic input values)
2 communicating through submit button(dynamic  input  values)
3communiating through java script code(asynchronous communication)

26 can you configure more then one url pattern for a servlet in web.xml (or) not?

Yes,we have following two approaches
1.<servlet-mapping>
<servlet-name>sone</servlet-name>
<url-pattern>/srv1</url-pattern>
<url-pattern>/srv2</url-pattern>
.</servlet-mapping>
2 nd way
.<servlet-mapping>
<servlet-name>sone</servlet-name>
<url-pattern>/srv1</url-pattern>
.</servlet-mapping>
.<servlet-mapping>
<servlet-name>sone</servlet-name>
<url-pattern>/srv2</url-pattern>
.</servlet-mapping>

27  can  you add any new sub tag in a <servlet-mapping> tag (or) not?
No,the only tags allowed  into <servlet -mapping >is
<servlet-name>and<url-pattern>  tags

28 why <load –on-stratup> tag?

This tag is inform to container to create my servlet object immediately without waiting for request

29 where will you placed <load-on-startup> in web.xml?

Under the <servlet>tag we placed this tag in web.xml

30 when we will give ,<load-on-startup> tag value is negative then container throws an exception or not?

Will not thrown exception,but it will ignores(waiting) to create object
Waits for first request

What method is used to  specify a containers Layout?      setLayout()
31 how to read environment variables from a servlet?
Servletconfig.getparameter(string pram-name)

32 what is the difference between http Get method and post method?
Get method                                                      Post method
1 .By using Get method we can          1.By using post method we can
Send limited amount of data from     send any size of data at a time
Browser to server. This method              from browser to server.it is
Canot send more then one 1KB           suitable for sending more amounts
Of data at a time                                    of data to server
2.this method appends the data         2.this method doesn’t append the
Entered in a form to the url of             form data the url in address bar
Address bar .it will show the secrect      so this method is secured then
Data like password also so this             Get method
Method is not a secured method
3. this method is doesn’t support             3.this method supports file
File uploading                                            uploading
4.Get method request is                            4.post method request is called
Called idempotent request                        an non-idempotent request

33 what is idempotent request and non idempotent request?
1.An idempotent request means ,a request sent to server to get a
File or page from server
2.Non-idempotent request mens,a request given to server for doing
Some processing(calculations)at server side

33 what are the archive files in jdk?
We have three archive files in jdk
1.jar(java archive)
2.war(web archive)
3.ear(enterprise archive)=jar+ war

34.what is default servlet?
If a servlet is configured in web.xml  with url pattern as /(forward slash)then that servlet is called default servlet
In web.xml only one servlet is act as a default servlet

35.when  container calls default servlet?
For a given request if no servlet is matched then the containe calls
Default servlet

36.where servlet is configured and who wiil take care about servlet?
Servlet is configured in web.xml file.
The web container take care about of servlet

37 can you use same url patterns in web.xml file or not?
No,because url patterns are uniqe In web.xml file

 38 what are the types of url-patterns ?
Ø  Exact match(means typed url-pattern and in web.xml url-pattern must be same)
Ø  Directory match(starts with \ and ends with*)
Ø  Extension match(starts with * .any name)
Ø   
39 What many methods to read request parameters?
1.getParameter()
It is used to read only one request parameter at a time
Syntax: public String getParameter(paramname)
2.getParameterValues()
It is used to read single and multiple values at a time
Syntax:public String[] getParameterValues(paramname)
3.getParameterNames()
It is used to read all parameters names of a servlet request
Syntax:public Enumaration getParameterNames()
4. GetParameterMap ()
It is used to store all request parameters keys, and their values into a
Collection of type Map
Syntax: public Map getParameterMap ()

40 can we write a parameterized constructer in a served class or not?
Sometimes yes and some time no.
1.every servlet class must be a public zero argument constructer
2.if we explicitly add a zero argument constructer then we can also add a paramterized constructer to a servlet class but without writing zero argument constructer we cant write parameterized constructer in a servlet class

41 can we write main() method in a servlet class or not?
Yes,but main() method is not called by the container.becoz it is not a life cycle method of servlet.the container only calls life cycles methods of servlet

42.can we write any user defined methods in a servlet class or not?
Yes,but user defined methods will not called by the container.becoz those are not a life cycle methods of servlet

42 what is life cycle init method and non life cycle init method?
Life cycle init method contains a parameter init(ServletConfig comfit).it is given by Servlet interface.non life cycle init method contains no parameter like init().it is given by GenericServlet.

43. why HttpServlet made as abstract class  without abstract methods?
If Httpservlet is not made as abstract then a servlet programmer can create any number of objects it increases the burdon of server so,in order to restrict programmers from creating objects ,servlet technology developers made as HttpServlet as an abstract class.

44.If a servlet doesn’t contain doGet() method for a get request or doPost() method for a post request then what happens?
If there is no suitable method in our class then container indirectly calls base class doGet(0 or doPost() methods.the base class doGet() or doPost()methods raises Http status 405 error message on browser.

45 In HttpServlet how many init() and service() methods are there?
Two init () and two service () methods are there in HttpServlet class

46.Is HttpServletRequest, HttpServletResponse are classes OR interfaces?
These are interfaces of javax.servlet.* package

47.Is there any relationship between ServletRequest and HttpServletRequest?
Yes,HttpServletRequest is derived from ServletRequest.similarlly HttpServletResponse is derived from ServletResponse

48.In service(request,response) life cycle method req and res are objects OR references?
R eferences.
Internally a container creates objects of implementation classess of ServletRequest and ServletResponse interfaces ofter that a container passes those objects to service method.service method receives into req and res references

49.In java can we find  an implementation class name by using an interface reference OR not?
Yes we can find implementation class name.we need to call getClass().getName() on interface reference
EX:String str=request.getClass().getName();

50.what is diference between  web server and application server?
Web server                                                    Application server
>It is a small server                                    >It is a big server                                                                                           
>It provides only web container.so        >It provides both web and ejb
It supports only servlets and jsp            containers so it supports
Tehnologies                                              servlets,jsp and ejbs
>It has no managed servers                   >it has managed servers                                                                                     
>It as less resources and services        >it as more resources and services                                                                        
>It allows war files has deployable      >It allows war ,er and jar files as  deployable
                                                                      
51.what is the deference between constructer and life cycle init() method of a servlet?
ServletConfig object is not accessable in constructer but aceessable to init() method.in a servlet to inilized an objet init method is better then contructer

52.when overriding a base class method in  derived class we can add a checked exception for throws statement or not?

No.We canot add checked exception,but we can a unchecked exception
53 what is the difference b/w init parameters and context parameters?

b)init parameters are configured inside the servlet tag but context parameters are configured outside of a servlet tag
a)init parameters are local to a servlet but context parameters are global to a servlet

54.what are the different types of parameters are configured in web.xml?
         1.init parameters
        2.context parameters

55.what  are the different types of parameters in servlet?                                                                                          
     1.request parameters
    2.init parameters
    3.context parameters

56. what are the differences b/w ServletConfig and ServletContext objects?
ServletConfig                                                                        ServletContext
1.config object is local object                  1.context object is global
to a servlet                                                      object to a servlet
2.the ratio b/w config object                  2.the ratio b/w context
and servlet object is 1:1                               object and servlet object
3.config object doesnot exists                        is 1:n   
 Without a servlet object                       3.context object exist without                                                                            
4.config objects doesn’t                          a servlet object
Store  attributes                                      4.context object stores  both                
                                                                 Parameters and attributes

57. How can we create ServletContext object?
           1 ServletContext ctx=getServletContext();
         2 ServletContext ctx=config.getServletContext ();(if we write life cycle init method use this otherwise go for one)

58 How can we get ServletConfig object ?
1.super.init(cg);
2.ServletConfig cg=getServletConfig();

59.what are the different MIME types in servlet?
       1.text/html --------àhtml document(default mime type)
       2.text/xml-----------àxml document
      3 text.xhtml----------àxhtml
      4.image/gif-----------àgif image file
       5.application/jar----àjar file
      6 application/pdf-----àpdf file
      7 application/und.ms-excel----àms excel
      8.application/octet.stream-----à(common mime type for all types responses)

60 . What are the different types of communication in servlets?
 We have 4 types of  communication in servlet
                                                                                    1. Browser to servlet communi……
                                                                           2. Html to servlet communi…………
                                                                           3. Applet to servlet communi……..
                                                                            4. Servlet to sevlet communi………         

61. What is servlet collaboration? OR how many ways two servlets can communicate?                                                                                                                                  
Communication between two servlets is servlet collaboration.two servlets can communicate in three ways
   1. RequestDispatcher forward () and include() methods
  2. using sendRedirect() method of Response object

62.How many  ways to get Request Dispatcher object ?
We have three ways to get RequestDispatcher,
                                                                       1.by using Request interface
RequestDispatcher rd =request.getRequestDispatcher(“request relative path”);
2.by  using Context interface
      RequestDispatcher rd=context.get RequestDispatcher(“/context releative path”);
3.by using Context interface method of getNamedDispatcher
                                                                       RequestDispatcher rd=context.getNamedDispatcher(“alias name of servlet”);

63. What is the diff b/w request based getRequestDispatcher () and  context based getRequestDispatcher()?
In request based method we pass request relative path as parameter but in context based we pass context relative path as a parameter

64. what is the diff b/w forwarding and redirecting?
Forwarding
1.In this ,client doesn’t no About forwarding
2.in this only one pair of (req,res) objects are created shared by source and destination servlets
3.in this along with the request ,paraneters also qutomatically forwards to destination
4.In this to forward any req the source and destination servlets must be in same server
5.In this the deatination should be a java type(servlet,jsp ,html)
redirecting
1.In this ,client knows about redirecting
2.in this separate pair of req and res objects are created by the container for a source and destination servlets
3.in this req is redirected  from a source to destination ,but the parameters will not be redirected
4.to redirect a req, source and destination servlets  in same server or different server
5.In this the deatination should be a java type(servlet,jsp ,html) or non java type

65.can we redirect along with the request or not?
Yes.the parameters are not automatically redirected but we can explicitly attach the parameters ,in the form of a queay string
Ex:reponse.sendRedirect(“srv2?p1=100&p1=200”);

66.How to refresh  the webpage generated by servlet automatically at regular intervals?
                                                                       Response.setHeader(“Refresh”,”5”);
                                                                       The above statement refreshes the browser every 5 seconds

67 can you give me some list of HttpRequestHeader and HttpResponseHeaders
httpRequestHeader
                                                                       1. cookie
                                                                       2.host
                                                                       3.referer
                                                                       4.accept
                                                                       5. accept-language
                                                                       6.accept-encoding
                                                                       7.user-agent
                                                                       8connection
                                                                       9.keep-alive
                                                                       10.if-modified-since
httpResponseHeaders are
                                                                       1.Location
                                                                       2.Refresh
                                                                       3.set-cookie
                                                                       4content-type
                                                                       5.content-length
                                                                       6.content-encoding
                                                                       7.last-modified
                                                                       8.cache-control/param

68.how many no of sessions objects are created at server side?
It depends on the no of clients,always the no of clients and no of session objects are equal

69.can one  client share the data of a session object of another client or not?
NO.
It is possible to share the data of context object by the entire client

70.How can we find a web-site is using url-rewriting technique for session tracking or not?
We can find by observing sessionId  in the url of Address bar

71.Why all real time applications prefer url-rewriting?
A web-sit developer is unknown about whether a client is accepting or rejecting the cookies
Eventhough a browser reject cookies but still session tracking to be executed in a realtime project for this url-rewriting is used

72.What are the scopes in servlets?
                                                                       1.request
                                                                       2.session
                                                                       3.context(also called as application scope)
Request is least scope and context is highest scopes

73. How can u create multiple tomcat insatances?
Copy and past the existing installed tomcat rename to some name  then go to server config file increment port  number by one where u have port


No comments:

Post a Comment