Servlet and JSP
Interview questions
By Sujata Regoti
1.What is difference between include action and include directive in
JSP?
Include directive Include action
Resource included during jsp translation time request time
change on included resource
reflect after
Compilation of JSP (static) Next request (dynamic)
attribute to specify resource file page
Can pass parameter No yes
Example <%@ include file="loan.jsp" %> <jsp:include page="header.jsp">
<jsp:param name="some"
value="thing"/>
</jsp:include>
2.How do you define application wide error page in JSP
//error.jsp
<%@ page isErrorPage="true"%>
//login.jsp
<%@ page errorPage="error.jsp"%>
If any unhandled Exception thrown from login.jsp , error.jsp error page will be
invoked
3.Difference between sendredirect and forward in Servlet
sendredirect forward
Declared in interface HttpServletResponse RequestDispatcher
syntax void sendRedirect(String url) forward(ServletRequest request,
ServletResponse response)
Visible to client yes no
Further processing within Different server or module Same server
Comparison Slower than forward as
completer new request created
Faster than sendredirect
4.What is difference between Web Server and Application Server
Read more: http://java67.blogspot.com/2012/10/servlet-jsp-interview-
questions-answer-faq-experience.html#ixzz49IBh21A6
4
Web Server Application Server
support Only Servlets and JSP distributed transaction and EJB
super Can be subpart of Application
server
Include Web Server
Logical difference Provide http protocol level service Provide web service and expose
business level service
Resource utilization Less so less heavy than
application server
High so more heavy than web
server
Example Apache tomcat Glassfish,JBoss,WAS
5.What is difference between ServletContext and ServletConfig
ServerConfig ServerContext
Available for one per servlet Web application
scope Like local parameter for particular
servlet
global parameter associated with
whole application
Where name value pair defined
in web.xml
inside the servlet section outside of servlet tag
Method to get object getServletConfig() getServletContext()
Example To get Shopping cart details To get the MIME type of a file or
application session related
information
6.What is difference between GET and POST method in HTTP protocol
Read more: http://java67.blogspot.com/2012/10/servlet-jsp-interview-
questions-answer-faq-experience.html#ixzz49IJORF59
Read more: http://java67.blogspot.com/2012/10/servlet-jsp-interview-
questions-answer-faq-experience.html#ixzz49IGWgAiy
GET POST
Passes request parameter In URL string In request body
Limit to send data only pass limited amount of data
depending on browser
large amount of data to server i.e.
no limit
Can bookmarked or catched yes no
Mostly used for view purpose update purpose
Suitable for sensitive and
confidential information
No yes
7.What is difference between URL Encoding and URL rewriting
URL Encoding URL rewriting
purpose Way to pass string to server
containing special characters by
converting special characters
Technique used maintain user
session if cookies are not
enabled or not support
Methods used java.net.URLEncoder.encode()
and
java.net.URLDecoder.decode()
java.servlet.http.HttpServletResp
onse.encodeURL(String url) and
encodeRedirectURL(String URL)
URL changes special character replaced by
another character like space with
‘%20’
session id is appended to URL
8.What is difference between Servlet and JSP
Servlet JSP
What it is Java program can be used to create
dynamic web pages
webpage scripting language that can
generate dynamic content.
Speed faster compared to JSP slower compared to Servlet as it takes
compilation time to convert into Java
Servlets.
MVC part act as a controller act as a view
Custom tag facility Not available Available e.g.tags used to call Java
beans directly
When to preferr more processing and manipulation
involved
not much processing of data required
9.What is war file and how to create?
A war (web archive) file specifies the web elements. A servlet or jsp project can be
converted into a war file. Moving one servlet project from one place to another will
be fast as it is combined into a single file.
The war file can be created using jar tool found in jdk/bin directory. If you are
using Eclipse or Netbeans IDE, you can export your project as a war file.
To create war file from console, you can write following code.
jar -cvf abc.war *
10.What is difference between GenericServlet and HttpServlet?
The GenericServlet is protocol independent whereas HttpServlet is HTTP protocol
specific. HttpServlet provides additional functionalities such as state management
etc.
Thank You
http://www.regotiss.github.io

