Posts

JSP and Servlet Interview Questions Part-1

1} What is JSP? Ans : Java Server Pages technology (JSP) is used to create dynamic web page. It is an extension to the servlet technology. A JSP page is internally converted into servlet. 2} What are the life-cycle methods for a jsp? Ans: public void jspInit() It is invoked only once, same as init method of servlet. public void _jspService(ServletRequest request,ServletResponse)throws ServletException,IOException It is invoked at each request, same as service() method of servlet. public void jspDestroy() It is invoked only once, same as destroy() method of servlet. 3} What are the JSP implicit objects ? Ans: JSP provides 9 implicit objects by default. They are as follows: Object (Its Type) 1) out (JspWriter) 2) request (HttpServletRequest) 3) response (HttpServletResponse) 4) config (ServletConfig) 5) session (HttpSession) 6) application (ServletContext) 7) pageContext (PageContext) 8) page (Object) 9) exception (Throwable) 4} What is servlet? A: A s...

Everything about Filters

The Java Servlet specification version 2.3 introduces a new component type, called a filter. A filter dynamically intercepts requests and responses to transform or use the information contained in the requests or responses. Filters typically do not themselves create responses, but instead provide universal functions that can be "attached" to any type of servlet or JSP page. Filters are important for a number of reasons. First, they provide the ability to encapsulate recurring tasks in reusable units. Organized developers are constantly on the lookout for ways to modularize their code. Modular code is more manageable and documentable, is easier to debug, and if done well, can be reused in another setting. Second, filters can be used to transform the response from a servlet or a JSP page. A common task for the web application is to format data sent back to the client. Increasingly the clients require formats (for exampl...

Servlet Mapping in Web.xml

<servlet>       <description></description>       <display-name>CheckPasswordValidity</display-name>       <servlet-name>CheckPasswordValidity</servlet-name>       <servlet-class>com.raj.util.CheckPasswordValidity</servlet-class> </servlet>   <servlet-mapping>       <servlet-name>CheckPasswordValidity</servlet-name>       <url-pattern>/CheckPasswordValidity</url-pattern> </servlet-mapping>

JSTL : Java Standard Tag Library

Use this taglib for to use basic tags of JSTL in jsp <%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %> <c:out> <c:out value=’${user}’ default=’guest’ />    or <c:out value=’${user}’>guest</c:out> <c:forEach> <c:forEach var=”movie” items=”${movieList}” varStatus=”movieLoopCount” >       ${movieLoopCount.count} :       ${movie} </c:forEach> varStatus attribute makes a new variable that holds an instance of  javax.servlet.jsp.jstl.core.LoopTagStatus LoopTagStatus class has a count property that gives you the current value of the iteration counter. (Like the “i” in a for loop.) <c:if> <c:if test=”${userType eq ‘member’ }” >          inside if  </c:if> <c:choose> and <c:when> and <c:otherwise> <c:choose>   <c:when test=”${userPref == ‘performance’}”>   ...

Include Header and Footer in all JSP files

<jsp-config> <jsp-property-group> <url-pattern>/*/*/*.jsp</url-pattern> <include-prelude>/view/commons/Header.jsp</include-prelude> <include-coda>/view/commons/Footer.jsp</include-coda> </jsp-property-group> </jsp-config>

Points to remember about include directive and include action

You can build a page with reusable components using one of two include mechanisms—the include directive or the <jsp:include> standard action. The include directive does the include at transla- tion time, only once. So the include directive is considered the appropriate mechanism for including content that isn’t likely to change after deployment. The include directive essentially copies everything from within the included file and pastes it into the page with the include. The Container combines all the included files and compiles just one file for the generated servlet. At runtime, the page with the include runs exactly as though you had typed all the source into one file yourself. The <jsp:include> standard action includes the response of the included page into the original page at runtime. So the include standard action is considered appropriate for including content that may be updated after deployment, while the include directive is not. Either...

Bean-related standard action points to remember

The <jsp:useBean> standard action defines a variable that holds a reference to either an existing bean attribute or, if the bean doesn’t already exist, a new bean. The <jsp:useBean> MUST have an “id” attribute which declares the variable name that’ll be used in this JSP to refer to the bean. If you don’t include a “scope” attribute with <jsp:useBean>, the scope defaults to page scope. The “class” attribute is optional, and it declares the class type that will be used if a new bean is created. The type must be public, non-abstract, and have a public no-arg constructor. If you put a “type” attribute in <jsp:useBean>, it must be a type to which the bean can be cast. If you have a “type” attribute but do NOT have a “class” attribute, the bean must already exist, since you haven’t specified the class type that should be instantiated for the new bean. The <jsp:useBean> tag can have a body, and anything in the body runs ONLY if a new bean is cr...