Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Name: Deepu Gupta

Sap Id: 45207190059


Roll No.: A015

ENTERPRISE JAVA ASSIGNMENT NUMBER 3

1. What is the difference between a traditional JSP page and a JSP


document?
Ans)A JSP document is a JSP page written in XML syntax as opposed
to the standard syntax JSP Page. Because it is written in XML syntax, a
JSP document is also an XML document and therefore gives you all the
benefits offered by the XML standard:
You can author a JSP document using one of the many XML-aware
tools on the market, enabling you to ensure that your JSP document is
well-formed XML.
You can validate the JSP document against a document type definition
(DTD).
You can nest and scope namespaces within a JSP document.
You can use a JSP document for data interchange between web
applications and as part of a compile-time XML pipeline.
In addition to these benefits, the XML syntax gives the JSP page author
less complexity and more flexibility. For example, a page author can use
any XML document as a JSP document. Also, elements in XML syntax
can be used in JSP pages written in standard syntax, allowing a gradual
transition from JSP pages to JSP documents.
E.g
Jsp page : <%=expression %>
Jsp document: <jsp:expression></jsp:expression>

In basic difference between two is :


1: A JSP document is a JSP page written in XML syntax as opposed to
the standard syntax.
2: As JSP document is written in XML syntax, a JSP document is also
an XML document and therefore it can take all the benefits offered by
the XML standard.
3: Jsp pages are saved having file extension of .jsp , while JSP
documents having .jspx extension.
4: The main difference is Standard Syntax(that JSP page use) Versus
XML Syntax(that a JSP Document use).

2. Explain any five implicit objects in Java Server Pages.


Ans)The five types of implicit objects in Java Server Pages are:
1. out.
2. request.
3. response.
4. config.
5. session.

Out : The Out object is a reference to an output stream that can be used within
scriptlets. In actual fact, the Out object represents the output stream of the JSP
page, the contents of which are being sent to a client browser.
The out object is the PrintWriter, which is used to send output to the client.
The out object has page scope and is and instance of javax.servlet.jsp.JspWriter
For Eg : out.print(“welcome to jsp”);

Request: The implicit Request object represents the HTTP request


The request object has a request scope. It encapsulates the request coming from the
client and gets processed by the JSP.
It is passed to the JSP by the JSP engine, as a parameter to the _jspService()
method.
It is where a client submits a request to a server, it can send information along with
the request in the form of request parameters.
For Eg: <h2>Welcome<%=request.getParameter(“username”)%>!</h2>
The above code extracts a request parameter called username from the implicit
request object and constructs an appropriate greeting.

Response : The implicit Response object represents the response sent back to the
user browser as a result of processing the JSP page.
The response object has page scope. It encapsulates the response generated by the
JSP. This is the response being sent back to a client browser in response to its
request. The response is generated by the Web server and passed to the JSP as a
parameter to _jspService(). Here the response generated can be modified by the
JSP.
The response object is an instance of javax.servlet.http.HttpServletResponse.

Config : The Config object has page scope and is an instance of


javax.servlet.ServletConfig.
A web server uses a ServletConfig instance to pass information to a Servlet or JSP
page during initialization.
Session : The JSP components in a Web application automatically participate in a
given client’s session, without needing to do anything special.
Sessions are created automatically. Hence, this variable is present even if there are
no incoming session references.
By default, during a given time period, the session can persists over multiple
connections and/or request. Sessions are used to maintain state and user identity
across page requests.
The session object has session scope.
It represents the session created by requesting client and is only valid for HTTP
requests.
The session object is an instance of javax.servlet.http.HTTPSession, which
provides access to session data as well as information about the session such as
when it was created and when a request for the session was last received.

3. Differentiate between JSP and Servlets.


Ans)
JSP SERVLET
JSP is slower than Servlets, as the first Servlets are faster as compared to JSP,
step in the JSP lifecycle is the as they have a short response time.
conversion of JSP to Java code and
then the compilation of the code.
JSP are HTML-based codes. Servlets are Java-based codes.
JSPs are easier to code, as here Java is Servlets are harder to code, as here,
coded in HTML. the HTML codes are written in Java.
In MVC architectures, the JSPs act as In an MVC architecture, Servlets act
a view to present the output to the as the controllers.
users.
The service() function cannot be The service() function can be
overridden in JSPs. overridden in Servlets.
JSPs provide session management by Servlets require the users to enable the
default. default sessions management
explicitly, as Servlets do not provide
default session management.

