JSP Using JavaBeans
JavaBeans components are Java classes that can be easily reused and composed together into
applications. Therefore a JavaBean can be defined as a reusable software component. we can
write a JavaBean that can then be used in a variety of other Java based softwares such as
applications, Servlets or JSP pages. In this way we can define our business logic within a JavaBean
and then consistently use that logic in separate applications.
Benefits of JavaBeans
By using JavaBeans we can fully separate the business logic from the generation of the display.
This is an important philosophy that leads to better structured and more maintainable systems.
We would use the JavaServer Page to dynamically generate display and also to handle the user
interaction. The other advantage of using JavaBeans is that the business logic can be used by
more than one application. For example, both a client based Java application and a JSP page can
access the same JavaBean thus guaranteeing the same functionality.
Example
Let’s take another Bean Example which will calculate the area of a circle provided its radius value
is given. This radius value is taken as input from a HTML form. After clicking the “Submit” button
in the HTML form, the control will be transferred to a JSP page which will call the Bean. The
example codes are placed within BeanTest directory (under webapps).
The Example JavaBean
[Link]
package [Link];
public class Circle {
double radius;
double area;
public void setRadius(double r) {
radius = r;
}
public double getRadius(){
return radius;
}
public double getArea(){
return 3.14*radius*radius;
}
}
The HTML form
[Link]
<html>
<body>
<center>
<form name="Form1" method="post" action="[Link]">
<B>Enter a value for radius:</B>
<input type=text name="data" size=10 value="">
<input type=submit value="Submit">
</form>
</body>
</html>
The JSP Page
[Link]
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<%!
String value;
double result;
%>
<%
value = [Link]("data");
result = [Link](value);
%>
<jsp:useBean id="circle1" class="[Link]" scope="page" />
<jsp:setProperty name="circle1" property="radius" value="<%= result %>" />
<b>RADIUS: </b> <jsp:getProperty name="circle1" property="radius" />
<br><br>
<b>AREA </b> <jsp:getProperty name="circle1" property="area" />
</body>
</html>
Output
JSP Implicit Objects
Object Type Description
out [Link] The output stream for the page
request [Link] The current request to the page
Used for sending a response to the
response [Link]
client
session [Link] The user’s session
config [Link] The Servlet’s configuration
application [Link] The web application’s environment
page [Link] The page’s servlet instance
pageContext [Link] The page’s environment
exception [Link] Used for error handling purposes
1) out
The implicit object out is probably the most frequently used implicit object. We call either
its print method or its println method to send text or other data to the client browser. In a servlet,
we always need to call the getWriter() method of the [Link]
interface to obtain a PrintWriter before we can output anything to the browser, as follows:
PrintWriter out = [Link]();
In JSP, we don’t need to do this because we already have an out that represents a
[Link] object.
2) request and 3) response
In Servlets, both request and response objects are passed in by the Servlet container to
the service() method of the [Link] class. Its signature is as follows:
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
In a Servlet, before we send any output, we are required to call the setContentType()
method of the HttpServletResponse to tell the browser the type of the content, as in the following
code:
[Link]("text/html");
In JSP, this is done automatically for us in the generated JSP servlet class.
Having an request object of [Link] class and an response
object of [Link] class, we can do anything we like as we would in
a Servlet.
demonstrates the use of out, request and response objects:
[Link]
<html>
<head>
<title>Sample Form</title>
</head>
<body>
<form action="[Link]">
<b>Enter your name: </b>
<input type="text" name="name">
<input type="submit" value="Submit"><br/>
</form>
</body>
</html>
[Link]
<html>
<head>
<title>Greetings Page</title>
</head>
<body>
<%
//use of 'request' object
String name = [Link]("name");
//use of 'out' object
[Link]("Welcome " + name + ". Have a nice day!" + "<br>");
%>
<form action="[Link]">
Click this for redirection to another page
<input type="submit" value="Go">
</form>
</body>
</html>
[Link]
<html>
<body>
<%
//use of 'response' object
[Link]("[Link]
%>
</body>
</html>
Session
In JSP we have been provided an implicit object session so we don’t need to create an
object of session explicitly as we do in Servlets. In JSP the session is by default true. The session
is defined inside the directive <%@ page session=”true/false” %>. If we don’t declare it inside the
JSP page then session will be available to the page, as it is default by true. This session object is
of type [Link] class.
We can tell the container to disable session in the JSP file by setting the session attribute
to false. Set the session attribute of the page directive to false, as shown in the following example:
<%@ page session="false" %>
The session implicit object represents the HttpSession object that we can retrieve in a
Servlet by calling the getSession() method of the well-known interface
[Link], as in the following code:
[Link]();
[Link]
<html>
<head>
<title>Sample Form</title>
</head>
<body>
<form action="[Link]">
<b>Enter your name: </b>
<input type="text" name="name">
<input type="submit" value="Submit"><br/>
</form>
</body>
</html>
[Link]
<html>
<body>
<%
String name = [Link]("name");
[Link]("Hello " + name + "!" + "<br>");
//use of 'session' object
[Link]("Session Id: " + [Link]() + "<br>");
[Link]("user", name);
%>
<br>
<a href="[Link]">Other JSP Page</a>
</body>
</html>
[Link]
<html>
<body>
<%
String name = (String)[Link]("user");
[Link]("Session Id: " + [Link]() + "<br>");
[Link]("Bye " + name + "!" + "<br>");
%>
</body>
</html>
5) config
The config implicit object represents a [Link] object that in a Servlet
can be retrieved by using the getServletConfig() method.
6) application
The application implicit object represents the [Link] object. In an
HttpServlet, we can retrieve the ServletContext method by using the getServletContext()
method.
7) page
The page implicit object represents the [Link] interface.
8) pageContext
The pageContext implicit object represents the [Link] object.
[Link]
<html>
<body>
<%
//testing of 'page' object
String page_name = [Link]();
[Link]("Page Name is: " + page_name + "<br>");
//testing of 'pageContext' object
[Link]("PNR", "JahnaviSreenidhi", pageContext.PAGE_SCOPE);
String name = (String)[Link]("PNR");
[Link]("Guru name is: " + name);
%>
</body>
</html>
Using JSP Code for JDBC
<%-- [Link] --%>
<%@ page import="[Link].*" %>
<html>
<body>
<table border="1">
<th>Roll</th><th>Name</th>
<%!
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String url = "jdbc:mysql://localhost:3306/test?useSSL=false&allowPublicKeyRetrieval=true";
String user = "root";
String pass = "system";
%>
<%
try {
[Link]("[Link]").newInstance();
con = [Link](url, user, pass);
if(con!=null) {
[Link]("Successfully connected to " + "MySQL server using TCP/IP..." + "<br>");
stmt = [Link]();
rs = [Link]("select * from students");
}
while ([Link]()) {
%>
<tr>
<td><%=[Link](1)%></td>
<td><%=[Link](2)%></td>
</tr>
<%
}
}
catch (Exception e) {
[Link]("Exception: " + [Link]());
}
finally {
try {
if (con != null) {
[Link]();
}
}
catch (SQLException e) { }
}
%>
</table>
</body>
</html>
Procedure to run the tomcat
C:\apache-tomcat-8.5.37\bin> [Link]
Using CATALINA_BASE: "C:\apache-tomcat-8.5.37"
Using CATALINA_HOME: "C:\apache-tomcat-8.5.37"
Using CATALINA_TMPDIR: "C:\apache-tomcat-8.5.37\temp"
Using JRE_HOME: "C:\Program Files\Java\jdk1.8.0_162"
Using CLASSPATH: "C:\apache-tomcat-8.5.37\bin\[Link];C:\apache-tomcat-
8.5.37\bin\[Link]"
Now we open a web browser (Google Chrome) to send the Web URL
([Link] to the Tomcat Server. The output is given below: