Powered by Blogger.

Tuesday, 16 September 2014

JSP

By Unknown  |  07:28 No comments

1.      Why JSP?
·         JSP's provide a much cleaner way of separation of presentation from logic.
·         It speeds development with the reusable components and tags embedded in the page.And are simpler to write.
·         JSPs are interpreted only once, to Java byte-code, and reinterpreted only when the file is modified.
·         JSP specs is built on top of the Java Servlet API.
·         Servlets are supported by almost all major web servers, browsers and tools.
2.      What are JSPs?
·         A JSP page is a page created by the web developer that includes JSP specific tags, declarations and possibly scriplets, in combination with other static (HTML or XML) tags.
3.      How do JSP’s work?
·         JSP pages use Java scriplets and XML tags to encapsulate the logic that generates the content for the page.
·         The JSP pages are compiled into a Java servlet class and remains in server memory, so subsequent calls to the page have very fast response times.
·         JSP pages may call JavaBeans components or EJB’s components to perform the processing on the server.
·         A JSP page has a (.jsp) extension, this signals to the web server that the JSP engine will process elements on this page.
·         A JSP page is executed by a JSP engine or container, which is installed on the web server or an application server.
·         The page is initialized by invoking jspInit () methods just like the servlets.
·         Every time a request comes to the JSP, the container generated jspService () method is invoked, the request is processed, and the JSP generates an appropriate response. This response is taken by the container and passed back to the client.
·         When the JSP is destroyed by the server, the jspDestroy () is invoked to perform any clean up.

4.      What does a JSP Page contain?
·         Everything in a JSP can be broken down into two categories:-Element data that are processed on the server and Template data.
·         The Element data are classified in to following categories:-Directives, Declarations, Scriptlets, Expressions, and Standard Actions.
·         Directives:-They are used to set global values such as class declarations, methods to be implemented, output content type, etc.
·         They do not produce any output to the client.
·         All directives have the scope of the entire JSP file.
·         The syntax of a directive is as follows:-
<%@ directivename  attribute=”value” attribute=”value” %>
·         There are three types of directives:-The page, the include and the taglib directive.
·         PAGE directive defines a number of attributes that affect the whole page.
·         The syntax is as follows:-
<%@ page attributename=”value” %>
·         The attributes can be any of the following:-language, import, session, isThreadSafe, info, isErrorPage, etc…
·         E.g:- <%@ page language=”java” import=”java.rmi.*,java.util.*” session=”true” buffer=”12kb” autoflush=”true” isThreadSafe=”false” errorPage=”Error.jsp” isErrorPage=”false” %>
·         INCLUDE directive notifies the container to include the content of the resource in the current JSP.
·         The syntax is as follows:-
<%@ include file=”filename” %>
·         E.g :-<%@ include file=”/examples/myfile.html” %>
·         TAGLIB directive allows the page to use custom user defined tags.
·         It also names a tag library that they are defined in.The engine uses this tag to find out what to do when it comes across the custom tags in the JSP.
·         URI (Uniform resource identifier) identifies the tag library descriptor.A tag library descriptor is used to uniquely name the set of custom tags and tells the container what to do with the specified tags.
·         A tagprefix defines the prefix string in <prefix>:<tagname> that is used to define a custom tag.(the reserved tag prefixes are jsp, jspx, java, javax, servlet, sun and sunw).
·         The syntax is as follows:-
<%@ taglib uri=”tagLibraryURI” prefix=”tagPrefix” %>
·         E.g:-    <%@ taglib uri=”http://www.myserver.com/mytags” prefix=”hrishi” %>
<hrishi:processElement>
</hrishi:processElement>
·         Scripting Elements are used to include scripting code (normally java code) within JSP.
·         The three types are:-Declarations, Scriptlets, and Expressions.
·         A Declaration is a block of Java code in a JSP that is used to define class-wide variables and methods in the generated class file.
·         They are initialized when the JSP page is initialized and anything defined in a declaration is available throughout the JSP.
·         The syntax is as follows :-< %!          %>
·         E.g:-    <%! private int i=4;
Public void myMethod() {
}
%>
·         Scriptlets is a block of Java code that is executed at request processing time.
·         A scriptlet is enclosed between <% and %>
·         JSP gets compiled into a servlet and all the code between the <% and %> in the JSP, gets put in the service () of this servlet, hence it is processed for every request that the servlet processes.
·         An Expression is used to display the output value in the response stream back to the client.
·         An expression is enclosed within <%= and %>.
·          E.g:-  <%= “This is the output” +i  %>.
·         Actions are specific tags that affect the runtime behavior of the JSP and the response sent back to the client.
·         The standard action types are :-<jsp:useBean>, <jsp:setProperty>, <jsp:getProperty>, <jsp:param>, <jsp:include>, <jsp:forward>, <jsp:plugin>
·         <jsp:useBean> is used to associate a JavaBean with the JSP.
·         The syntax is as follows:-
<jsp:useBean id=”name” scope=”page|request|session|application” beandetails />
·         <jsp:setProperty> is used in conjunction with the useBean action and sets the value of simple and indexed properties in a bean.
·         The syntax is as follows:-
<jsp:setProperty name=”beanname” propertydetails />
·         <jsp:getProperty> action is used to access the properties of a bean.
·         The syntax is as follows:-
<jsp:getProperty name=”name” property=”propertyname” />
·         <jsp:param> action is used to provide other tags with additional information in the form of name/value pairs.This action is used in conjunction with the jsp:include, jsp:forward, jsp:plugin.
·         The syntax is as follows:-
<jsp:param name=”paramname” value=”paramvalue” />
·          <jsp:include> action allows a static or dynamic resource to be included in the current JSP at request time.
·         It can have one or more jsp:param tags in its body.
·         The syntax is as follows:-
<jsp:include page=”urlspec” flush=”true”>
<jsp:param name=”paramname” value=”paramvalue” />
</jsp:include>
·         <jsp:forward> action allows the request to be forwarded to another JSP, a servlet or a static resource.
·         The resource to which the request is being forwarded must be in the same context as the JSP dispatching the request.
·         The syntax is as follows:-
<jsp:forward page=”url” />
<jsp:param name=”paramname” value=”paramvalue”>
</jsp:forward>
·          <jsp:plugin> action is used to generate client browser specific HTML tags.
·         The syntax is as follows:-
<jsp:plugin type=”bean|applet” code=”objectname” codebase=”objectcodebase” align=”alignment” height=”height” hspace=”hspace” vspace=”vspace” width=”width” nspluginurl=”url” iepluginurl=”url” >
<jsp:param name=”paramname” value=”paramvalue” />
<jsp:fallback>Alternate text to display </jsp:fallback>
</jsp:plugin>

