Category Archives: JSP Tutorial

JSP Tutorial

JSP CRUD Example

JSP CRUD With Pagination

Here we will make an project of CRUD (Create, Read, Update and Delete) with JSP and also we are going to use Pagination in this project. If records are too many so on single page it takes time so it is better to break the page into parts so that loading time of page become reduce, The Pagination do work for this theory.

First we need to create a database for CRUD and Pagination. I have XAMPP in my system if you do not have please download it, Also you can use Oracle Database. Follow the steps for creation database.

 

1.step: After starting your XAMPP server open this link in browser http://localhost/phpmyadmin/

7

  1. Create an Database on click of “New”. I have created with name of “EmpManagement”.

8

  1. After successful creation of database now we need to create table, As per my requirement I have created a table with name of “Employees” and number of column is “7”. Check below image.

9

  1. Now we need to enter our columns name and also need to set data type like below image.

10

Now we are all set with database creation. Now we have to create Java Web Project in which we are going to use this Database. Check below steps for this.

 

 

#Create Project: In this step we will create new project with netbeans, click on “File>New Project” You will get screen like below image.

11

#click on “Next” button and then enter your project details like below image.

12

#Again we need to click on “Next” and then we will “Finish”.

13

#iAdd Library: so we are all set with project creation now let’s add some important library in our project ,which we are going to use in our project.

 

#jstl-1.2.jar: Let’s include JSTL(JSP Standard Tag Library) in your project

After downloading jar file, do right click on your project and then go to properties. Like below image.

14

Now go to libraries and then click on Add JAR/Folder Option.

15

 

#Select your jstl-1.2.jar from your directory and after including jar file you will get image like below.

16

#now click on Add Library… option and select MySQL JDBC Driver library and then click on Add Library as i did like below image.

17

#Now you will get screen like below image, simple click on “OK” button and these libraries will be added in your project.

18

Now we are done with including libraries in our project, let’s move to coding side.

Project Structure: Let’s create one by one some jsp and java classes for this project.The project structure will look like below image so please make classes according to this structure.

19

#UserBean.java:Java Bean are classes that encapsulate many objects into a single object. It contains constructor, Getter, Setter Methods. With the help of Setter and Getter method user can set and get data. Check below code.

#UserDAO.java: So this is User Data Access Object, here we will write queries of connection, Creation, Reading, Updating and Deletion and also for Pagination. Check below code.

#header.html: This will be our header which we will include in our jsp pages.

#index.jsp: This will be our launching page, do coding of this as I did, check below code.

 

#adduserForm.jsp: Here we have included userForm.html file , which will redirect to addUser.jsp page after submitting all details.Check below code.

#userForm.html: This is the html file in which we create a form for registration and then it redirect to addUser.jsp. Check below code.

#addUser.jsp: So from here we will add user. From this we have called save method of UserDAO class.

#AddUserSuccess.jsp: After successful registration this code will run.

#errorPage.jsp: If any exception or error occurred so this code will give error message.

#viewUsers.jsp: With the help of this code you can view all data of employees.

#updateForm.jsp: This will be our Update form, here you can update your details.

#UpdateUser.jsp: This code will run when you were done with updation.

#DeleteUser.jsp: If you want to delete any employee so you can delete with this code.

#paginationPage.jsp: So this code will work for pagination.

Download: You can simply download this project by below link.

 

 

#Output: Let’s run this project you will get output like below images.

#Home: This will be our Home Screen.

20

#Add Employee: This screen will appear when you click on “Add Employees” or “Add User”.

21

#AddUserSuccess: After successful registration this screen will appear.

22

#ViewUser: In this screen we can see all employees list.

23

#Edit: When you click on edit the below screen will appear. I have edit salary 12000 to 150000.

24

#After Update: After updation it will look like below image.

25

#Delete: When you click on delete so data will be removed from table, I have deleted user “KK”, So below screen will appear after deletion.

26

#Pagination: see pagination in below images.

27 28 29

 

So this was a simple example of CRUD with Pagination hopefully this will help you.

JSP Sending Email

JSP Sending Email

In this article we are going to send mail using JSP page. I have used simple format for creation your own mail API, which will based on two protocols SMTP and MIME. Follow the simple steps and you will get reach to your goal.

 

  1. Create Project: Simply create a project , I hope you are aware for creation of project with netbeans which is already discussed in our “Hello World” Web Application. So I am not going to explain here.
  2. Download Jar file: Before starting coding of this project we need to download jar files that are mail.jar and activation.jar files. By below link you can download jar files.

mail.jar:
activation.jar:

Simply add this jar files in your project library, If you do not how to add it just do right click on project and then go to Properties>Libraries, you will get screen like below image. Simply click on Add JAR/Folder Then click on “OK”, Your jar will be added to your project.

