0% found this document useful (0 votes)
13 views

Java Assignment

Some Questions and Answers on Advanced Java Programming

Uploaded by

TEJASWINI P
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Java Assignment

Some Questions and Answers on Advanced Java Programming

Uploaded by

TEJASWINI P
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

1. What is meant by JSP? What is the need of JSP?

 Java Server Pages (JSP) is a technology for developing Webpages that supports
dynamic content.
 This helps developers insert java code in HTML pages by making use of special JSP
tags, most of which start with <% and end with %>.
 A Java Server Pages component is a type of Java servlet that is designed to fulfill the
role of a user interface for a Java web application.
 Web developers write JSPs as text files that combine HTML or XHTML code, XML
elements, and embedded JSP actions and commands.
 Using JSP, you can collect input from users through Webpage forms, present records
from a database or another source, and create Webpages dynamically.
 JSP tags can be used for a variety of purposes, such as retrieving information from a
database or registering user preferences, accessing JavaBeans components, passing
control between pages, and sharing information between requests, pages etc.
 NEED:
 The need for JSP arises because static HTML pages have limitations in terms of
dynamic content, database connectivity, and web application functionality.
 It also allows programmers
 With JSP, developers can embed Java code inside HTML pages to create dynamic
content that can be customized for each user, such as personalized messages,
dynamically generated tables, and graphs. JSP pages can also connect to databases
and other backend systems, allowing for the retrieval and manipulation of data, and
the creation of more complex web applications.

2. What is JPS? Discuss its advantages.

 JSP (JavaServer Pages) is a technology used for creating dynamic web pages with Java.
It allows developers to create HTML pages with embedded Java code that can
dynamically generate content, interact with databases, and perform other server-side
tasks.
 JSP pages are compiled into Java servlets by the web server when they are first
accessed, and then executed by the servlet container. This allows for the creation of
dynamic content that can be customized for each user, such as personalized
messages, dynamically generated tables, and graphs.
 ADVANTAGES:
 Platform independence: JSP can be run on any platform that supports Java,
which makes it easy to develop and deploy web applications across different
operating systems.
 Easy generation with java code: JSP is based on Java, so it can be easily
integrated with other Java-based technologies and frameworks, such as
Servlets, Spring, and Hibernate.
 Improved performance: JSP pages are compiled into servlets, which can be
cached by the web container for faster processing. This can result in improved
performance compared to traditional CGI-based approaches.
 Separation of concerns: JSP allows for the separation of business logic and
presentation logic, which can make it easier to manage and maintain large web
applications.
 Reusability: JSP components can be reused across multiple pages, which can
help reduce development time and improve consistency across the application.
 Simplified coding: JSP provides an easy-to-use tag-based syntax for
incorporating dynamic content into web pages, which can simplify coding and
reduce errors.
 Support for custom tags: JSP provides support for custom tags, which can be
used to encapsulate complex functionality and provide a higher level of
abstraction.

3. What is JSP? How is it different from other technologies or PHP?


 JSP (JavaServer Pages) is a technology used for creating dynamic web pages with Java.
It allows developers to create HTML pages with embedded Java code that can
dynamically generate content, interact with databases, and perform other server-side
tasks.
 JSP pages are compiled into Java servlets by the web server when they are first
accessed, and then executed by the servlet container. This allows for the creation of
dynamic content that can be customized for each user, such as personalized
messages, dynamically generated tables, and graphs.
 Here are some ways in which JSP is different from other web development
technologies:
 HTML: HTML is a static markup language that is used for creating web pages.
JSP, on the other hand, allows developers to embed Java code in HTML pages,
making them dynamic and interactive
 Servlets: Servlets are Java classes that are used for server-side programming.
While JSP and servlets both use Java, servlets are focused on generating
dynamic content and handling requests, while JSP is focused on creating the
presentation layer of a web application
 JavaScript: JavaScript is a client-side scripting language used for creating
interactive web pages. While JSP can also be used to create interactive pages, it
does so using server-side Java code rather than client-side scripting
 How is JSP different from PHP:
SNEHA WILL SEND.
4. Discuss the working principle of JSP.
 JSP pages are written using HTML markup and embedded Java code. The Java code is