5.      What are CONTEXTS and various SCOPES?
·         JSP is designed to be portable between different servers.
·         The context provides an abstraction and bridging of the gap between server-specific implementations and portable JSP code.
·         A context provides an invisible container for resources and an interface for them to communicate with their enviroment.
·         For eg a servlet executes in a context.Everything that a servlet needs to know about its server can be extracted from its context and everything that the server wants to communicate to the servlet goes through the context.
·         Everything in a JSP is associated with a context and every context has a scope.
·         The PAGE_SCOPE:-the object reference is discarded upon completion of the current service ().
·         The REQUEST_SCOPE:-the object reference is available as long as the HttpRequest object exixts, even if the request is passed/chained to different pages.
·         The SESSION_SCOPE:-the object is distinct for every client amd is available as long as the client’s session is valid.
·         The APPLICATION_SCOPE:-This is the most persistent.This is not unique to individual clients and, consequently, all clients access the same object.

6.      What are the types of ERRORS and EXCEPTIONS?
·         There are two primary types of exceptions you can run into while processing with JSP pages.Translation time errors and Run time exceptions.
·         Translation errors occur during page compilation, this results in an Internal Servlet Error (error code 500).
·         Common translation time errors are caused by missing semicolons, forgetting to import a package, or using the wrong JSP directives.
·         Where as Exceptions occur once the page is compiled. Exceptions are dealt with within the JSP using the standard Java exception-handling methods.
·         The error page can be used to display a more user-friendly error message.(using the isErrorPage, errorPage attributes).

7.      What is need for tag extension mechanism?
·         Web application often want to provide multiple views on the same data to different channels(WML,HTML).It is important in these applications to separate the view from the controller so that the same controller can create different views of the same data.
·         You can customize u r own tags.

8.      What is a taglib tag?
·         Every tag u define should be declared in the directive with the extension (.tld).
·         The taglib directive references an XML document that defines the tags operation.
·         We first define a tag library using the <taglib> tag.Then for each custom tag we need a <tag> entry.
·         Inside the <tag> entry a name (which we use in the JSP).
·         A tagclass that maps the name of the fully-qualified Java class, which implements the tag interface.
·         A bodycontent that specifies the content type of the tag.
·         Optional information.
·         E.g:-
<?xml version=”1.0” encoding=”ISO-8859-1” ?>
<!DOCTYPE taglib
PUBLIC “-//sun microsystems………..” http://java.sun.com/j2ee/dtds/web-jsplibrary_1_1.dtd>
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>name</shortname>
<uri>http://www.wrox.com</uri>
<info>information</info>
<tag>
<name>name</name>
<tagclass>class name of tag</tagclass>
<teiclass>class name of tagExtraInfo</teiclass>
<bodycontent>empty|jsp|tagdependent</bodycontent>
<info>…</info>
</tag>
</taglib>
·         The basic interface that a custom tag must implement is the Tag interface.
·         The TagSupport class provides a default inplementation, so u only override the methods doStartTag() and doEndTag().
·         The name of the tag ,class name,optional TagExtraInfo implementation,body content type,attributes,and other desciptive information is stored in a <tag> entry in the taglib.
·         The TEI(TagExtraInfo) class provides a way to deal with the complexities of setting a variable for use within the tag or in the page.