4

3.index.jsp: Now let’s do coding for our mail composer page. From here we will send details to mailProcess.jsp page through a form, check below code.

Output: The below screen will be the output of above code.here you need to enter email id of sender, subject of your mail and content of mail.

5

4.mailProcess.jsp: When you will click on “Send” button of above image after filling all details , so this page will do process of mail and will send to sender. Check below code.

Output: After entering correct email id of sende, click on Send button and wait for while, the below screen will appear.

6

JSP useBean Action Tag

JSP useBean Action Tag

This action tag is used in jsp page when we want to use java bean class in our jsp page. This tag is used to locate or instantiate a bean class. Depending on the scope this tag does not create the bean if bean object of the Bean class is already created. If the bean’s object is not created so it instantiates the bean.

Syntax:

After successful loading of bean class,we have to use jsp:setProperty and jsp:getProperty action tags to use the parameters of bean class.

 

jsp:setProperty: This action tag is used to sets a property value in bean.

Syntax:

jsp:getProperty: This tag is used to get the value from the bean object.

Syntax:

Attributes: Let’s get understand it’s attributes one by one.

 

#id:This attribute is used to identify the bean in the specified scope.

 

#scope: This attribute is used to represent the scope of bean that can be anything page,request,session,application. Let’s understand scope one by one.

 

  • page: This is the default scope of this attribute for useBean action tag and specifes that you can use it within this JSP page.
  • request: With the help of this scope you can this bean class from any jsp page which are processing same request. The scope of request is wider than page.
  • session: With the help of this scope you can use this bean from any jsp page which are using same session, it doesn’t matter that they are processing same request or not. The scope of session is wider than request.
  • application: With the help of this scope you can use this bean from any JSP page within application. The scope of application is wider than session.

 

#class: This attribute used to creates an object of bean class.

 

#type: If bean is already in scope so this attribute provides the bean a data type.

 

#beanName: This attribute is used to instantiates the bean with the help of java.beans.Beans.instantiate() method.

 

Example: In this example we will learn that how to use useBean, setProperty, getProperty action tags. Check below pages.

 

#userBean: First we need to create bean class, which will consist getter and setter method like below code.

#index.jsp: From here we will send some data to next jsp page, check below code.

#process.jsp: On this page we will do process of setting and getting data. Here I have declared useBean , setProperty and getProperty Tag. Please check below code.

Output: So we are all set with jsp:useBean, jsp:setProperty, jsp:getProperty tags, now just run this project and you will output like below images.

 

When you will run this project the index page will appear, fill your details in the fields like below image.

2

 

 

  1. Click on Sign up button after filling details, you will get output like below image.

3

 

JSP include Action Tag

JSP include Action Tag

This tag is used to include some external resources at runtime response. It may be JSP page, html file, or servlet. This tag includes resources at request time so it’s better for dynamic pages. It can be used to include both static or dynamic pages.

 

Now you will think that what is difference between JSP include directive and JSP include Action tag. Let ‘s see how they both are differ.

  1. JSP include directive include resources during translation phase but JSP include Action tag include resources during request phase.
  2. JSP include directive is better for static page but JSP include Action tag is better for dynamic page.
  3. JSP include directive includes original content in generated servlet but JSP include Action tag calls the include method.

The main advantage of this Tag is Code Reusability which means we can use a page many times like i have created header so i can use it in many pages, it saves time.

 

Syntax:

Syntax with Parameter:

Example: Let’s understand it by an simple example.

 

#header.html: Create a header html file so that we can include it in our JSP file.

#index.jsp: here we will include above header file, check below code.

Output: The output of this code will look like below image.

1

 

JSP forward action tag

JSP forward action tag

This tag is used to terminates the action of the current page and forwards the request to another page which is bind with forward tag, it may be static page, JSP page, or Java Servlet.

Syntax without parameter:

As we discussed in our previous article that param action tag we can use with forward tag and include tag, so let’s see how it will use in forward action tag.

Syntax with param:

Example : Let’s get understand it with simple example, In this example I did not use parameter action , we will do in next example.

 

#index.jsp: In this jsp we will declare our jsp:forward action tag and will set the path in “page” attribute. Check below code of this jsp page.

#forwardedPage.jsp: This jsp page will be appear instead of index.jsp page because we put this page in forward tag, check below code.

Output: So output of above code will look like below image.

13

Example with param action tag : In the below example we will get understand about jsp:forward tag with jsp:param tag.

 

#index.jsp: The same page i have used, apply only jsp:param action tag, check below tag.

#forwardedPage.jsp: In this page we will get our parameter like below code.

Output: The output of the above code will look like below image.

14

JSP Action Tags

