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
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.
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
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).
package examples;
import java.io.*; //// THIS PROGRAM IS EVERY TIME I MEAN WHEN U REFRESH THAT PARTICULAR CURRENT DATE THIS CUSTOM TAG WILL DISPLAY
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.
What are the implicit objects in JSP & differences between them
There are nine implicit objects in JSP.
1. request : The request object represents httprequest that are trigged by service( ) invocation. javax.servlet
2. response:The response object represents the servers response to request.
javax.servlet
3. pageContext : The page context specifies the single entry point to many of the page attributes and is the convient place to put shared data.
javax.servlet.jsp.pagecontext
4. session : the session object represents the session created by the current user.
javax.Servlet.http.HttpSession
5. application : the application object represents servlet context , obtained from servlet configaration . javax.Servlet.ServletContext
6. out : the out object represents to write the out put stream .
javax.Servlet.jsp.jspWriter
7. Config :the config object represents the servlet config interface from this page,and has scope attribute. javax.Servlet.ServletConfig
8. page : The object is th eInstance of page implementation servlet class that are processing the current request.
java.lang.Object
9. exception : These are used for different purposes and actually u no need to create these objects in JSP. JSP container will create these objects automatically.
java.lang.Throwable
You can directly use these objects.
Example:
If i want to put my username in the session in JSP.
JSP Page: In the about page, i am using session object. But this session object is not declared in JSP file, because, this is implicit object and it will be created by the jsp container.
If u see the java file for this jsp page in the work folder of apache tomcat, u will find these objects are created.
What is jsp:usebean. What are the scope attributes & difference between these attributes
page, request, session, application
What is difference between scriptlet and expression
With expressions in JSP, the results of evaluating the expression are converted to a string and directly included within the output page. Typically expressions are used to display simple values of variables or return values by invoking a bean's getter methods. JSP expressions begin within tags and do not include semicolons:
But scriptlet can contain any number of language statements, variable or method declarations, or expressions that are valid in the page scripting language. Within scriptlet tags, you can declare variables or methods to use later in the file, write expressions valid in the page scripting language,use any of the JSP mplicit objects or any object declared with a
What is Declaration
Declaration is used in JSP to declare methods and variables.To add a declaration, you must use the sequences to enclose your declarations.
How do you connect to the database from JSP
To be precise to connect jdbc from jsp is not good idea ofcourse if ur working on dummy projects connecting to msaccess u can very well use the same connection objects amd methods in ur scriplets and define ur connection object in init() method.
But if its real time u can use DAO design patterns which is widely used. for ex u write all ur connection object and and sql quires in a defiened method later use transfer object [TO ]which is all ur fields have get/set methods and call it in business object[BO] so DAO is accessd with precaution as it is the crucial. Finally
No comments:
Post a Comment