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..
}
else{
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:
- Bean file code for this example bean is given as below:
package myexample;
import java.io.*;
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://192.168.10.59/messagepaging";
public bean()
{
try {
// Load the database driver
Class.forName("com.mysql.jdbc.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(){
try{
String sql = "insert into message(id,message)
values('"+msgid+"','"+message+"')";
Statement s = connection.createStatement();
s.executeUpdate (sql);
s.close ();
}
catch(Exception e){
System.out.println("Exception is ;"+e);
}
}
}
- This "bean.java" 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="*"/>
</jsp:useBean>
- And by using scrpitlet in JSP we are calling insert method when the form will be submit.
where "sample" is the id assigned for "bean.java" in myexample package.
- Full JSP code for "jspBean.jsp" is given as:
<%@ page language="Java" import="java.sql.*" %>
<html>
<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%">
<tr>
<td width="10%"><b>ID</b></td>
<td width="90%"><input type="text" name ="msgid"></td>
</tr>
<tr>
<td width="10%"><b>Message</b></td>
<td width="90%"><input type="text" name ="message"> </td>
</tr>
<tr>
<td width="100%" colspan="2" align="center"><p align="left">
<input type = "submit" value="Submit">
</td>
</tr>
</table>
<jsp:useBean id="sample" class="myexample.bean" scope="page">
<jsp:setProperty name="sample" property="*"/>
</jsp:useBean>
<!--Bean method called using JSP scriptlet -->
<% sample.insert();%>
</form>
</body>
</html>