enclosed within special tags, such as "<%" and "%>", which tell the JSP engine when
to execute the code
 When a user requests a JSP page, the web server recognizes the file extension and
sends the page to the JSP engine
 The JSP engine compiles the page into a Java servlet, which can handle the request
and generate dynamic content. The servlet is cached in memory, so subsequent
requests for the same JSP page are handled more quickly.
 When the servlet is executed, the JSP engine first processes any static HTML content
and sends it to the user's browser. It then executes any embedded Java code, which
can generate dynamic content, access databases, and perform other server-side tasks
 The JSP engine can also make use of JavaBeans and custom tag libraries to simplify
the development process and make the code more modular and reusable
 Once the servlet has finished executing, the JSP engine sends the resulting HTML
content to the user's browser
Overall, the working principle of JSP is based on the idea of combining HTML markup
with embedded Java code to create dynamic, database-driven web applications. By
compiling JSP pages into Java servlets, the technology can provide high performance and
scalability, making it a popular choice for web developers.

5. What are scriptlets? Explain with an example.

 Scriptlets in JSP (JavaServer Pages) are code snippets written in Java that can be
embedded within an HTML page to create dynamic content. They are enclosed within
<% %> tags and can contain any valid Java code, including variable declarations, loops,
conditional statements, and method calls.
 Scriptlets provide a way to execute Java code on the server-side and generate
dynamic content based on user input or other data. For example, a scriptlet can be
used to retrieve data from a database, manipulate it, and display the result on the
web page.
 Scriptlets are a powerful feature of JSP that allow developers to write Java code
directly within HTML pages, making it easy to create dynamic web applications using
Java technology.
 However, they can also lead to complex and hard-to-maintain code, which is why
modern JSP development practices recommend using JSTL (JSP Standard Tag Library)
or custom tags instead of scriptlets.
 EXAMPLE: using a scriptlet in a JSP page to retrieve the value of a form parameter
and display it on the web page:
<html>
<head>
<title>Example of Scriptlets in JSP</title>
</head>
<body>
<h1>Welcome to My Website!</h1>
<p>
<%
// Retrieve the value of the "name" parameter from the request
String name = request.getParameter("name");

// Output a personalized greeting using the out.println() method


out.println("Hello " + name + "! Welcome to my website.");
%>
</p>
<form action="greeting.jsp" method="post">
<p>Please enter your name: <input type="text" name="name"></p>
<p><input type="submit" value="Submit"></p>
</form>
</body>
</html>

 In this example, the scriptlet code is enclosed within <% and %> tags, and includes
the following steps:
 Retrieve the value of the "name" parameter from the HTTP request using the
request.getParameter() method.
 Use the out.println() method to output a personalized greeting to the web
page, including the value of the "name" parameter.
 Include an HTML form that allows the user to enter their name and submit the
form to the server.

 When the user submits the form, the server receives the form data and generates a
new HTTP request to the "greeting.jsp" page, passing the value of the "name"
parameter. The scriptlet code in "greeting.jsp" retrieves the value of the "name"
parameter and uses it to output a personalized greeting on the web page.
6. Write a note on JSP Expressions. Illustrate with a example.

 JSP Expression Tags are a powerful feature in JSP that allow developers to create
custom tags that can evaluate and output dynamic content using expressions.
 JSP Expression Tags are used to encapsulate complex business logic and data
processing tasks into reusable components that can be easily integrated into JSP
pages.
 JSP Expression Tags are defined using a Tag Library Descriptor (TLD) file, which
specifies the attributes, behavior, and usage of the tag.
 JSP Expression Tags can take any number of attributes, which can be passed using the
JSP Expression Language (EL) syntax. The tag's behavior is defined by a Java class that
implements the Tag interface, and can use any Java expression or function to evaluate
the output.
 EXAMPLE:
<html>
<head>
<title>Example of JSP Expression</title>
</head>
<body>
<h1>Welcome to My Website!</h1>
<p>
Today's date is: <%= new java.util.Date() %>
</p>
<p>
The value of 3 + 4 is: <%= 3 + 4 %>
</p>
<p>
Your name is: <%= request.getParameter("name") %>
</p>
</body>
</html>
 In this example, we are using JSP Expressions to dynamically output three different