JSP provides some Action Tags which we can use within JSP pages. It’s a bunch of standard action tag which are used for specific task. These all tags begin with jsp: prefix.

Syntax:

 

 

There are many Action Tags which are listed below and are used to perform some specific task.

 

1.jsp:forward: This tag is used to forward the request to new page.

Syntax:

 

 

 

2.jsp:useBean: This tag is used to instantiates a Java Bean.

Syntax:

 

 

 

4.jsp:setProperty: This tag is used to sets the value to the bean object.

Syntax:

 

 

5.jsp:getProperty: This tag is used to get the value from the bean object.

Syntax:

 

 

6.jsp:include: This tag is used to include some external resources at runtime response.

Syntax:

 

 

7.jsp:plugin: This tag is used to generates the browser specific code that makes an OBJECT or EMBED tag for the Java Plugin. In short it is used to insert Java component into JSP page.

Syntax:

 

 

 

8.jsp:params: This tag is used to sets the parameter value to request object. Mostly it is used with include and forward tag.

Syntax:

 

 

9.jsp:fallback: This tag can be used to print the message if plugin is working and also supplies alternate text if java plugin is unavailable. It is used in jsp:plugin Action Tag.

Syntax:

From next article we will discuss each tag briefly, It was simple overview of Action Tags.

Java Java Bean

Java Java Bean

Java Bean are classes that encapsulate many objects into a single object. It contains constructor, Getter, Setter Methods. With the help of Setter and Getter method user can set and get data. It also have zero argument constructor. Check out below code for more details.

 

Example: Let’s get understand it by an simple example.

 

#simpleBean.java: Create a java class for bean and do code as i did like below.

#testBean.java: This will be another java file from which we will set and get data from our bean class. Check below code.

Output: The output of above code will look like below image.

12

So this was the overview of bean class, we will use in next article of jsp:useBean action tag.

JSP Taglib Directive

JSP Taglib Directive

The taglib directive is used to define a tag library in which we can defines many tags. The JSP allows us to define custom tag and with the help of Tag Library Descriptor(TLD) we can define our tags.

Syntax:

Example: :Let’s get understand it by an example, we need to follow some steps for creation our own tag, see below.

 

#Need to create TagHandler java class: With the help of this class we can perform action at the start and at the end of tag. Check below code.

MyTagHandler.java: This is the java file which I created for TagHandler.

#Need to create TLD(Tag Library Descriptor): Here we will define our tag.

1.step: do right click on your project and then “New>Tag Library Descriptor” like below image.

7

If you are unable to find here click on “Other” and then “Web>Tag Library Descriptor” like below image.

 

9

2.Step:Click on Next and define your tld file name,and then finish it, like below image

10

3.step: Define Tag: After creation tld file we have to define our tag here like i did, please check below code.

customtag.tld:

#Create JSP file: In this file we will use our custom tag.create a jsp file and paste below code there.

Index.jsp:

#Run: So we all set with taglib directive, Simply run this project you will get output like below image.

11

 

JSP Directives Elements

JSP Directives Elements

Directives are elements that relay message to the JSP container and affect how it compiles the JSP page. The JSP directives gives direction to server that how to do process of a page.

#Syntax:

There are three types of directives in JSP:

There are three types of directives in JSP:

 

  1. Page Directive: This directive is used to defines page-dependent attributes.

    Syntax:

  2. Include Directive: This directive is used to insert the text contained in another file, it may be static content or another jsp page.

    Syntax:

  3. Taglib Directive: This directive is used to declare custom tag in jsp page.

    Syntax:

Let’s get more understand about these directives.

 

  1. Page Directives: The jsp:directive.page element defines a number of page-dependent properties and communicates these to the JSP container.You can use page directives anywhere in your JSP page. Some attributes are listed below which are used in Page directives tag.

    # buffer

    #import

    #contentType

    # errorPage

    #isErrorPage

    #extends

    #info

    #language

    #isELIgnored

    #isThreadSafe

    #autoFlush

    #session

    #pageEncoding

 

 

1.buffer: This attributes is used to specify the buffer size in KB so that we can handle output generated by JSP page. The default value of buffer attribute is 8kb, we can change it according to our requirement.
Syntax is:

if you want to change the value of buffer so just replace “value” as you want.

Example: Let’s do it in JSP page, like below code.

Output: The output will be look like below image.

2

2.import: This attribute is most useful attributes in Page Directives. By the name it can be understand that with the help of this attribute we can import other java classes, interface , packages etc.

Syntax:

So on place of value you can use anything which is valid and you want to important. Like if you want to import java.util.Date class so you can do it like below code.

 

Example: Let’s understand it by an simple example like I did in jsp, check below code.

Output: The output of this attribute of page directive will look like below image.

3

3.contentType: This attribute is used to define contentType of JSP page, the default contentType is text/html.

