Implementing Bean with scriptlet in JSP

We can use all of the JSP coding while using Java Beans in a JSP page. There are three main kinds of JSP scripting elements in JSP.

Implementing Bean with scriptlet in JSP


Example for implementing bean with scriptlet <% code %> in a JSP page

We can use all of the JSP coding while using Java Beans in a JSP page. There are three main kinds of JSP scripting elements in JSP.

  • <%= expression %> is called Expression to evaluate value in output
  • <%  somecode  %> is called Scriptlet that are inserted into serlet's "service" method
  • <%! declaration %> is called JSP Declaration for declaring objects of components. 

JSP Scriptlets

If you want to do some coding of multiple lines or of single lines rather than inserting a single expression then you can use JSP scriptlets in the following way : 

<% Java Code %>

For example if you have to use "if-else" in your JSP page you can use it like this:

   if(condition) {
  some statements..

   some statements..

In this way, JSP scriptlet lets you do java coding in a JSP page wherever you want. Following example will describe you to use JSP Scriptlet in a JSP page using Java Beans. In our example we have made a bean file which is making the database connection in its constructor and after getting connection according to getter and setter method it will insert values into database by insert() method of this bean. 

For inserting data into bean file we are using MySQL database and connecting to the database "messagepaging" and table is "message" and structure of this database table is given as below:

CREATE TABLE `message` (
`id` int(11) NOT NULL auto_increment,
`message` varchar(256) default NULL,

Bean file code for this example bean is given as below:

package myexample;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
public class bean 
  private int msgid;
  private String message;
  private Connection connection=null;
  private ResultSet rs = null;
  private Statement st = null;
  String connectionURL = "jdbc:mysql://";
  public bean() 
 try {
  // Load the database driver
  // Get a Connection to the database
  connection = DriverManager.getConnection(connectionURL, "root""root")
catch(Exception e){
System.out.println("Exception is ;"+e);
  public void setmsgid(int msgid){
     this.msgid = msgid;
  public int getmsgid(){
    return (this.msgid);

   public void setmessage(String message){
    this.message = message;
   public String getmessage(){
    return (this.message);
   public void insert(){
  String sql = "insert into message(id,message) 
  Statement s = connection.createStatement();
  s.executeUpdate (sql);
  s.close ();
(Exception e){
  System.out.println("Exception is ;"+e);

This "" file has two properties "msgid" and "message" which are having their setter and getter to set and get properties values and these properties are being set in the JSP file using following code

<jsp:useBean id="sample" class="myexample.bean" scope="page">
   <jsp:setProperty name="sample" property="*"/>

 And by using scrpitlet in JSP we are calling insert method when the form will be submit.

<% sample.insert();%>

where "sample" is the id assigned for "" in myexample package. Full JSP code for "jspBean.jsp" is given as:

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

<head><title>Using Scriptlet to use Bean</title></head>
<body bgcolor="#ffccff">
<h1>Using JSP Scriptlet to use Bean</h1>
<form name="form1" method="POST">

  <table border="0" width="35%">
  <td width="10%"><b>ID</b></td>
  <td width="90%"><input type="text" name ="msgid"></td>
  <td width="10%"><b>Message</b></td>
  <td width="90%"><input type="text" name ="message"> </td>
  <td width="100%" colspan="2" align="center"><p align="left">
   <input type = "submit" value="Submit">
<jsp:useBean id="sample" class="myexample.bean" scope="page">
  <jsp:setProperty name="sample" property="*"/>

<!--Bean method called using JSP scriptlet -->
<% sample.insert();%>

To run this example you can go through the following steps:

  • Create and Save "".
  • Compile and put class file into classes folder
  • Create and save jspBean.jsp file and put this in a folder (e.g JSPexample in our example)
  • Start Tomcat Server and type the following URL in address bar


Download Sourcecode

Share on Google+Share on Google+

Implementing Bean with scriptlet in JSP

Posted on: July 4, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Implementing Bean with scriptlet in JSP   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
July 17, 2012

October 5, 2012

Thanks for the article.It helped me to complete my project. thanks a lot.