Servlet and jsp interview questions

  • 1.
    Servlet and JSP Interviewquestions By Sujata Regoti
  • 2.
    1.What is differencebetween include action and include directive in JSP? Include directive Include action Resource included during jsp translation time request time change on included resource reflect after Compilation of JSP (static) Next request (dynamic) attribute to specify resource file page Can pass parameter No yes Example <%@ include file="loan.jsp" %> <jsp:include page="header.jsp"> <jsp:param name="some" value="thing"/> </jsp:include>
  • 3.
    2.How do youdefine application wide error page in JSP //error.jsp <%@ page isErrorPage="true"%> //login.jsp <%@ page errorPage="error.jsp"%> If any unhandled Exception thrown from login.jsp , error.jsp error page will be invoked
  • 4.
    3.Difference between sendredirectand forward in Servlet sendredirect forward Declared in interface HttpServletResponse RequestDispatcher syntax void sendRedirect(String url) forward(ServletRequest request, ServletResponse response) Visible to client yes no Further processing within Different server or module Same server Comparison Slower than forward as completer new request created Faster than sendredirect
  • 5.
    4.What is differencebetween Web Server and Application Server Read more: http://java67.blogspot.com/2012/10/servlet-jsp-interview- questions-answer-faq-experience.html#ixzz49IBh21A6 4 Web Server Application Server support Only Servlets and JSP distributed transaction and EJB super Can be subpart of Application server Include Web Server Logical difference Provide http protocol level service Provide web service and expose business level service Resource utilization Less so less heavy than application server High so more heavy than web server Example Apache tomcat Glassfish,JBoss,WAS
  • 6.
    5.What is differencebetween ServletContext and ServletConfig ServerConfig ServerContext Available for one per servlet Web application scope Like local parameter for particular servlet global parameter associated with whole application Where name value pair defined in web.xml inside the servlet section outside of servlet tag Method to get object getServletConfig() getServletContext() Example To get Shopping cart details To get the MIME type of a file or application session related information
  • 7.
    6.What is differencebetween GET and POST method in HTTP protocol Read more: http://java67.blogspot.com/2012/10/servlet-jsp-interview- questions-answer-faq-experience.html#ixzz49IJORF59 Read more: http://java67.blogspot.com/2012/10/servlet-jsp-interview- questions-answer-faq-experience.html#ixzz49IGWgAiy GET POST Passes request parameter In URL string In request body Limit to send data only pass limited amount of data depending on browser large amount of data to server i.e. no limit Can bookmarked or catched yes no Mostly used for view purpose update purpose Suitable for sensitive and confidential information No yes
  • 8.
    7.What is differencebetween URL Encoding and URL rewriting URL Encoding URL rewriting purpose Way to pass string to server containing special characters by converting special characters Technique used maintain user session if cookies are not enabled or not support Methods used java.net.URLEncoder.encode() and java.net.URLDecoder.decode() java.servlet.http.HttpServletResp onse.encodeURL(String url) and encodeRedirectURL(String URL) URL changes special character replaced by another character like space with ‘%20’ session id is appended to URL
  • 9.
    8.What is differencebetween Servlet and JSP Servlet JSP What it is Java program can be used to create dynamic web pages webpage scripting language that can generate dynamic content. Speed faster compared to JSP slower compared to Servlet as it takes compilation time to convert into Java Servlets. MVC part act as a controller act as a view Custom tag facility Not available Available e.g.tags used to call Java beans directly When to preferr more processing and manipulation involved not much processing of data required
  • 10.
    9.What is warfile and how to create? A war (web archive) file specifies the web elements. A servlet or jsp project can be converted into a war file. Moving one servlet project from one place to another will be fast as it is combined into a single file. The war file can be created using jar tool found in jdk/bin directory. If you are using Eclipse or Netbeans IDE, you can export your project as a war file. To create war file from console, you can write following code. jar -cvf abc.war *
  • 11.
    10.What is differencebetween GenericServlet and HttpServlet? The GenericServlet is protocol independent whereas HttpServlet is HTTP protocol specific. HttpServlet provides additional functionalities such as state management etc.
  • 12.