0% found this document useful (0 votes)
321 views7 pages

WTL Assignment No. 6

The document provides instructions for an assignment to create a database table using MySQL or Oracle and display the table contents using JSP. It includes: 1) Creating a "students_info" table with fields for student ID, name, class, division and city. 2) Using JSP tags like scriptlets to connect to the database and execute SQL queries to retrieve data. 3) An overview of the JSP lifecycle and how JSP pages are translated into servlets.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
321 views7 pages

WTL Assignment No. 6

The document provides instructions for an assignment to create a database table using MySQL or Oracle and display the table contents using JSP. It includes: 1) Creating a "students_info" table with fields for student ID, name, class, division and city. 2) Using JSP tags like scriptlets to connect to the database and execute SQL queries to retrieve data. 3) An overview of the JSP lifecycle and how JSP pages are translated into servlets.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Assignment No.

: 06

o Title:
Implement the program demonstrating the use of JSP. e.g., Create a database table
students_info (stud_id, stud_name, class, division, city) using database like
Oracle/MySQL etc. and display (use SQL select query) the table content using JSP.
o Date of performance:
o Date of submission:
o Sign:
Assignment No.06

• Aim:
Implement the program demonstrating the use of JSP. e.g., Create a database
table students_info (stud_id, stud_name, class, division, city) using database
like Oracle/MySQL etc. and display (use SQL select query) the table content
using JSP.

• Hardware/Software Requirements:
Software:
1. Any Operating System
2. JDK 7 or later
3. Editors : Eclipse
4. Web browser

• Theory:

❖ JSP :

JSP technology is used to create web application just like Servlet


technology. It can be thought of as an extension to Servlet because it
provides more functionality than servlet such as expression language,
JSTL, etc.

A JSP page consists of HTML tags and JSP tags. The JSP pages are
easier to maintain than Servlet because we can separate designing and
development. It provides some additional features such as Expression
Language, Custom Tags, etc.
• Advantages :
1. Extension to servlet
2. Easy to maintain
3. Fast Development
4. No need to recompile and redeploy
5. Less code that Servlet

• The Lifecycle of a JSP Page


The JSP pages follow these phases:

o Translation of JSP Page


o Compilation of JSP Page
o Classloading (the classloader loads class file)
o Instantiation (Object of the Generated Servlet is created).
o Initialization ( the container invokes jspInit() method).
o Request processing ( the container invokes _jspService() method).
o Destroy ( the container invokes jspDestroy() method).

Note: jspInit(), _jspService() and jspDestroy() are the life cycle methods of JSP.
As depicted in the above diagram, JSP page is translated into Servlet by the help of
JSP translator. The JSP translator is a part of the web server which is responsible
for translating the JSP page into Servlet. After that, Servlet page is compiled by the
compiler and gets converted into the class file. Moreover, all the processes that
happen in Servlet are performed on JSP later like initialization, committing
response to the browser and destroy.

• Creating a simple JSP Page


To create the first JSP page, write some HTML code as given below, and save it by
.jsp extension. We have saved this file as index.jsp. Put it in a folder and paste the
folder in the web-apps directory in apache tomcat to run the JSP page.

index.jsp
Let's see the simple example of JSP where we are using the scriptlet tag to put Java
code in the JSP page. We will learn scriptlet tag later.

<html>

<body>

<% out.print(2*5); %>


</body>
</html>

• JSP Scripting Element

JSP Scripting element are written inside <% %> tags. These code inside <% %> tags
are processed by the JSP engine during translation of the JSP page. Any other text in
the JSP page is considered as HTML code or plain text.

There are five different types of scripting elements

Scripting Element Example

Comment <%-- comment --%>

Directive <%@ directive %>

Declaration <%! declarations %>

Scriptlet <% scriplets %>


Expression <%= expression %>

❖ Java Database Connectivity


1. Register the driver class
The forName() method of Class class is used to register the driver class.
This method is used to dynamically load the driver class.

Example,

Class.forName("oracle.jdbc.driver.OracleDriver");

2. Create the connection object


The getConnection() method of DriverManager class is used to establish
connection with the database.

Example,

Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","password");

3. Create the Statement object


The createStatement() method of Connection interface is used to create
statement. The object of statement is responsible to execute queries with the
database.

Example,
Statement stmt=con.createStatement();

4. Execute the query


The executeQuery() method of Statement interface is used to execute queries
to the database. This method returns the object of ResultSet that can be used
to get all the records of a table.

Example,

ResultSet rs=stmt.executeQuery("select * from emp");

while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}

5. Close the connection object


By closing connection object statement and ResultSet will be closed
automatically. The close() method of Connection interface is used to close
the connection.

Example,
con.close();

• Conclusion:
Thus, we have implemented the program demonstrating the use of JSP.

You might also like