JSP Introduction in Java
Introduction to JSP
JSPHome
JSP's allow to design user interfaces. JSP pages used for presentation of content. Hard coding of HTML Code into Servlet, they invented the JSP.
Presentation code embedded inside servlets. This will make web developer should have servlet knowledge. But
JSP pages makes the flexibility in developing presentation content by
separating the application logic from presentation logic.
JSP is converted into servlets at the time of compilation. All the code which is written in scriptlets is inserted into servlets doGet() method.
Programming in JSP.
JSP Elements
<!-- --> HTML user will use (for documentation).
<%-- --%> script let are not visible in client output.
I. Scripting elements:
In this tutorial, you are just going to learn how to use script lets. There are 3 types of scripting elements available in JSP.
- script lets
- declarations
- expressions
<% %>---> script lets → contains doGet() method java code. <%! %>---> declaration → variables, method declarations goes here. <%= %>---> expression → mathematical expressions. Examples: <%= String var ="Welcome"; %> <%@ page import="java.util.Date()" %> <%! Date date = new Date(); %> <% out.println("Date="+date.toString()); %>
First.jsp
This example just prints some message on the browser's screen.
To print messages, JSP uses out object's println method.
out object is an implicit object.
You will learn in detail in further tutorial about out object.
- In eclipse create a webproject with some name ex: JSPWebapp
- Right click and select new → jsp → give the name as first.jsp.
- Remove the code in it and add the code line give below..
<!DOCTYPE>
<html>
<head>
<title>Codingzon JSP Demos</title>
</head>
<body>
<% out.println("<br>Welcome to JSP"); %>
</body>
</html>
Save it, and run first.jsp file. You will see the output as shown in picture.
expressions in JSP
This example shows how to use expression. expression is written as shown…
syntax:
<%= someexpression %>
expression does have semicolon as a line terminator
example:
<%= (5 * 5) %>
It is converted as
out.println((5*5));
expressiondemo.jsp
<!DOCTYPE html>
<html>
<head>
<title>Codingzon JSP Demos</title>
</head>
<body>
<%= ((5 * 5)) %>
</body>
</html>
expressiondemo2.jsp
An expression with string data.
<!DOCTYPE html>
<html>
<head>
<title>Codingzon JSP Demos</title>
</head>
<body>
<%= "<br>Hello How are you" %>
</body>
</html>
declarations
Show declaring a variables in JSP. It can be done using
syntax:
<%! Datatype variable = value; %>
example:
<%! String str= "John"; %>
declarationsdemo.jsp
<!DOCTYPE html>
<html>
<head>
<title> Codingzon JSP Demos</title>
</head>
<body>
<%! String str= "John";%>
<%
out.println("<br>Hello " + str);
%>
</body>
</html>
2. Scripting elements:
Continue..
Comments
Post a Comment