JSP and PHP both are server side scripting languages. But here is a list of reasons why JSP is better than PHP
JSP Scriptlet tag (Scripting elements)
There are three types of scripting elements:
JSP scriptlet tag
A scriptlet tag is used to execute java source code in JSP.
Syntax is as follows:
<% java source code %>
Example:
<html>
<body>
<% out.print("welcome to jsp"); %> </body>
</html>
Creating a simple JSP Page
<html>
<body>
<% out.print(2*5); %>
</body>
</html>
How to run a simple JSP Page?
Follow the following steps to execute this JSP page:
Start the server
Put the JSP file in a folder and deploy on the server
Visit the browser by the URL http://localhost:portno/contextRoot/jspfile,
for example
http://localhost:8080/myapplication/index.jsp
Example of JSP scriptlet tag that prints the user name
<html>
<body>
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
</body>
</html>
index.html
<html>
<body>
<%
String name=request.getParameter("uname");
out.print("welcome "+name);
%>
</form>
</body>
</html>
welcome.jsp
JSP expression tag
Syntax of JSP expression tag
<%= statement %>
Adding dynamic content via expressions
<html>�<body>
Hello! The time is now <%= new java.util.Date()%>
</body>
</html>
Note:
The character sequences enclose “<%= %>”Java expressions, which are evaluated at run time
JSP Declaration Tag
Syntax of JSP declaration tag
<%! field or method declaration %>
Example of JSP declaration tag that declares field
<html>
<body>
<%! int data=50; %>
<%= "Value of the variable is:"+data %>
</body>
</html>
Example of JSP declaration tag that declares method
<html>
<body>
<%!
int cube(int n)
{
return n*n*n*;
}
%>
<%= "Cube of 3 is:"+cube(3) %>
</body>
</html>
JSP Directives
There are three types of directives:
Syntax of JSP Directive�<%@ directive attribute="value" %> �
Page Directives :JSP page directive is used to define the properties applying the JSP page, such as the size of the allocated buffer, imported packages and classes/interfaces, defining what type of page it is etc.
The syntax of JSP page directive is as follows: � <%@page attribute = "value"%>
Different properties/attributes
import: This tells the container what packages/classes are needed to be imported into the program.�Syntax: <%@page import = "value"%>
Ex:
<%@page import = "java.util.Date"%>
<%Date d = new Date();%>
<%=d%>
contentType: This defines the format of data that is being exchanged between the client and the server. It does the same thing as the setContentType method in servlet used to.�Syntax:
<%@page contentType="value"%>
Ex:
<%@page contentType = "text/html" %>
<% = "This is sparta" %>
info: Defines the string which can be printed using the ‘getServletInfo()’ method.
Syntax:
<%@page info="value"%>
Ex:
<%@page contentType = "text/html" %>
<% = getServletInfo()%>
buffer: Defines the size of the buffer that is allocated for handling the JSP page. The size is defined in Kilo Bytes.�Syntax:
<%@page buffer = "size in kb"%>
language: Defines the scripting language used in the page. By default, this attribute contains the value ‘java’.
isELIgnored: This attribute tells if the page supports expression language. By default, it is set to false. If set to true, it will disable expression language.�Syntax:
<%@page isElIgnored = "true/false"%>
<%@page contentType = "text/html" %>
<%@page isELIgnored = "true"%>
<body bgcolor = "blue">
<c:out value = "${'This is sparta}"/>
</body>
Include directive :
JSP include directive is used to include other files into the current jsp page. These files can be html files, other jsp files etc. The advantage of using an include directive is that it allows code re-usability.
syntax of an include directive� <@%include file = "file location"%>
<h1>This is the content of a.html</h1>
A.html
<% = Local content%>
<%@include file = "a.html"%>
<% = local content%>
index.jsp
Taglib Directive
Taglib Directive : The taglib directive is used to mention the library whose custom-defined tags are being used in the JSP page. It’s major application is JSTL(JSP standard tag library).
Syntax:
<@%taglib uri = "library url" prefix="the prefix to identify the tags of this library with"%>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/core”prefix = "c” %>
<c:out value = "${'This is Sparta'}"/>
In the above code, we’ve used to taglib directive to point to the JSTL library which is a set of some custom-defined tags in JSP that can be used in place of the scirptlet tag (<%..%>). The prefix attribute is used to define the prefix that is used to identify the tags of this library. Here, the prefix c is used in the <c:out> tag to tell the container that this tag belongs to the library mentioned above.
JSTL (JSP Standard Tag Library): The JSP Standard Tag Library (JSTL) represents a set of tags to simplify the JSP development.
Advantage of JSTL:
1. JSTL Core Tags:
<%@ taglib uri=http://java.sun.com/jsp/jstl/core prefix=“c” %>
JSTL Core Tags List
JSTL Function Tags
JSTL Function Tags List:
1. fn:contains()
2. fn:indexOf()
3. fn:trim()
4. fn:startsWith()
5. fn:split()
6. fn:toLowerCase()
7. fn:toUpperCase()
8. fn:substring()
9. fn:substringAfter()
10. fn:substringBefore()
11.fn:length()
12. fn:replace()
JSTL XML tags:
JSTL XML tags List
JSTL SQL Tags
<%@ taglib uri=http://java.sun.com/jsp/jstl/sql prefix=“sql” %>
JSTL SQL Tags List
JSP Implicit object
9 implicit objects are
request Object: �
index.html
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
welcome.jsp
<%
String name=request.getParameter("uname");
out.print("welcome "+name);
%>
response object
index.html
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
welcome.jsp
<%
response.sendRedirect("http://www.google.com");
%>
JSP out implicit object�
index.jsp
<html>
<body>
<% out.print("Today is:"+java.util.Calendar.getInstance().getTime()); %>
</body>
</html>
JSP config implicit object�
index.html
<form action="welcome">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
web.xml
<web-app>
<servlet>
<servlet-name>sonoojaiswal</servlet-name>
<jsp-file>/welcome.jsp</jsp-file>
<init-param>
<param-name>dname</param-name>
<param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>sonoojaiswal</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
</web-app>
welcome.jsp
<%
out.print("Welcome "+request.getParameter("uname"));
String driver=config.getInitParameter("dname");
out.print("driver name is="+driver);
%>
JSP application implicit object
<form action="welcome">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
index.html
web.xml
<web-app>
<servlet>
<servlet-name>sonoojaiswal</servlet-name>
<jsp-file>/welcome.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>sonoojaiswal</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
<context-param>
<param-name>dname</param-name>
<param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>
</context-param>
</web-app>
<%
out.print("Welcome "+request.getParameter("uname"));
String driver=application.getInitParameter("dname");
out.print("driver name is="+driver);
%>
welcome.jsp
session implicit object
pageContext implicit object
In JSP, page scope is the default scope.
page implicit object
It is written as:
Object page=this;
For using this object it must be cast to Servlet type.
For example:
<% (HttpServlet)page.log("message"); %>
Since, it is of type Object it is less used because you can use this object directly in jsp.
For example:<% this.log("message"); %>