1 of 47

JSP and PHP both are server side scripting languages. But here is a list of reasons why JSP is better than PHP

  • Anything you can do with PHP, you can do with JSP; the reverse is not true.
  • JSP is much more powerful, since it has access to all the Java libraries. PHP only has access to PHP libraries.
  • JSP is Object-Oriented, so leads to cleaner code that's easier to debug, maintain, and improve. (PHP also allows objects, but the object model is more primitive, and most scripted pages ignore PHP objects and just use normal variables.
  • With JSP, if the code inside a page gets too big, or if you want to use it elsewhere, you can cut it out, make it into a Java class, and invoke it from anywhere in your application (even not from a page). With PHP, you're stuck inside the HTML box.

2 of 47

JSP Scriptlet tag (Scripting elements)

  • In JSP, java code can be written inside the jsp page using the scriptlet tag.

There are three types of scripting elements:

  • scriptlet tag
  • expression tag
  • declaration tag

3 of 47

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> 

4 of 47

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

5 of 47

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

6 of 47

JSP expression tag

  • The code placed within JSP expression tag is written to the output stream of the response.
  • So you need not write out.print() to write data.
  • It is mainly used to print the values of variable or method.

Syntax of JSP expression tag

<%=  statement %>

7 of 47

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

8 of 47

JSP Declaration Tag

  • The JSP declaration tag is used to declare fields and methods.
  • The code written inside the jsp declaration tag is placed outside the service() method of auto generated servlet.
  • So it doesn't get memory at each request.

Syntax of JSP declaration tag

<%!  field or method declaration %>  

9 of 47

Example of JSP declaration tag that declares field

<html>  

<body>  

<%! int data=50; %>  

<%= "Value of the variable is:"+data %>  

</body>  

</html> 

10 of 47

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>  

11 of 47

JSP Directives

  • The jsp directives are messages that tells the web container how to translate a JSP page into the corresponding servlet.
  • provide directions and instructions to the container, telling it how to handle certain aspects of the JSP processing.

There are three types of directives:

  1. page directive
  2. include directive
  3. taglib directive

12 of 47

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

13 of 47

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

14 of 47

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

15 of 47

info: Defines the string which can be printed using the ‘getServletInfo()’ method.

Syntax:

<%@page info="value"%>

Ex:

<%@page contentType = "text/html" %>

<% = getServletInfo()%>

16 of 47

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

17 of 47

<%@page contentType = "text/html" %>

<%@page isELIgnored = "true"%>

<body bgcolor = "blue">

<c:out value = "${'This is sparta}"/>

</body>

18 of 47

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

19 of 47

<h1>This is the content of a.html</h1>

A.html

<% = Local content%>

<%@include file = "a.html"%>

<% = local content%>

index.jsp 

20 of 47

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). 

  • The JavaServer Pages API allow you to define custom JSP tags that look like HTML or XML tags and a tag library is a set of user-defined tags that implement custom behavior.
  • The taglib directive declares that your JSP page uses a set of custom tags, identifies the location of the library, and provides means for identifying the custom tags in your JSP page.

Syntax

<@%taglib uri = "library url" prefix="the prefix to identify the tags of this library with"%>

21 of 47

<%@ 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.

22 of 47

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. Fast Development JSTL provides many tags that simplify the JSP.
  2. Code Reusability We can use the JSTL tags on various pages.
  3. No need to use scriptlet tag It avoids the use of scriptlet tag.

23 of 47

1. JSTL Core Tags:

  • The JSTL core tag provides variable support, URL management, flow control etc. The syntax used for including JSTL core library in your JSP is:

<%@ taglib uri=http://java.sun.com/jsp/jstl/core prefix=“c” %>

24 of 47

JSTL Core Tags List

25 of 47

JSTL Function Tags

  • The JSTL function provides a number of standard functions, most of these functions are common string manipulation functions.
  • The syntax used for including JSTL function library in your JSP is:
  • <%@ taglib uri=http://java.sun.com/jsp/jstl/functions prefix=“fn” %>

