0% found this document useful (0 votes)
21 views16 pages

13. JSP

The document provides an overview of Java Server Pages (JSP), including its purpose, lifecycle, and comparison with Servlets. It details various JSP tags, including directive, scriptlet, action, and custom tags, as well as the use of JavaBeans and JSTL. Additionally, it outlines examples of creating HTML and JSP files for user input and database interaction.

Uploaded by

bhanu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views16 pages

13. JSP

The document provides an overview of Java Server Pages (JSP), including its purpose, lifecycle, and comparison with Servlets. It details various JSP tags, including directive, scriptlet, action, and custom tags, as well as the use of JavaBeans and JSTL. Additionally, it outlines examples of creating HTML and JSP files for user input and database interaction.

Uploaded by

bhanu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Date:11/Jan/2023

----------------

JSP

---

- JSP stands for Java Server Pages

- JSP is used to develop server side dynamic web applications like Servlets

Servlets vs JSP

---------------

- In case of Servlets, Java code contains HTML code where as in case

of JSP, HTML code contains Java Code

- In case of Servlets, if the code is modified we need to recompile

and redeploy the application where as in case of JSP, if the code

is modified we need not recompile and redeploy the application

- JSP code is less than Servlet as in JSP we can use JSP tags,

JSP Implicit Objects and JSTL

JSP Life Cycle

--------------

- jspInit()

- called only once when JSP page is loaded

- _jspService()

- called for every request

- cannot be overridden
- jspDestroy()

- called only once when JSP page is destroyed

JSP Tags

--------

- Directive tags

- page directive tag => <%@page .... %>

- include directive tag => <%@include ... %>

- taglib directive tag => <%@taglib ... %>

- Scriptlet tags

- Declaration tag => <%! ... %>

- Expression tag => <%= ... %>

- Script tag => <% ... %>

- Action tags

- <jsp:include>

- <jsp:forward>

- <jsp:param>

- <jsp:useBean>

- <jsp:setProperty>

- <jsp:getProperty>

- Custom tags (self learning)

- used to create user defined tags in JSP

Page Directive tag


------------------

- <%@page ... %>

- used to import java packages

Ex:

<%@page import="java.util.*" %>

<%@page import="java.sql.*" %>

or

<%@page import="java.util.*,java.sql.*" %>

Refer program Date.jsp

include directive tag

---------------------

- <%@include ... %>

- used to include other JSP files

Ex:

<%@include file="header.jsp" %>

....

<%@include file="footer.jsp" %>

Refer programs

- header.jsp

- footer.jsp

- includedemo.jsp (run)
Scriptlet tags

--------------

Declaration tag

---------------

- <%! .... %>

- used to declare variables and to define methods

Ex:

<%! int n = 100; %>

<%! void xxx()

...

%>

<%! void jspInit()

...

%>

Expression tag

--------------

- <%= ... %>

- used to access the value of the variable and to call methods

- the code goes into _jspService()

Ex:

<%= n %> //100 is displayed on browser


<%= xxx() %>

<%= new java.util.Date() %>

Script tag

----------

- <% ... %>

- used to include Java statements

- the code goes into _jspService()

Ex:

<%

for(int i=1;i<=10;i++)

out.println(i);

%>

- out is an implicit object of JSP and it is an object of class JspWriter

- JspWriter is a subclass of PrintWriter

Create a HTML file which accepts a number from user.

Create a JSP file which reads the number from HTML file and displays

the factorial of the given number

Refer programs

- fact.html (run)

- Factorial.jsp

Create a HTML file which accepts a color from user.


Create a JSP file which reads the color from HTML file and sets the

background of webpage with the given color

Refer diagram BGColor.png

Refer programs

- color.html(run)

- BGColor.jsp

Note

----

request is an implicit object of JSP which is an object of HttpServletRequest interface

response is an implicit object of JSP which is an object of HttpServletResponse interface


Date:12/Jan/2023

----------------

JSP Tags

--------

- page directive tag => <%@page ... %> => used to import java packages

- include directive tag => <%@include ... %> => used to include other JSP files

- Declaration tag => <%! ... %> => used to declare variables and to define methods

- Expression tag => <%= ... %> => used to access the value of variable and to call methods

- Script tag => <% ... %> => used to include Java statements

JSP Action tags

---------------

- <jsp:include>

- <jsp:forward>

- <jsp:param>

- <jsp:useBean>

- <jsp:setProperty>

- <jsp:getProperty>
<jsp:include> and <jsp:forward>

-------------------------------

used to include other JSP files

Refer programs

- First.jsp (run)

- Second.jsp

- Third.jsp

In case of <jsp:include> the response is handled by all the JSP pages

where as in case of <jsp:forward> the response is handled by only

the last page

<jsp:param>

-----------

used to send the parameters from one JSP page to another JSP page

Refer programs

- JspParam1.jsp (run)

- JspParam2.jsp

Using JavaBeans in JSP

----------------------

JavaBean

--------
A JavaBean is a public class with private properties and public

setters and getter methods for every property

Ex:

public class Book

private int bno;

private String bname;

private double price;

public void setBno(int bno)

this.bno = bno;

public int getBno()

return bno;

...

...

Refer diagram JavaBeans in JSP.png


Example Application

-------------------

- Create a package "mypack" in WebProj

- Create a Javabean class "Book" in mypack package

Refer program Book.java

Generating Getters and Setters

------------------------------

Right click on Book.java -> Source -> Generate Getters and Setters

-> Click Select All -> Click Generate

- Create a welcome page "book.html" in WebProj

Refer program book.html

- Create a JSP file "SaveBook.jsp" in WebProj

Refer program SaveBook.jsp


- Create a JSP file "DisplayBook.jsp" in WebProj

Refer program DisplayBook.jsp

- Run the application book.html

JSTL

----

- JSTL stands for JSP Standard Template Library

- JSTL is a set of tags created by Apache Company

- In order to use JSTL tags we need to copy "jstl1.2.jar" into WEB-INF/lib folder

Refer programs

- JSTLCore1.jsp

- JSTLCore2.jsp

- JSTLCore3.jsp

- JSTLCore4.jsp

${} => expression language in JSP (EL)


Date:13/Jan/2023

----------------

Create a HTML file which accepts the customer details like

customer name, username, password, age and email.

Create a JSP page which inserts the customer details in Customer

database table

- Create a table "Customer" in MySQL

mysql>create table Customer (custName varchar(10), username varchar(10),

password varchar(10), age int(2), email varchar(20));

- Create a Javabean class "Customer" in mypack package of WebProj

Refer program Customer.java

- Create a welcome page "customer.html" in WebProj

Refer program customer.html

- Create a JSP file "SaveCustomer.jsp" in WebProj

Refer program SaveCustomer.jsp

- Create a class "InsertCustomer" in mypack package

Refer program InsertCustomer.java

- Create a JSP file "DisplayCustomer.jsp" in WebProj

Refer program DisplayCustomer.jsp

- Run the application customer.html


Hibernate - 3

Web Services - 2

Spring - 6

Microservices - 2

Angular - 3

Note

----

No classes on Monday and Tuesday i.e., 16/Jan and 17/Jan @7PM (IST)

You might also like