9.      What is the life cycle of a “Tag with no body” ?
·         The doStartTag() method is evaluated once the tag object has been initialized with any attributes and page context information.
·         The default implementation in the TagSupport class is to return SKIP_BODY,so the body of the tag is not evaluated.
·         If u want to evaluate the body of the tag then return EVAL_BODY_INCLUDE from this method.
·         The doEndTag() method is called when the tag evaluation is done. Its default return is EVAL_PAGE which allows it to continue with rest of the page.
·         You can halt the JSPs output, because of an error then return SKIP_PAGE.
·         The void release () method is called at the end of every tag.

10.  What is the life cycle of a “Tag with a body”?
·         We use the BodyTag interface.
·         For body tags, the doStartTag () returns EVAL_BODY_TAG to evaluate the body,
·         Then the doInitBody() method is invoked.
·         The body between the tags is written to the bodycontent stream, then the doAfterBody() method is called which returns a default SKIP_BODY,so that doEndTag() is called.
·         Then atlast the void release() method is called.

11.      What are servlets?
·         Servlets are small, platform independent Java classes that can be loaded dynamically.
·         It is a web component managed by a container that generates dynamic content.
·         A servlet is a program written in the Java language that runs on the server. It is like a server-side applet.
·         Servlets are loaded and executed by a web server.

12.      What are servlet containers?
·         It is responsible for the client and server interaction using the request and response paradigm.
·         It decodes MIME based requests and formats MIME based responses.

13.      What are the advantages of servlet?
·         They are cross-platform i.e “Write once, run anywhere”.
·         They are cross-server i.e can be run on any server.
·         They are multithreaded i.e multiple client requests can be handled by separate threads within a single process.
·         They can maintain services such as (database connections) between requests.
·         They are secure and are fast.
·         They use a standard API supported by many web servers and also can access the Java API’s.

14.      What can servlets do?
·         Process user input passed by an HTML form and return a request.
·         Provide a user authentication and security.
·         Forward requests from one server to another.

15.      What are GET and POST methods?
·         GET:-
Ø  It is the default HTTP method used to request a resource from the server.
Ø  It is normally used to request static data.
Ø  The main two disadvantages of this method are that : A limited amount of data can be passed as part of the URL & the informaton  passed in the URL is visible.
·         POST :-
Ø  It is normally used to pass the user input to the server.
Ø  It is usually used to request dynamic data.
Ø  Its parameter information is stored in the body of the request rather than the URL.
Ø  The advantage of this method is that : There is no limit to the amount of information passed.
Ø  And as it is stored in the body of the request, the user info is not visible.

16.      What is the life cycle of a servlet?
·         The servlet life cycle is made up of three methods:- init(),service() & destroy() methods.
·         The servlet is loaded only once.
·         The init() is called a single time when the servlet is first loaded.
·         The service() is automatically called by the server in response to the client request.
·         Depending upon the type of HTTP request recevied the methods doGet() and doPost() are used.
·         The service() method processes the client request by evaluating the ServletRequest or HttpServletRequest object and responds using ServletResponse or HttpServletResponse object.

17.      What is thread safety?
·         The servlet writer must ensure that no thread can read a variable while another thread is in the process of changing it.
·         This is done by Synchronization :- This in Java is a process of locking a variable so that it can be read and modified by only a single thread at a time.
·         The SingleThreadModel interface can be used to ensure that no more than one thread can execute the service() at a time for a particular servlet instance.

18.      What is a Stateless protocol?
·         A protocol is said to be stateless if it does not preserve information of prior connections and cannot distinguish one client request from another.
·         The advantage of the stateless nature is that it keeps the protocol simple and also consumes less resources of the server.
·         The disadvantage is the increased overhead requried to create a new connection for each request.
·         And also state is extremely useful for secure sites that require a user to log in.

19.      What is the difference between state & session management?
·         In state management only the client’s state is maintained. It cannot uniquely identify each client.
·         In session management both state and identity is maintained.

20.  What is a Session ?
·         A session is a persistent network connection between two hosts (usually a server and a client) that facilitates the exchange of information.
·         The HttpSession involves a virtual connection between a client and the server.
·         A virtual connection associates each request received by the server with the client that issued it.
·         This is accomplished by requiring the client to return a piece of state information with each request.
·         The server uses this piece of information (usually a sessionID or userID) to uniquely identify the client and associate the current request  with the previous request.
·         These virtual connections are commonly known as sessions.
·         Sessions are used to maintain state and client identity across multiple requests.
·         These sessions automatically expire after a period of inactivity.

