MY mENU


Saturday, 17 March 2012

Servlet Questions- Part4

What is the important packages in Servlet Api ?
            According to JAVA language API means set of classes & interfaces that comes in the form of packages.  Servlet –API:
1. javax.servlet:  (This package definers several classes and interfaces to develop sevlets from the scratch irrespective of any protocol )

2. javax.servlet.http: (This package defines several classes and interfaces to develop http based servlets.)


I can access internet hosted websites by typing domain name like www.myjavahub.com.

But our class room web-applications are not accessed in the same manner more over we are typing technical URL(http://localhost:8080/webappname/url) to access the web-resource of web-application can you explain the answer?            
      The ISP[Internet service provider] maintains a special registry called ”DNS registry” containing domain names mapped with the URL’s to open the home pages of website. When user gives request to web-site by typing “domain name” the actual URL. will be collected from the DNS registry using that URL it interacts with real web-resource of web-application using the internet network. Due to this reason we can just type domain name which acts as a logical name to access web-sites while working with websites that are hosted on the Internet. In class room level web-application since we don’t take the help of domain naming registry so we need to type complete URL having port no web-application name & etc technical details to interact with web-resources. Protocol is a set of rules followed by both parties who want to participate in communication.

http is a protocol that is given to transfer hypertext [text with hyper links ] between web browser software & web server software, vice-versa.

Can we write a default constructor for servlet ?
 Yes.But not recommended.  Because by default container generating default constructor.so we no need to write explicitly.

what is the use of HttpServletRequest ?
1. retrive html form parameres
2. retrive html header inforamtion
3. retrive cookies.
4. retrive html form parameres

 What is the use of HttpServletResponse object ?
Http response:
1. S: http response status code
2. C: http rensonse content
3. H: http response headers.
4. add cookies to the response.

How to refresh the webpage generated by servlet automatically at regular intervals?
response.set header(“refresh”,”5”);
by mention above statement , the browser will refresh automatically for every “5” seconds.

What is a buffer/cache & how to disable the buffer available in the browser while displaying response of a web-resource [servlet] in the browser window?Buffer is a temporary memory residing in browser & stores the response data given by web-server
By default browser enable buffering while receiving response from a web-server.
To disable this we can use “cache-control/pragma” response Headers as shown below from the service () method of servlet.
res.setHeader(“cache-control”,”no-cache”);//for http1.1 based web server
(OR)
res.setHeader(“pragma”,”no-cache”);//for http1.o based servers.

HttpservletRequest, HttpServletResponse are the interfaces. How did you say request, response parameters of service() methods as objects?     
             An interface reference variable points to implementation class object. Reference variable also becomes indirect object of implementation class.
In any servlet service(request,response) method request is not the object of HttpServletRequest interface, it is the object of class that implements HttpServletRequest interface. Similarly response is not the object of HttpServletResponse interface, it is the object of a class that implements HttpServletResponse. Both these classes will be given by the underlying Web-server/Application server.

what are different mime types are there in servlets ?
The following is the list of some common mime types:
1.text/html à html document
2.text/xhtml à xhtml document
3.text/xml à xml document
4.image/GIF à gif image fiel
5.text/jpeg à jpeg image file
6.application/pdf à pdf file
7.application/jar à jar file

What is the difference between PrintWriter stream object ServletOutputStream object .which are useful to write response content to browser from servlet.
PrintWriter is a character stream & sends the response to browser in the form of characters.
ServletOutputStream is a byte stream & sends the response to browser in the form of binary data.
Both stream objects must be prepared using response object & associated with response object.
We cannot see both stream objects in a single servlet.
To get PrintWriter object the code is
PrintWriter pw = res.getWriter();
To get ServletOutputStream object the code is
ServletOutputStream out = res.getOutputStream();
Character stream object is good for sending text response And Byte stream object is good for sending binary information like images as response.

Note :between these two PrintWriter is best one.

What are the important resources in Servlet WebApplication ?
1.static content
[All the static information we have to place with in the context root either directly or indirectly]
2.jsp pages
3.Servlet classes
4.Deployment Descriptor (web.xml)
5.Tag Liabraries.
6.Jar files
7.Java class Files

How many web.xml files in possible in one web application ?
for every web application we have to maintain only one web.xml and should be placed directly in WEB-INF folder.

No comments:

Post a Comment