values to the web page:
 The current date and time, generated by a Java util.Date object using <%= new
java.util.Date() %>
 The sum of two integers, calculated using <%= 3 + 4 %>
 The value of a request parameter named "name", retrieved using <%=
request.getParameter("name") %>
7. What are JSP directives? Give an example program.

 JSP directives are the messages to JSP container. They provide global information
about an entire JSP page.
 JSP directives are used to give special instruction to a container for translation of JSP
to servlet code.
 In JSP life cycle phase, JSP has to be converted to a servlet which is the translation
phase.
 They give instructions to the container on how to handle certain aspects of JSP
processing
 Directives can have many attributes by comma separated as key-value pairs.
 SYNTAX: < %@ directive attribute="" %>
 TYPES:
 Page directive
 Include directive
 Taglib directive

1) Page directive:
 It provides attributes that get applied to entire JSP page
 It defines page dependent attributes, such as scripting language, error page,
and buffering requirements
 It is used to provide instructions to a container that pertains to current JSP
page.
EXAMPLE:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*, java.text.*" %>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>My JSP Page</title>
</head>
<body>
<h1>Welcome to my JSP page!</h1>

<%
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM d,
''yy");
String formattedDate = sdf.format(now);
%>

<p>Today's date is <%= formattedDate %></p>


</body>
</html>

 In this example, the JSP page directive is used to specify the page language,
content type, and import statements. The language attribute is set to java,
indicating that the JSP page will be written in Java. The contentType
attribute specifies that the content type of the page will be text/html, with a
character encoding of UTF-8. The pageEncoding attribute specifies that the
JSP page itself is encoded in UTF-8. The import attribute is used to specify
the Java packages and classes that will be used in the JSP page.

2) Include directive:
 JSP “include directive”( codeline 8 ) is used to include one file to the another file.
 This included file can be HTML, JSP, text files, etc
 It is also useful in creating templates with the user views and break the pages
into header&footer and sidebar actions.
 It includes file during translation phase
EXAMPLE:
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>My JSP Page</title>
</head>
<body>
<%@ include file="header.jsp" %>
<p>This is the content of my JSP page.</p>
<%@ include file="footer.jsp" %>
</body>
</html>

header.jsp
<header>
<h1>Welcome to my website</h1>
<nav>
<a href="/">Home</a>
<a href="/about">About</a>
<a href="/contact">Contact</a>
</nav>
</header>
footer.jsp
<footer>
<p>Copyright © 2022 My Website</p>
</footer>
 In this example, we are using the include directive to include the contents of two
other JSP files (header.jsp and footer.jsp) into our main JSP page. The file attribute
specifies the location of the file to be included, which can be either an absolute or a
relative path. By including these files in our JSP page using the include directive, we
can reuse the header and footer code across multiple pages, making our code more
modular and easier to maintain.

3) Taglib directive:
 JSP taglib directive is used to define the tag library with “taglib” as the prefix,
which we can use in JSP.
 JSP taglib directive is used in the JSP pages using the JSP standard tag libraries
 It uses a set of custom tags, identifies the location of the library and provides
means of identifying custom tags in JSP page.
EXAMPLE:

<%@ taglib prefix="mytaglib" uri="/WEB-INF/mytaglib.tld" %>


<html>
<head>
<title>My JSP Page</title>
</head>
<body>
<h1>Welcome to my JSP page</h1>
<mytaglib:mytag attribute1="value1" attribute2="value2" />
<p>This is a sample JSP page that demonstrates the use of taglib directives.</p>
</body>
</html>

 In this example, we are using the taglib directive to include a custom tag library in our