21.  What is HttpSessions life cycle?
·         The client requests a resource from the server.
·         The server returns an authentication validation.
·         The client returns a valid password and name.
·         The server returns a valid sessonID that uniquely identifies the client.(Sent in a cookie or some other method).
·         This cookie is returned by the client with each request to the server.
·         This BEGINS the HttpSession.
·         The client issues many requests to the server which is able to identify the client based on the sessionID passed in the cookie with each request.
·         The client closes the browser without explicitly logging out.
·         After the session time out period which is set ,the server expires the session by deleting the sessionID from the database table.
·         This CONCLUDES the HttpSession.


22.  What are the different session tracking methods?
·         The different sesson tracking methods are as follows: Rewriting URLs, Hidden variables, Cookies.
URL Rewritting:- The sessionId is embedded in the URL path for managing the state of the clients request.
·         Each time the client follows a relative URL hyperlink within the site, the browser will return the full path info (including the sessionID) to the server.
·         The server can use this session information (from sessionID) to identify the client.
         Advantages:-
·         For storing the sessionID in the URL path no special browser feature is required.
·         This will work even if cookies are disabled.

Disadvantages:-

·         You have to only use a relative path throughout the website.
·         The sessionID is visible in the URL path causing security concern.
·         Storing the sessionID in the URL can frustrate the search engine.
Hidden Variables:- Hidden HTML form variables are commonly used to store state information.
·         When a hidden variable is submitted , the client transmitts the fields name/value pair to the server.
·         Session management is implemented by storing the unique sessionID within a hidden variable on every page.

Disadvantages:-

·         They only work when the client submits an HTML form.
·         As hidden variables have to be passed on every page the code becomes cumbersum and the amount of processing is increased.

Cookies :-

·         It is a simple mechanism used to store and retrieve user-specific information on the web.
·         In addition to the requested resource, the server may choose to return some state information that is stored by a cookie-enabled client.
·         A cookie can be transmitted to the  client by adding the set-cookie header to the Http response using HttpServletResponse objects setHeader().
·         The addCookie() method of the HttpServletResponse object allows one or more cookie objects to be added to the response.
·         Cookie information can be extracted from the client request using a servlet API method (getCookie() of HttpServletRequest).

Advantages:-

·         Cookies provide a simple solution to state management and security.
·          Either relative or absolute URLs may be used without losing state.
Disadvantages:-
·         The user can disable the option of cookies, in which case the cookies won’t work.
·         Not all browsers support cookies.

23.  What is a Redirect ?
·         An Http redirect is a set of instructions included in the header of an Http response that instructs the browser to issue a new request to a new URL.
·         Also the HttpServletResponse method sendRedirect(String Location) can be used before any data is sent to the output stream.

24.  What is RequestDispatcher ?
·         Included in the servlet API 2.1, the RequestDispatcher object allows you to forward requests to or include output from other server resources.
·         The requestDispatcher object essentially serves as a “Wrapper” around the server resource in order to provide forwarding and including functionality.
·         It has only two methods:- forward() and include().
·         The forward() method allows you to forward a request to another servlet(or other server resources).
·         The include() allows you to include the content generated by another servlet( or other server resources).

25.  What is servlet chaining ?
·         It is the process of passing the output of one servlet to the input of another.
·         The client request is sent to the first servlet in the chain.
·         Once the request has been processed by the servlet , it passes its out put to the next servlet in the chain.
·         This continues until the last servlets output is returned to the client.
·         The servlet chain is triggered by :Servlet Aliasing, Mime types, Http request.

26.  What are server-side includes ?
·         The SSI’s allow you to embbed servlet calls within the HTML document using a special <servlet> tag.
·         They should have the file extension as (.shtml).
·         When the <servlet> tag is encountered the SSI loads the servlet specified by the <servlet> tag and includes the servlet output in the HTML page returned to the client.
·         SYNTAX:-                                                                                                         
<servlet           name = SERVLET_NAME
                                    code = CLASS_NAME
                                    codebase = CLASS_LOCATION_URL
                                                INIT_PARAM1 = VALUE1
                                        …………>
                  <param name = SERVLET_PARAM1 value =VALUE1>
                  ……………………   
               </servlet>                 









           


Author: Unknown

Hello, I am Author, decode to know more: In commodo magna nisl, ac porta turpis blandit quis. Lorem ipsum dolor sit amet, consectetur adipiscing elit. In commodo magna nisl, ac porta turpis blandit quis. Lorem ipsum dolor sit amet.

0 comments:

Recent Articles

© 2014 Learning Java. WP themonic converted by Bloggertheme9. Published By Gooyaabi Templates
TOP