Syntax:

 

 

Default value:

You can change it according to you like if you want to change it in xml so just enter the value like below code

 

 

Example: In below example we have used contentType as “msword”, checkout how it will respond.

Output: Simply run this code and the output will instruct you to download the ms word file and then you can open it. Check out below image.

4

Simply “Save file” or “Open with” and you will get output like below image.

 

5

 

4.errorPage: The errorPage attribute is used to handle exception, if any error is occurred so we can simply use errorPage for this, the brief example of errorPage is briefly described in exception implicit object, here i have used an simple example so that we did not use to go back for it.

 

Example: You need to create an exception.jsp page, which will shown when an error occurred.

 

#exception.jsp:

 

#directive.jsp: Include exception.jsp page in this jsp , which is called when an error occurred.

#Output: This two jsp files will give result like below image to us. Because 1 can not be divided by 0 so an exception will occurred.

 

6

5.isErrorPage: This attribute of page directive is used to specify that current page is used as an error page for other jsp page or not. The default value is “false”, If it is “true” means it will use when an exception will occurred else it will not use. As you can see I have used it in above attribute that is “errorPage” the line is

 

If I did it “false” so this will no more in use.

Syntax:

Example: Check above example, that is related to this attribute also.

 

 

6.extends: The extends means you are inheriting parent class , means using inheritance in JSP page. This is rarely used and we can use it if we have extended HttpServlet and overridden some of it’s implementation.

 

Syntax:

7.info : We can use this attribute to sets the information of the JSP page and later on we can retrieve it by using getServletInfo() method of servlet interface.

 

Syntax:

 

 

Value can be your message like:

Example: Need to do

 

 

8.language: This attribute specify the scripting language which is used in the page. The default value is “Java” and this is only language is supported by it.

Syntax:

  1. isELIgnored: With the help of this attribute we can Ignore Expression Language(EL) in jsp. Expression Language is enabled by default, it means the default value is “false”. We can ignore it by doing it “true”.

Syntax:

 

 

10.isThreadSafe: The Servlet and JSP page both are support multithreaded means more than one thread can execute simultaneously. This attribute is allows you to handle multithreading concept, If you do not want to use multithreading so just change the value as “false” and if you want to allow multithreading in your jsp page just do it “true”.

Syntax:

  1. autoFlush: This attribute is used to handle buffer , if it is “true” means buffer should be flushed whenever it is full and if it is false so it will throw an exception whenever buffer overflows.

Syntax:

12.session: By default JSP page creates a session, The default value for session is “True” , If we do not want that it creates default session so just do it “false”. Sometimes we do not need session in JSP page so in that case this attribute will use.

Syntax:

13.pageEncoding:This attribute is used to sets the response encoding type of our JSP page. The default value of this attribute is “ISO-8859-1”.

Syntax:

 

 

 

JSP Include Directive

JSP Include Directive

The include directive is used to include external files during the translation phase. Translation phase is the phase when JSP page get converted into the equivalent servlet. We can directly add in jsp but we have to write code each JSP page, let’s get understand it by simple example: suppose we have 50 JSP file and we want to add header in each file so write code to each JSP page will be bad idea, we can simply create a header file and then we can include it in each jsp. If we don’t like header color so we will go to header file and simply change the color, it will change in each JSP page. The main purpose of include directive is code re-usability.

Syntax:

Example: Let’s get understand it by an simple example. Here I have created two files,

1.header.html

2.index.jsp

 

1.header.html: We will include this file in our index.jsp page. I have created some menus in header file, check below code.

2.index.jsp: It will be our launching page for our Java Web Application and also we will include our header.html file in this jsp page. Check below code.

Output: So we are done with our Include directive coding, run this code you will find output like below image.

1

JSP Session

JSP Session

This is the instance of javax.servlet.http.HttpSession which provides a way to identify a user across more than one page request. It behaves like same as session in servlet. The session is used for Add, Remove attributes and also we can get session information of logged in user.

#Setting Attributes in Session: With the help of setAttribute(String name, Object value) Method we can set our data in session.

#Getting Attributes from Session: With the help of getAttribute(String name) Method we can get our data from session. It returns the object bound with the specified name in the session.

Example:Let’s get understand it by an example. Create index.jsp file in your project like below

#index.jsp

#Output: The output of this index.jsp file will look like below image.

15

#session.jsp: From here we will set attributes in our session, check below code.

 

#Output: When you will click on index.jsp’s “click” button so this code will run and the output of this above code will look like below image and also we have set attributes in session from above code.

16

#session_second.jsp: When you will click on “Next Page” of “session.jsp” page, The below code will run. Here we will get session attributes which you have set from “session.jsp” page.

#Output: The output of this code will look like below image.

17