JSP file. The prefix attribute specifies a short name that we can use to refer to the tag
library throughout the JSP file. The uri attribute specifies the location of the tag
library descriptor file (mytaglib.tld) relative to the application root. After including the
tag library, we can use its custom tags in our JSP file by prefixing them with the prefix
we specified in the taglib directive. In this case, we are using a custom tag named
mytag from the mytaglib tag library.
8. Differentiate between mobile application and web application.
FEATURES MOBILE APPLICATION WEB APPLICATION
Platform Mobile applications are On the other hand, web
designed specifically for applications are designed to
mobile devices such as be accessed through a web
smartphones and tablets. browser and can run on any
They are optimized for the device with an internet
specific hardware and connection.
software features of the
mobile device they run on,
such as touch screens,
camera, GPS, and push
notifications.
User Experience Mobile applications usually Web applications are limited
provide a richer user to the capabilities of the
experience than web web browser they run on
applications because they and often require an
can take advantage of the internet connection to work.
device's hardware and
software features.
Development Process Developing a mobile In contrast, web applications
application requires can be developed using
specialized skills, tools, and standard web development
programming languages technologies such as HTML,
such as Java or Swift. It also CSS, and JavaScript, and can
involves developing run on any device with a
separate versions of the app web browser.
for different mobile
operating systems, such as
iOS and Android.
Installation Mobile applications need to web applications do not
be downloaded and require installation and can
installed from an app store be accessed through a web
or website browser.
Accessibility Mobile applications, on the Web applications are more
other hand, are limited to accessible than mobile
the specific mobile devices applications because they
they were designed for. can be accessed from any
device with a web browser
and an internet connection.

9. Describe JSP action tags. Illustrate with an example.


 JSP action tags are a set of predefined tags in JSP that allow developers to perform
certain actions or implement certain functionalities within a JSP page.
 These tags are processed by the JSP container at runtime and translated into Java
code.
 There are several types of JSP action tags, including:

JSP Action Description Attribute Description of Attributes

Specifies the URL of the


Used to forward a request
to a target page page target page
<jsp:forward>

Specifies the URL of the

resource to be included.

Specifies whether the

Includes a file in buffer should be flushed or

the current JSP page not. The flush value can be

page flush either true or false


<jsp:include>

Uniquely identifies the instance

of the bean.

Identifies the class from which

the bean objects are to be

Invokes and id implemented.

searches for class Defines the scope of the bean.

an existing scope Defines the referential name for

bean. beanName the bean.


<jsp:useBean>

<jsp:getProperty> Retrieves the name Defines the name for the

property of a property bean.


Defines the property from

which the values are to


bean or create a bean into a
defined scope be retrieved.

Specifies a name for the

bean.

Defines the property for

which values are to be set.

Defines an explicit value

name for the bean property.

property Defines the name of the

value request parameter to be


Used to set the property for
a bean param used.
<jsp:setProperty>

Defines a

parameter to be Defines the name of the

passed to an reference parameter.

included or name Defines the value of the

forwarded page value specified parameter


<jsp:param>

Defines the type of plug-in to

be included.

Embed a Java type Defines the name of the class

applets or a code to be executed by the plug-in.

JavaBean codebase Defines the path of the code


<jsp:plugin>
10. Explain <jsp:forward > tag with an example program.

 The JSP forward action tag is used to forward the control from one JSP page to
another JSP page or a servlet.
 This tag is often used to achieve the separation of concerns in web application
development.
 Instead of writing all the functionality in a single JSP page, we can split the
functionality into multiple JSP pages or servlets and use the forward action tag to
redirect the control.
 EXAMPLE:

index.jsp

<html>
<head>
<title>JSP forward example with parameters</title>
</head>
<body>
<jsp:forward page="display.jsp">
<jsp:param name="name" value="Chaitanya" />
<jsp:param name="site" value="BeginnersBook.com" />
<jsp:param name="tutorialname" value="jsp forward action" />
<jsp:param name="reqcamefrom" value="index.jsp" />
</jsp:forward>
</body>
</html>

display.jsp

<html>
<head>
<title>Display Page</title>
</head>
<body>
<h2>Hello this is a display.jsp Page</h2>
My name is: <%=request.getParameter("name") %><br>
Website: <%=request.getParameter("site") %><br>
Topic: <%=request.getParameter("tutorialname") %><br>
Forward Request came from the page: <
%=request.getParameter("reqcamefrom") %>
</body>
</html>

You might also like