_ JSP(Java Server Pages) Interview Questions
1.What
is JSP- JavaServer
Pages?
JavaServer Pages. A server-side technology, JavaServer pages are an extension to the Java servlet technology that was developed by Sun. JSPs have dynamic scripting capability that works in tandem with HTML code, separating the page logic from the static elements -- the actual design and display of the page. Embedded in the HTML page, the Java source code and its extensions help make the HTML more functional, being used in dynamic database queries, for example. JSPs are not restricted to any specific platform or server.
Jsp contains both static and dynamic resources at run time.Jsp extends web server functionalities
2.What are advantages of JSP ?
whenever there is a change in the code, we dont have to recompile the jsp. it automatically does the compilation. by using custom tags and tag libraries the length of the java code is reduced.
3.What is the difference between include directive & jsp:include action ?
include directive (): if the file includes static text if the file is rarely changed (the JSP engine may not recompile the JSP if this type of included file is modified) .
if you have a common code snippet that you can reuse across multiple pages (e.g. headers and footers)
jsp:include : for content that changes at runtime .to select which content to render at runtime (because the page and src attributes can take runtime expressions) for files that change often JSP:includenull
4.What are Custom tags. Why do you need Custom tags. How do you create Custom tag ?
1) Custom tags are those which are user defined.
2) Inorder to separate the presentation logic in a separate class rather than keeping in jsp page we can use custom tags.
3) Step 1 : Build a class that implements the javax.servlet.jsp.tagext.Tag interface as follows. Compile it and place it under the web-inf/classes directory (in the appropriate package structure).
/* This Custom Tag will display Current Date, when you refresh that page every time. */
package examples;
import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class ShowDateTag implements Tag {
private PageContext pageContext;
private Tag parent;
public int doStartTag() throws JspException {
return SKIP_BODY; }
public int doEndTag() throws JspException {
try {
pageContext.getOut().write("" + new java.util.Date());
} catch (IOException ioe) {
throw new JspException(ioe.getMessage());
}
return EVAL_PAGE; }
public void release() {
}
public void setPageContext(PageContext page) {
this.pageContext = page;
}
public void setParent(Tag tag) {
this.parent = tag;
}
public Tag getParent() {
return this.parent;
}
}
Step 2:Now we need to describe the tag, so create a file called taglib.tld and place it under the web-inf directory."http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd"> 1.0 1.1
myTag http://www.mycompany.com/taglib My own tag library showDate examples.ShowDateTag Show the current date
Step 3 : Now we need to tell the web application where to find the custom tags, and how they will be referenced from JSP pages. Edit the web.xml file under the web-inf directory and insert the following XML fragement.http://www.mycompany.com/taglib /WEB-INF/taglib.tld
Step 4 : And finally, create a JSP page that uses the custom tag.Now restart the server and call up the JSP page! You should notice that every time the page is requested, the current date is displayed in the browser. Whilst this doesn't explain what all the various parts of the tag are for (e.g. the tag description, page context, etc) it should get you going. If you use the tutorial (above) and this example, you should be able to grasp what's going on! There are some methods in context object with the help of which u can get the server (or servlet container) information.
Apart from all this with the help of ServletContext u can implement ServletContextListener and then use the get-InitParametermethod to read context initialization parameters as the basis of data that will be made available to all servlets and JSP pages.
JavaServer Pages. A server-side technology, JavaServer pages are an extension to the Java servlet technology that was developed by Sun. JSPs have dynamic scripting capability that works in tandem with HTML code, separating the page logic from the static elements -- the actual design and display of the page. Embedded in the HTML page, the Java source code and its extensions help make the HTML more functional, being used in dynamic database queries, for example. JSPs are not restricted to any specific platform or server.
Jsp contains both static and dynamic resources at run time.Jsp extends web server functionalities
2.What are advantages of JSP ?
whenever there is a change in the code, we dont have to recompile the jsp. it automatically does the compilation. by using custom tags and tag libraries the length of the java code is reduced.
3.What is the difference between include directive & jsp:include action ?
include directive (): if the file includes static text if the file is rarely changed (the JSP engine may not recompile the JSP if this type of included file is modified) .
if you have a common code snippet that you can reuse across multiple pages (e.g. headers and footers)
jsp:include : for content that changes at runtime .to select which content to render at runtime (because the page and src attributes can take runtime expressions) for files that change often JSP:includenull
4.What are Custom tags. Why do you need Custom tags. How do you create Custom tag ?
1) Custom tags are those which are user defined.
2) Inorder to separate the presentation logic in a separate class rather than keeping in jsp page we can use custom tags.
3) Step 1 : Build a class that implements the javax.servlet.jsp.tagext.Tag interface as follows. Compile it and place it under the web-inf/classes directory (in the appropriate package structure).
/* This Custom Tag will display Current Date, when you refresh that page every time. */
package examples;
import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class ShowDateTag implements Tag {
private PageContext pageContext;
private Tag parent;
public int doStartTag() throws JspException {
return SKIP_BODY; }
public int doEndTag() throws JspException {
try {
pageContext.getOut().write("" + new java.util.Date());
} catch (IOException ioe) {
throw new JspException(ioe.getMessage());
}
return EVAL_PAGE; }
public void release() {
}
public void setPageContext(PageContext page) {
this.pageContext = page;
}
public void setParent(Tag tag) {
this.parent = tag;
}
public Tag getParent() {
return this.parent;
}
}
Step 2:Now we need to describe the tag, so create a file called taglib.tld and place it under the web-inf directory."http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd"> 1.0 1.1
myTag http://www.mycompany.com/taglib My own tag library showDate examples.ShowDateTag Show the current date
Step 3 : Now we need to tell the web application where to find the custom tags, and how they will be referenced from JSP pages. Edit the web.xml file under the web-inf directory and insert the following XML fragement.http://www.mycompany.com/taglib /WEB-INF/taglib.tld
Step 4 : And finally, create a JSP page that uses the custom tag.Now restart the server and call up the JSP page! You should notice that every time the page is requested, the current date is displayed in the browser. Whilst this doesn't explain what all the various parts of the tag are for (e.g. the tag description, page context, etc) it should get you going. If you use the tutorial (above) and this example, you should be able to grasp what's going on! There are some methods in context object with the help of which u can get the server (or servlet container) information.
Apart from all this with the help of ServletContext u can implement ServletContextListener and then use the get-InitParametermethod to read context initialization parameters as the basis of data that will be made available to all servlets and JSP pages.