Tolal:53 Click:
1
2 3
Servlet Interview Questions And Answers
Page 1
Ques: 1 What is a servlet ?
Ans:
Servlets are the server side programs that runs on the server allow developer to add dynamic content and hand it to the web server and web server sent back to the client. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by Web servers. For such applications, Java Servlet technology defines HTTP-specific servlet classes. Servlets provide component-based, platform-independent methods for building Web-based applications, without the performance limitations of CGI programs.
The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing servlets. All servlets must implement the Servlet interface, which defines life-cycle methods.
Typical uses for HTTP Servlets include :
* Processing and/or storing data submitted by an HTML form.
* Providing dynamic content, e.g. returning the results of a database query to the client.
* Managing state information on top of the stateless HTTP, e.g. for an online shopping cart system which manages shopping carts for many concurrent customers and maps every request to the right customer.
Ques: 2 Can we use the constructor, instead of init(), to initialize servlet?
Ans:
Yes. But you will not get the servlet specific things from constructor. So do not use constructor instead of init(). 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. Plus, all the other servlet programmers are going to expect your init code to be in init().
Ques: 3 What is servlet context ?
Ans:
Servlet context defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file. A servlet context object is one per web application per JVM and this object is shared by all the servlet. You can get and set the Servlet context object. You can use servelt context object in your program by calling directly getServletContext() or getServletConfig().getServletContext().
Ques: 4 What is servlet mapping?
Ans:
Servlet mapping controls how you access a servlet. Servlet mapping specifies the web container of which java servlet should be invoked for a url given by client. For this purpose web container uses the Deployment Decriptor (web.xml) file. Servlets are registered and configured as a part of a Web Application. To register a servlet, you add several entries to the Web Application deployment descriptor.
Servlet Mapping :
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>myservlets.MappingExample</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>com/*</url-pattern>
</servlet-mapping>
A developer can map all the servelts inside its web application.
Ques: 5 What is session?
Ans:
A session refers to all the connections that a single client might make to a server in the course of viewing any pages associated with a given application. Sessions are specific to both the individual user and the application. As a result, every user of an application has a separate session and has access to a separate set of session variables. A session keeps all of the state that you build up bundled up so that actions you take within your session do not affect any other users connected to other sessions.
Ques: 6 What Difference between GET and POST ?
Ans:
Difference between GET and POST :
* Client can send information to the server by two methods : GET and POST.
* The GET method appends parameters (name/value) pairs to the URL. The length of a URL is limited, there is a character restriction of 255 in the URL. so this method only works if there are only a few parameters.
* The main difference between GET and POST is, POST has a body. Unlike GET, parameters are passed in the body of the POST insteat of appending to the URL. We can send large number of data with the POST.
* "GET" is basically for just getting (retrieving) data whereas "POST" may involve anything, like storing or updating data, or ordering a product, or sending E-mail.
* The data send with the GET method is visible at the browser's address bar. Don't send important and sensitive data with GET method, use POST.
* Get is idempotent, means it has no side effects on re-requesting the same thing on the server.
Ques: 7 What mechanisms are used by a Servlet Container to maintain session information?
Ans:
The mechanisms used by a Servlet Container to maintain session information :
a) Cookies
b) URL rewriting
c) Hidden form fields
d) SSL (using HTTPS protocol) Sessions
Ques: 8 What are the different ways for session tracking?
Ans:
The concept of session tracking allows you to maintain the relation between the two successive request from the same client (browser). The browser sends the request to the server and server process the browser request generate the response and send back response to the browser. The server is not at all bothered about who is asking for the pages. The server (because of the use of HTTP as the underlying protocol) has no idea that these 2 successive requests have come from the same user. There is no connection between 2 successive requests on the Internet.
There are three ways of tracking sessions :
* Using Cookies.
* Using URL Rewriting.
* Using Hidden Form Fields.
Ques: 9 What is the difference between ServletContext and ServletConfig?
Ans:
ServletContext and ServletConfig are declared in the deployment descriptor. ServletConfig is one per servlet and can be accessed only within that specific servlet. ServletContext is one per web application and accessible to all the servlet in the web application.
Ques: 10 How can a servlet refresh automatically?
Ans:
We can Refresh Servlet Page by two ways : one through client side and another through Server Push.
Client Side Refresh :
< META HTTP-EQUIV="Refresh" CONTENT="5; URL=/servlet/MyServlet/">
Server Side Refresh :
response.setHeader("Refresh",5);
This will update the browser after every 5 seconds
Ques: 11 What is Server side push?
Ans:
The term 'server push' generally means that a server pushes content to the browser client. Server push is a Netscape-only scheme for providing dynamic web content. Server side push is a mechanism that support real time connection to the server with the advantage of instant updates.
Server side push may be emulated in a number of ways.
* The client polls the server at a certain interval, say every five minutes. This technique is typically used to update news information. The client does this by reloading a page every so often.
* The client uses the 'multipart/x-mixed-replace' content type when sending a response. The content type is expected to send a series of documents one after the other, where each one will replace the previous one. The server might delay between each part, which gives the illusion that the data is being updated after an interval. This technique requires a connection to stay open.
Ques: 12 What is the Max amount of information that can be saved in a Session Object ?
Ans:
There is no limit for amount of information that can be seve into the session object. It depends upon the RAM available on the server machine. The only limit is the Session ID length which should not exceed more than 4K. If the data to be store is very huge, then it's preferred to save it to a temporary file onto hard disk, rather than saving it in session.
Ques: 13 Why should we go for inter servlet communication?
Ans:
Due ot the following reason need inter servlet communication :
* Two servlet want to communicate to complete the shopping cart.
* One servlet handles the client request and forward it to another servlet to do some calculation or add some information to complete the view.
* One servlet want to reuse the some methods of another servlet.
Ques: 14 What is a output comment?
Ans:
A comment that is sent to the client in the viewable page source. The JSP engine handles an output comment as uninterpreted HTML text, returning the comment in the HTML output sent to the client. You can see the comment by viewing the page
source from your Web browser.
JSP Syntax
<!-- comment [ <%= expression %> ] -->
Example 1
<!-- This is a commnet sent to client on
<%= (new java.util.Date()).toLocaleString() %>
-->
Ques: 15 What are the differences between a session and a cookie?
Ans:
Differences between a session and a cookie :
* A cookie is a special piece of data with a bit of associated metadata that an HTTP server includes in an HTTP response. A session is a series of related HTTP requests and responses that
together constitute a single conversation between a client and server.
* Cookies are stored at the client side whereas session exists at the server side.
* A cookie can keep information in the user's browser until deleted, whereas you close your browser you also lose the session.
* Cookies are often used to store a session id, binding the session to the user.
* There is no way to disable sessions from the client browser but cookies.
Ques: 16 What is HttpTunneling?
Ans:
Encapsulating the information into the Http header and directed it to a server at the other end of the communication channel that takes the packets, strips the HTTP encapsulation headers and redirects the packet to its final destination.
HTTP-Tunnel acts as a socks server, allowing you to use your Internet applications safely despite restrictive firewalls and/or you not be monitored at work, school, goverment and gives you a extra layer of protection against hackers, spyware, ID theft's with our encryption.
Reason For HTTP-Tunnel :
* Need to bypass any firewall
* Need secure internet browsing
* Need to use favorite programs with out being monitored by work, school, ISP or gov.
* Extra security for online transactions
* Encrypt all your Internet traffic.
* Need play online games
* Visit sites that you are previously blocked
* Prevent 3rd party monitoring or regulation of your Internet browsing and downloads
* Use your favorite applications previously blocked
* Hide your IP address
* Make it next to impossible for you to identify online.
* Free unlimited data transfer
* Compatible with most major Internet applications
* Secure and virus-free servers
* 99% uptime
* No spam, pop-ups, or banners
In many cases it is not possible to establish a connection between JMS clients and a SwiftMQ message router or between two SwiftMQ message routers if one part stands behind a firewall. In general it exists a proxy server to which it is allowed exclusively to establish an internet connection by a firewall. To avoid this fact, an operation called HTTP tunneling is used.
Ques: 17 Why in Servlet 2.4 specification SingleThreadModel has been deprecated?
Ans:
Tere is no practical implementation to have such model. Whether you set isThreadSafe to true or false, you should take care of concurrent client requests to the JSP page by synchronizing access to any shared objects defined at the page level.
Ques: 18 How can I set a cookie?
Ans:
The following program demonstrate that how to set the cookie :
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) {
response.addCookie(new Cookie("cookie_name", "cookie_value"));
}
public void doGet(HttpServletRequest req, HttpServletResponse res) {
doPost(req, res);
}
}
Ques: 19 How will you delete a cookie?
Ans:
To delete cookie from servlet, get the cookie from the request object and use setMaxAge(0) and then add the cookie to the response object.
Cookie killMyCookie = new Cookie("mycookie", null);
killMyCookie.setMaxAge(0);
response.addCookie(killMyCookie);
Ques: 20 What is the difference between Context init parameter and Servlet init parameter?
Ans:
Context init parameter is accessible through out the web application and declared outside the <servlet>...</servlet> element tag in the deployment descriptor(DD) . Servlet init parameters are declared inside the <servlet>...</servlet> element and only accessible only to the that specific servlet. You can access the context init parameters using the getServletContext() method and Servlet init parameters using the getServletConfig() method.
Declaring the Servlet init parameter in DD :
<servlet>
<servlet-name>My Page</servlet-name>
<sevlet-class>/mypage</servlet-class>
<init-param>
<param-name>email</param-name>
<param-value>jalees786@gmail.com</param-value) </init-param>
</servlet>
Declaring the Context init parameter in DD :
<context-param>
<param-name>webmaster</param-name>
<param-value>jalees786@gmail.com</param-value>
</context-param>
Goto Page:
1
2 3
Servlet Objective
Servlet Objective Questions And Answers
Servlet Interview Questions And Answers
Servlet Interview Questions And Answers
R4R,Servlet Objective, Servlet Subjective, Servlet Interview Questions And Answers,Servlet,Servlet Interview,Servlet Questions ,Servlet Answers