26 of 47

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()

27 of 47

JSTL XML tags:

  • The JSTL XML tags are used for providing a JSP-centric way of manipulating and creating XML documents. The xml tags provide flow control, transformation etc.
  • The url for the xml tags is http://java.sun.com/jsp/jstl/xml and prefix is x. The JSTL XML tag library has custom tags used for interacting with XML data.
  • The syntax used for including JSTL XML tags library in your JSP is:
  • <%@ taglib uri=http://java.sun.com/jsp/jstl/xml prefix=“x” %>

28 of 47

JSTL XML tags List

29 of 47

JSTL SQL Tags

  • The JSTL sql tags provide SQL support. The url for the sql tags is http://java.sun.com/jsp/jstl/sql and prefix is sql.
  • The SQL tag library allows the tag to interact with RDBMSs (Relational Databases) such as Microsoft SQL Server, mySQL, or Oracle.
  • The syntax used for including JSTL SQL tags library in your JSP is:

<%@ taglib uri=http://java.sun.com/jsp/jstl/sql prefix=“sql” %>

30 of 47

JSTL SQL Tags List

31 of 47

32 of 47

JSP Implicit object

  • JSP implicit objects are created during the translation phase of JSP to the servlet.
  • These objects can be directly used in scriplets that goes in the service method.
  • They are created by the container automatically, and they can be accessed using objects.
  • There are 9 jsp implicit objects.
  •  These objects may be accessed as built-in variables via scripting elements and can also be accessed programmatically by JavaBeans and Servlets.

33 of 47

9 implicit objects are

  • request
  • Response
  • Config
  • Application
  • Session
  • page context
  • page object
  • Exception
  • Out

34 of 47

request Object: �

  • This is the object of HttpServletRequest class associated with the request.
  • It can be used to get request information such as parameter, header information, remote address, server name, server port, content type, character encoding etc.
  • It can also be used to set, get and remove attributes from the jsp request scope.

35 of 47

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);  

%>  

36 of 47

response object

  • In JSP, response is an implicit object of type HttpServletResponse. The instance of HttpServletResponse is created by the web container for each jsp request.
  • It can be used to add or manipulate response such as redirect response to another resource, send error etc.

37 of 47

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");  

%> 

38 of 47

 JSP out implicit object�

  • For writing any data to the buffer, JSP provides an implicit object named out.

index.jsp

<html>  

<body>  

<% out.print("Today is:"+java.util.Calendar.getInstance().getTime()); %>  

</body>  

</html>  

39 of 47

JSP config implicit object�

  • In JSP, config is an implicit object of type ServletConfig. This object can be used to get initialization parameter for a particular JSP page. The config object is created by the web container for each jsp page.

40 of 47

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);  

%>  

41 of 47

42 of 47

JSP application implicit object

  • In JSP, application is an implicit object of type ServletContext.
  • The instance of ServletContext is created only once by the web container when application or project is deployed on the server.
  • This object can be used to get initialization parameter from configuaration file (web.xml). It can also be used to get, set or remove attribute from the application scope.
  • This initialization parameter can be used by all jsp pages.

43 of 47

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

44 of 47

<%   

  out.print("Welcome "+request.getParameter("uname"));  

  String driver=application.getInitParameter("dname");  

out.print("driver name is="+driver);  

  

%>  

welcome.jsp

45 of 47

session implicit object

  • in JSP, session is an implicit object of type HttpSession.The Java developer can use this object to set,get or remove attribute or to get session information.

46 of 47

pageContext implicit object

  • In JSP, pageContext is an implicit object of type PageContext class.The pageContext object can be used to set,get or remove attribute from one of the following scopes:page
  • request
  • session
  • application

In JSP, page scope is the default scope.

47 of 47

page implicit object

  • In JSP, page is an implicit object of type Object class.This object is assigned to the reference of auto generated servlet class.

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