ZeePedia buy college essays online


Web Design and Development

<<< Previous Java Server Pages 3 Next >>>
 
img
Web Design & Development ­ CS506
VU
request, _jspService() method is invoked
264
img
Web Design & Development ­ CS506
VU
<h3> <input type="radio" name = "page" value="web"/>
Web Design & Develoment
</h3> <br>
<h3> <input type="radio" name = "page" value="java"/>Java
</h3><br>
<input type="submit" value="Submit" />
</form>
</body>
</html>
controller.jsp
Based upon the selection made by the user, this page will redirect the user to respective pages. Those are
web.jspand java.jsp
<html>
<body>
<!-- scriptlet -->
<%
// reading parameter named page
String pageName = request.getParameter("page");
// redirecting user based on selection made
if (pageName.equals("web"))
{
response.sendRedirect("web.jsp");
}
else if (pageName.equals("java") )
{
response.sendRedirect("java.jsp");
}
%>
</body>
</html>
web.jsp
This page is used to display course outline of "web design and development" in a tabular format after
reading them from database. The code is:
// importing java.sql package using page directive, to work with database
<%@page import="java.sql.*"%>
<html>
<body>
<center>
<h2> Welcome to Web Design & Development Page </h2>
<h3> Course Outline</h3>
<TABLE BORDER="1" >
<TR>
<TH>Session No.</TH>
265
img
Web Design & Development ­ CS506
VU
<TH>Topics</TH>
<TH>Assignments</TH>
</TR>
<%-- start of scriptlet --%>
<% // establishing conection
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String conUrl = "jdbc:odbc:CourseDSN";
Connection con = DriverManager.getConnection(conUrl);
// preparing query using join statements
String sql = " SELECT sessionNo, topic, assignment " +"
FROM Course, SessionDetail" +" WHERE courseName = ? " + "
AND Course.courseId = SessionDetail.courseID";
PreparedStatement pStmt = con.prepareStatement(sql);
// setting parameter value "web".pStmt.setString( 1 , "web");
ResultSet rs = pStmt.executeQuery();
String sessionNo;
String topic;
String assignment;
// iterating over resultset
while (rs.next()) {
sessionNo = rs.getString("sessionNo");
topic = rs.getString("topic");
assignment = rs.getString("assignment");
if (assignment == null){
assignment = "";
}
%>
<%-- end of scriptlet --%>
<%-- The values are displayed in tabular format usingexpressions,
however it can also be done usingout.println(sessionNo) like statements
--%>
<TR>
<TD> <%=sessionNo%> </TD>
<TD> <%=topic%> </TD>
<TD> <%=assignment%> </TD>
</TR>
<%
} // end while
%>
</TABLE >
266
img
Web Design & Development ­ CS506
VU
</center>
</body>
</html>
java.jsp
The code of this page is very much alike of "web.jsp". The only change is in making of query. Here the
value is set "java" instead of "web"
// importing java.sql package using page directive, to work with database
<%@page import="java.sql.*"%>
<html>
<body>
<center>
<h2> Welcome to Java Page </h2>
<h3> Course Outline</h3>
<TABLE BORDER="1" >
<TR>
<TH>Session No.</TH>
<TH>Topics</TH>
<TH>Assignments</TH>
</TR>
<%-- start of scriptlet --%>
<%
// establishing connection
Calss.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String conUrl = "jdbc:odbc:CourseDSN";
Connection con = DriverManager.getConnection(conUrl);
// preparing query using join statements
String sql = " SELECT sessionNo, topic, assignment " +"
FROM Course, SessionDetail" +" WHERE courseName = ? " + "
AND Course.courseId = SessionDetail.courseID";
PreparedStatement pStmt = con.prepareStatement(sql);
// setting parameter value "web".pStmt.setString( 1 , "java");
ResultSet rs = pStmt.executeQuery();
String sessionNo;
String topic;
String assignment;
// iterating over resultset
while (rs.next())
{
sessionNo = rs.getString("sessionNo");
topic = rs.getString("topic");
assignment = rs.getString("assignment");
if (assignment == null)
{
assignment = "";
}
%>
<%-- end of scriptlet --%>
<%-- The values are displayed in tabular format usingexpressions, however it can also be done
usingout.println(sessionNo) like statements
--%>
267
img
Web Design & Development ­ CS506
VU
<TR>
<TD> <%=sessionNo%> </TD>
<TD> <%=topic%> </TD>
<TD> <%=assignment%> </TD>
</TR>
<%
} // end while
%>
</TABLE >
</center>
</body>
</html>
Issues with Last Example
Too much cluttered code in web.jsp and java.jsp. This makes it very difficult to understand (probably you
experienced it by yourself) and to make changes/enhancements.
A single page is doing everything that is really a bad approach while making of web applications. The tasks
performed by web.jspor java.jspare:
Displaying contents (Presentation logic)
Connecting with database (DB connectivity logic)
Results Processing (Business Logic)
Can we simplify it? Yes, the answer lies in the use of JavaBeans technology.
JavaBeans
A java class that can be easily reused and composed together in an application. Any java class that follows
certain design conventions can be a JavaBean.
JavaBeans Design Conventions
These conventions are:
A bean class must have a zero argument constructor
A bean class should not have any public instance variables/attributes (fields)
Private values should be accessed through setters/getters
For boolean data types, use boolean isXXX( ) & setXXX(boolean)
A bean class must be serializable
A Sample JavaBean
The code snippet of very basic JavaBean is given below that satisfies all the conventions described above.
The MyBean.java class has only one instance variable.
public class MyBean implements Serializable { private String name; // zero argument constructor
public MyBean( ){
name = "";
}
// standard setter
public void setName(String n) {
name = n;
}
// standard getter
public String getName( ) {
return name;
}
// any other method
268
img
Web Design & Development ­ CS506
VU
public void print( ) {
System.out.println("Name is: " + name);
}
} // end Bean class
Example Code: Displaying course outline by incorporating JavaBeans
This example is made by making more enhancements to the last one. Two JavaBeans are included in this
example code. These are CourseOutlineBean& CourseDAO.
The CourseOutlineBean is used to represent one row of the table. It contains the following attributes:
sessionNo
topic
assignment
The CourseDAO (where DAO stands of Data Acess Object) bean encapsulates database connectivity and
result processing logic.
The web.jsp and java.jsp will use both these JavaBeans. The code of these and the JSPs used in this
example are given below.
CourseOutlineBean.java
package vu;
import java.io.*;
public class CourseOutlineBean implements Serializable{
private int sessionNo;
private String topic;
private String assignment;
// no argument constructor
public CourseOutlineBean() {
sessionNo = 0;
topic = "";
assignment = "";
}
// setters
public void setSessionNo(int s){
sessionNo = s;
}
public void setTopic(String t){
topic = t;
}
public void setAssignment(String a){
assignment = a;
}
// getterspublic
int getSessionNo( ){
return sessionNo;
}
public String getTopic( ){
return topic;
}
public String getAssignment( ){
return assignment;
}
} // end class
269
img
Web Design & Development ­ CS506
VU
CourseDAO.java
package vu;
import java.io.*;
import java.sql.*;
import java.util.*;
public class CourseDAO implements Serializable{
private Connection con;
public CourseDAO() {
establishConnection();
}
//********** establishConnection method ************* method used to make connection with
databaseprivate
void establishConnection(){
try{
// establishing conection
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String conUrl = "jdbc:odbc:CourseDSN";
con = DriverManager.getConnection(conUrl);
}catch(Exception ex){
System.out.println(ex);
}
}
//*********** retrieveCourseList method ********************
public ArrayList retrieveCourseList(String cName){
ArrayList courseList = new ArrayList();
try{
String sql = " SELECT sessionNo, topic, assignment " +"
FROM Course, SessionDetail" +"
WHERE courseName = ? " + "
AND Course.courseId = SessionDetail.courseID ";
PreparedStatement pStmt = con.prepareStatement(sql);
pStmt.setString(1, cName);
ResultSet rs = pStmt.executeQuery();
int sNo;
String topic;
String assignment;
while ( rs.next() ) {
sNo = rs.getInt("sessionNo");
topic = rs.getString("topic");
assignment = rs.getString("assignment");
if (assignment == null)
{
assignment = "";
}
// creating a CourseOutlineBean object
CourseOutlineBean cBean = new CourseOutlineBean();
cBean.setSessionNo(sNo);
cBean.setTopic(topic);cBean.setAssignment(assignment);
// adding a bean to arraylist
courseList.add(cBean);
}
}catch(Exception ex){
System.out.println(ex);
270
img
Web Design & Development ­ CS506
VU
} finally {
// to close connection
releaseResources();
}
// returning ArrayList object
return courseList;
} // end retrieveCourseOutline
//********** releaseResources method ********************
private void releaseResources(){
try{
if(con != null){
con.close();
}
}catch(Exception ex){
System.out.println();
}
} // end releaseResources
}//end CourseDAO
index.jsp
This page is used to display the course options to the user in the radio button form.
<html>
<body>
<h2>Select the page you want to visit</h2>
<form name="myForm" action="controller.jsp" >
<h3> <input type="radio" name = "page" value="web"/>
Web Design & Develoment
</h3> <br>
<h3> <input type="radio" name = "page" value="java"/>Java
</h3><br>
<input type="submit" value="Submit" />
</form>
</body>
</html>
controller.jsp
Based on user selection, redirects the user to desired page.
<html>
<body>
<!-- scriptlet -->
<%
String pageName = request.getParameter("page");
if (pageName.equals("web")) {
response.sendRedirect("web.jsp");
} else if (pageName.equals("java") )
{
response.sendRedirect("java.jsp");
}
%>
</body>
</html>
web.jsp
This page is used to display course outline of "web design and development" in a tabular format after
reading them from database. Moreover, this page also uses the JavaBeans (CourseOutlineBean &
CourseDAO).
271
img
Web Design & Development ­ CS506
VU
<%@page import="java.util.*" %>
<%-- importing vu package that contains the JavaBeans--%>
<%@page import="vu.*" %>
<html>
<body>
<center>
<h2> Welcome to Web Design & Development Course </h2>
<h3> Course Outline</h3>
<TABLE BORDER="1" >
<TR>
<TH>Session No.</TH>
<TH>Topics</TH>
<TH>Assignments</TH>
</TR>
<%-- start of scriptlet --%>
<%
// creating CourseDAO object
CourseDAO courseDAO = new CourseDAO();
// calling retrieveCourseList() of CourseDAO class and
// passing "web" as value. This method returns ArrayListArrayList
courseList = courseDAO.retrieveCourseList("web");
CourseOutlineBean webBean = null;
// iterating over ArrayList to display course outline
for(int i=0; i<courseList.size(); i++){
webBean = (CourseOutlineBean)courseList.get(i);
%>
<%-- end of scriptlet --%>
<TR>
<TD> <%= webBean.getSessionNo()%> </TD>
<TD> <%= webBean.getTopic()%> </TD>
<TD> <%= webBean.getAssignment()%> </TD>
</TR>
<%
} // end for
%>
</TABLE >
</center>
</body>
</html>
java.jsp
The code contains by this page is almost same of web.jsp. Here, "java" is passed to retieveCourseList( )
method. This is shown in boldface.
<%@page import="java.util.*" %>
<%-- importing vu package that contains the JavaBeans--%><%@page import="vu.*" %>
<html>
<body>
<center>
<h2> Welcome to Java Course </h2>
<h3> Course Outline</h3>
<TABLE BORDER="1" >
<TR>
<TH>Session No.</TH>
<TH>Topics</TH>
272
img
Web Design & Development ­ CS506
VU
<TH>Assignments</TH>
</TR>
<%-- start of scriptlet --%>
<%
// creating CourseDAO object
CourseDAO courseDAO = new CourseDAO();
// calling retrieveCourseList() of CourseDAO class and
// passing "java" as value. This method returns ArrayList
ArrayList courseList = courseDAO.retrieveCourseList("java");
CourseOutlineBean javaBean = null;
// iterating over ArrayList to display course outline
for(int i=0; i<courseList.size(); i++){
javaBean = (CourseOutlineBean)courseList.get(i);
%>
<%-- end of scriptlet --%>
<TR>
<TD> <%= javaBean.getSessionNo()%> </TD>
<TD> <%= javaBean.getTopic()%> </TD>
<TD> <%= javaBean.getAssignment()%> </TD>
</TR>
<%
} // end for
%>
</TABLE >
</center>
</body>
</html>
References:
Entire material for this handout is taken from the book JAVA A Lab Course by Umair Javed.
This material is available just for the use of VU students of the course Web Design and
Development and not for any other commercial purpose without the consent of author.
273