Java Unit 6
Java Unit 6
JSP stands for Java Server Pages. It is a server-side technology which is used
for creating web applications. It is used to create dynamic web content. JSP
consists of both HTML tags and JSP tags. In this, JSP tags are used to insert
JAVA code into HTML pages. It is an advanced version of Servlet Technology
i.e. a web-based technology that helps us to create dynamic and platform-
independent web pages. In this, Java code can be inserted in HTML/ XML pages
or both. JSP is first converted into a servlet by the JSP container before
processing the client’s request. JSP has various features like JSP Expressions,
JSP tags, JSP Expression Language, etc.
Difference between JSP & Servlet
Servlet JSP
Servlet is a java code. JSP is a html based code.
Writing code for servlet is JSP is easy to code as it is java in html.
harder than JSP as it is html
in java.
Servlet plays a controller JSP is the view in MVC approach for
role in MVC approach. showing output.
Servlet is faster than JSP. JSP is slower than Servlet because the
first step in JSP lifecycle is the
translation of JSP to java code and then
compile.
Servlet can accept all JSP only accept http requests.
protocol requests.
In Servlet, we can override In JSP, we cannot override its service()
the service() method. method.
In Servlet by default session In JSP session management is
management is not enabled, automatically enabled.
user have to enable it
explicitly.
In Servlet we have to In JSP business logic is separated from
implement everything like presentation logic by using javaBeans.
business logic and
presentation logic in just
one servlet file.
Modification in Servlet is a JSP modification is fast, just need to
time consuming task click the refresh button.
because it includes
reloading, recompiling and
restarting the server.
Either we can search services on our pc and start the tomcat server there.
If the setup is pulled off successfully, type http://localhost:8080/ on browser.
we will receive output on successful execution −Tomcat Home page
To stop tomcat, use the following command
%CATALINA_HOME%\bin\shutdown
or
C:\apache-tomcat-9.0.34\bin\shutdown
Setting up CLASSPATH in JSP
For Windows, put the following in your C:\autoexec.bat file.
set CATALINA = C:\apache-tomcat-9.0.34
set CLASSPATH = %CATALINA%\common\lib\jsp-api.jar;%CLASSPATH%
1) Expression
2) Scriptlets
3) Declarations
Expression tag must begin with <%= Inside Expression tag, the user embeds any
Java expression. Expression tag ends with the notation %>.
2) Scriptlets: Scriptlet tag is a type tag used for inserting Java Code into JSP. To begin
the Scriptlet tag, the user must add <%. Inside the Scriptlet tag, the user can add
any valid Scriptlet, meaning any valid Java Code. User can write the code in between
the Scriptlet tag accesses any variable or been declared. The Scriptlet tag ends with
the notation %>.There must be semicolon; included at the end of each code inside
the Scriptlet tag
Syntax:
Scriptlet tag
<\% 0 statement1;
statement2:
For example,
%>;
Syntax:
Declaration Tag
statement2;
2. JSTL Formatting and Localisation Tags: JSTL Formatting tags are provided
for formatting of Numbers, Dates and i18n support through locales and
resource bundles. We can include these jstl tags in JSP with below syntax:
<%@ taglib uri="https://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
3. JSTL SQL Tags: JSTL SQL Tags provide support for interaction with
relational databases such as Oracle, MySql etc. Using JSTL SQL tags we can
run database queries, we include these JSTL tags in JSP with below syntax:
<%@ taglib uri="https://java.sun.com/jsp/jstl/sql" prefix="sql" %>
4. JSTL XML Tags: JSTL XML tags are used to work with XML documents such
as parsing XML, transforming XML data and XPath expressions evaluation.
Syntax to include JSTL XML tags in JSP page is:
<%@ taglib uri="https://java.sun.com/jsp/jstl/xml" prefix="x" %>
5. JSTL Functions Tags: JSTL tags provide a number of functions that we can
use to perform common operation, most of them are for String
manipulation such as String Concatenation, Split String etc. Syntax to
include JSTL functions in JSP page is:
<%@ taglib uri="https://java.sun.com/jsp/jstl/functions" prefix="fn" %>
User input is a necessity in modern web pages. Most dynamic web sites generate
pages based on user input submitted through an HTML form. Some times, users
seldom enter information in exactly the format we need, so before one can use such
input, he/she need to validate it to make sure it's usable.
Web browsers are also particular about the format of the HTML one send them. For
example, when one generates an HTML form with values taken from a database, a
name such as Ma’am can cause problems. The single quote character after the a can
fool the browser into believing it's at the end of the string, so one can end up with
just an a in the form.
Reading Request Parameter Values: JSP handles form data parsing automatically
using the following methods depending on the situation:
2) getParameter Values(): Call this method if the parameter appears more than once
and returns multiple values, for example checkbox.
4) getInputStream(): Call this method to read binary data stream coming from the
client.