4. Explain Life Cycle of a JSP Page.


Ans)This is the JSP life cycle. This translation phase deals with the Syntactic
correctness of JSP. Here For eg: test.jsp file is translated to test.java.

1. Compilation of JSP page :- Here the generated java servlet file (test.java) is
compiled to a class file (test.class).
2. Instantiation :- Here an instance of the class is generated. The container
manages one or more instances by providing responses to requests.
3. Initialization :- jspInit() method is called only once during the life cycle
immediately after the generation of Servlet instance from JSP.
4. Request processing :- jspService() method is used to serve the raised
requests by JSP. It takes request and response objects as parameters. This method
cannot be overridden.
5. Destroy :- In order to remove the JSP from the use by the container or to
destroy the method for servlets, jspDestroy()method is used. This method is called
once, if you need to perform any cleanup task like closing open files, releasing
database connections, jspDestroy() can be overridden.
5. Explain JSP Elements in details.
Ans)The Elements are classified as follows:-
1. JSP Directives.
2. JSP Scripting Elements.
3. JSP Actions.

JSP Directives : In the web applications, JSP Directives can be used to define
present JSP page characteristics, to include the target resource content into the
present JSP page, and to make available user-defined tag library into the present
JSP page. All the JSP directives are going to be resolved at the time of translating
the JSP page to the servlet. The majority of JSP Directives will not give a direct
effect to response generation.

JSP Actions : In JSP applications, Scripting Elements are often wont to allow java
code inside JSP pages but the most theme of JSP technology isn’t to permit java
code inside the JSP pages. In the above context, to preserve the theme of JSP
technology we’ve to eliminate scripting elements from JSP pages, for this we’ve to
supply an alternate i.e. JSP Actions provided by JSP Technology. In the case of
JSP Actions, we’ll define the scripting tag in situ of java code, in JSP pages, and
that we will provide the respective java code inside the classes folder. In this
context, when the JSP container encounters the scripting tag then the container will
execute the respective java code and perform a specific action called JSP Action.

JSP Scripting Elements : In web applications, JSP Scripting Elements are often
wont to provide code in JSP pages. All the JSP Scripting Elements are going to be
resolved at the time of request processing. The majority of Scripting Elements will
give a direct effect on response generation. JSP scripting elements enable you to
insert java code directly into the servlet that will be generated from the current JSP
page. There are three types of Scripting Elements:

a. Declarations: Declaration of the form that is inserted into the body of the
servlet class, outside of any existing methods.
b. Scriptlets: Scriptlets of the form that are inserted into the servlets service
method.
c. Expressions: Expressions of the form that are evaluated and inserted into
out.

6. Write a short note on Character Quoting Conventions.


Ans)In JSP certain character sequences are used to represent start and stop tags,
the developer sometimes needs to escape a character so the JSP engine does not
interpret it as part of a special character sequence.
In a scripting element, if the character ‘%>’ needs to be used, escape the greater
than sign with a backslash. <% String message = “This is the %/> message” ; %>
The backslash before the expression acts as an escape character, informing the JSP
engine to deliver the expression verbatim instead of evaluating it.
Character Quoting and Data Conventions:
<\% : It is used in template text (static HTML) where you really want “<%”.

%\> : It is used in scripting elements where you really want “%>”.

\’ : A single quote in an attribute that uses single quotes. Remember, however,


that you can use either single or double quotes, and the other type of quote will
then be a regular character.

\” : A double quote in an attribute that uses double quotes. Remember, however,


that you can use either single or double quotes, and the other type of quote will
then be a regular character.

\ : It is used as a delimiter.

7. What is JSTL? State disadvantages of JSTL.

Ans)JSTL stands for Java Server Pages Standard Tag Library


JSTL mainly provides five types of tags:
Core tags
Function tags
Formatting tags
XML tags
SQL tags
Disadvantages Of JSTL:
JSTL puts the processing burden to the server. Java Scriptlet and the tag libraries
both are compiled into a servlet, which is then executed by the servlet engine. Java
code embedded in scriptlets is pretty much just copy pasted into the servlet.
But on the other hand, JSTL tags causes much more code to be added to the
servlet. In some cases this is not measurable, but it should be considered.
Scriptlets are more powerful than JSTL tags. JSTL provides a powerful set of
reusable libraries to application developers. However, JSTL cannot do everything
that the Java code can do.

You might also like