Servlet Interview Questions
21 - By default servlets and JSP are thread safe or not?
No, Both servlets and JSP are not thread safe.22 - How to include the javascript in a servlet?
There are two ways to add the javascript in a servlet- Wrap JavaScript in println() statements
Syntax:out.println("<HTML><HEAD><title>JavaScriptExample</title>"); out.println("<SCRIPT LANGUAGE="text/JavaScript">"); out.println("function validate() {"); out.println("your code;"); out.println("}"); out.println("</SCRIPT>"); out.println("</HEAD>"); - Add external files containing JavaScript code at compile-time. There are two ways to include external file inside another file :
- Include via ServletContext.getResource()
- Include via RequestDispatcher.include()
23 - How internally the Tomcat server processes the Servlet Request ?
When the client makes the request, then request is goes to the server, but the server cannot handle the servlet class that the request is requesting so the server sends that request to the servlet container then servlet container calls the particular servlet program and executes that servlet class and returns the response to the web server and then web server returns that response to the client.24 - How to create new Session for client?
To create an HttpSession object when the user is visiting the web page for the first time or fetch the session if one already exists, use the methods getSession(true) of the HttpServletRequest objectHttpSession session = request.getSession(true);
25 - Where can <init-param> appear in the DD?
<init-param> comes under <servlet> tag in web.xml, syntax:<servlet> <init-param> <param-name> any name </param-name> <param-value> any value </param-value> <init-param> <servlet>
26 - What is SSI( server side includes)?
Server Side Includes (SSI) is a simple interpreted server-side scripting language.27 - Why do we need ServletConfig object?
An object of ServletConfig is created for each servlet by web container. This object is use to get configuration information from web.xml.You not need to modify the servlet file if the information in web.xml file is modified.
28 - Can we use Single Threaded servlet? What is the implication?
Yes, you can use single threaded servlet also called single thread model. To make servlet single threaded use synchronized block or method so that only one thread can access a variable or method at a time.Single Threaded servlet slow down the speed and it's not a good practice to implement it.
29 - Why genericservlet and httpservlet implement the Serializable interface?
GenericServlet and HttpServlet implement the Serializable interface so that servlet engines can "hybernate" the servlet state when the servlet is not in use and reinstance it when needed or to duplicate servlet instances for better load balancing.30 - What is a RequestDispatcher?
RequestDispatcher object can forward a client request to a servlet, jsp or a HTML file or include the resource itself in the response back to the client. You can make the RequestDispatcher object by using ServletRequest.getRequestDispatcher() method or the ServletContext.getRequestDispatcher() method.31 - How to refresh servlet or jsp page after every 10 seconds
- Set Refresh Header in JSP or servlet
Syntax : response.setHeader("Refresh", "10"); - Set Refresh attribute in Meta Tag
Syntax : <meta http-equiv="refresh" CONTENT="10; URL=http://www.javamad.com/index.jsp/"> //Refresh Another URL
<meta http-equiv="refresh" content="10;"> //Refresh same URL
32 - How to stop servlet or jsp page caching?
To stop servlet or jsp page caching you need set headers for no cachingresponse.setHeader("Pragma","no-cache");
response.setDateHeader("Expires",0);
response.setHeader("Cache-Control","no-cache");
33 - Which protocol is used to communicate between a browser and a servlet?
Both browser and servlet communicate using the HTTP protocol.34 - what is load-on-startup?
load-on-startup is a tag element in web.xml. <load-on-startup> tag specifies that the servlet should be loaded automatically when the web application is started.35 - Can we call a jsp from the servlet?
Yes,by using RequestDispatcher intefaceSyntax : RequestDispatcher rd=request.getRequestDispatcher("/AnyFile.jsp");
rd.forward(request,response);


