servlet and jsp interview questions

12
Servlet and JSP Interview questions By Sujata Regoti

Upload: sujata-regoti

Post on 13-Apr-2017

169 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Servlet and jsp interview questions

Servlet and JSPInterview questions

By Sujata Regoti

Page 2: Servlet and jsp interview questions

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>

Page 3: Servlet and jsp interview questions

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

Page 4: Servlet and jsp interview questions

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

Page 5: Servlet and jsp interview questions

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#ixzz49IBh21A64

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

Page 6: Servlet and jsp interview questions

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

Page 7: Servlet and jsp interview questions

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

Page 8: Servlet and jsp interview questions

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.HttpServletResponse.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

Page 9: Servlet and jsp interview questions

8.What is difference between Servlet and JSPServlet 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

Page 10: Servlet and jsp interview questions

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 *

Page 11: Servlet and jsp interview questions

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.

Page 12: Servlet and jsp interview questions

Thank Youhttp://www.regotiss.github.io