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

Java Unit 6

JSP stands for Java Server Pages and is used for creating dynamic web content by inserting Java code into HTML pages. JSP is converted into a servlet before processing requests. It allows for separating business logic from presentation logic and is easier to code than servlets as Java code can be written directly in HTML pages. The key components needed for a JSP environment are a JDK, Tomcat web server, and an HTML editor. Dynamic content can be generated using expressions, scriptlets, and declarations directly in JSP pages.

Uploaded by

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

Java Unit 6

JSP stands for Java Server Pages and is used for creating dynamic web content by inserting Java code into HTML pages. JSP is converted into a servlet before processing requests. It allows for separating business logic from presentation logic and is easier to code than servlets as Java code can be written directly in HTML pages. The key components needed for a JSP environment are a JDK, Tomcat web server, and an HTML editor. Dynamic content can be generated using expressions, scriptlets, and declarations directly in JSP pages.

Uploaded by

samreen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

JSP

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.

Setting Up the JSP Environment


Java Server Page or JSP is very affordable as most of the software needed for it,
is easily available for free or at low cost and these software are:
1) Java Developer Kit,
2) Tomcat web server, and
3) HTML editor.
To set-up the JSP environment in Microsoft Windows
1. Java Development Kit
Step 1: Verify Presence of JDK: The first and foremost step before setting-up
the JSP environment is to verify the presence of Java Developer Kit or JDK on
the programmer's system. If it is not present, the Java Developer Kit or JDK must
be downloaded and installed.
Step 2: Run Executable Set-Up: After the download, the next step is to run the
executable set-up, or the exe, and follow the prompted instructions on screen.
Step 3: Setting the PATH and CLASSPATH: Once you download Java
implementation, follow the given instructions to install and configure the setup.
Set the PATH and JAVA_HOME environment variables. This path will refer to the
destination where java/javac are there. It typically can be java_install_dir/bin.
For Windows:
set PATH = C:\jdk 14.0.1\bin;%PATH%
set JAVA_HOME = C:\jdk 14.0.1

2. Installing and Setting up Tomcat: Web Server


Step 1: Install Web Server
Step 2: Click Next to install Tomcat.
Step 3: Agree to the terms and conditions in order to proceed further.
Step 4: In this step, you will select the normal or full installation of Tomcat. For
newbies, it is better to have full installation as it contains examples as well.
Step 5: Set up the ports, admin, and password to run your server.
Step 6: You will see that JVM asks to select the path. If Java JDK is in program
files, then Tomcat should be in the same location.
Step 7: Set up the path for your Tomcat server.
Step 8: Click on the finish button, and you are all done with installing the
Tomcat server.
3. Starting and Stopping Tomcat
If the Tomcat as service require to be start, then use the following command-
%CATALINA_HOME%\bin\startup.bat
or
C:\apache-tomcat-9.0.34\bin\startup.bat

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%

Generating Dynamic Content

The content script can be generated in the following way

1) Expression

2) Scriptlets
3) Declarations

1) Expressions: Expression tag is used to display output of any data on the


generated page. The data placed in Expression tag prints on the output stream
and automatically converts data into string The Expression tag can contain any
Java expression used for printing output equivalent to out.println(). Thus, an
expression tag contains a scripting language expression which is evaluated,
automatically converts data to a String and the outputs are displayed.

Expression tag must begin with <%= Inside Expression tag, the user embeds any
Java expression. Expression tag ends with the notation %>.

Expression should not contain a semicolon between codes, as with Declaration


tag.
Syntax:
< %=Statement %>;

2) Scriptlets: Scriptlet tag is a type tag used for inserting Java Code into JSP. To begin
the Scriptlet tag, the user must add &lt;%. 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 %&gt;.There must be semicolon; included at the end of each code inside
the Scriptlet tag

Syntax:

Scriptlet tag

<\% 0 statement1;

statement2:

\%>: //end of Scriptlet tag

For example,

for (int j = 0 j &lt;5; j++)


out.print(j);

%>;

3) Declarations: Declaration tag is used to define functions, methods and variables


that will be used in Java Server Pages. At the start of Declaration tag one must place
&lt;%! Inside Declaration tag one can declare variables or methods. Declaration tag
ends with the notation %&gt;. Also care must be taken to place a semicolon, i.e., at
the end of each code placed inside Declaration tag.

Syntax:

Declaration Tag

<%! //start of declaration tag

statement1; //variables or methods declaration

statement2;

\%>; //end of declaration tag


For example,
<%!
Private int example=0;
Private in test=5;
%>;
Java Server Pages Standard Tag Library
Java Server Pages Tag Library (JSTL) is a set of tags that can be used for
implementing some common operations such as looping, conditional
formatting, and others.
JSTL aims to provide an easy way to maintain SP pages.The use of tags defined
in JSTL has Simplified the task of the designers to create Web pages. They can
now simply use a tag related to the task that they need to implement in a JSP
page.
JSTL jars are container specific, for example in Tomcat, we need to
include jstl.jar and standard.jar jar files in project build path. If they are not
present in the container lib directory, you should include them into your
application.
1. JSTL Core Tags: JSTL Core tags provide support for iteration, conditional
logic, catch exception, url, forward or redirect response etc. To use JSTL
core tags, we should include it in the JSP page like below.

<%@ taglib uri="https://java.sun.com/jsp/jstl/core" prefix="c" %>

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" %>

Processing Input and Output

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:

1) getParameter(): One can call request.getParameter() method to get the value of a


form parameter.

2) getParameter Values(): Call this method if the parameter appears more than once
and returns multiple values, for example checkbox.

3) getParameterNames(): Call this method if you want a complete list of all


parameters in the current request.

4) getInputStream(): Call this method to read binary data stream coming from the
client.

You might also like