Sei sulla pagina 1di 12

Student Id: Student Name:

Academic year: 2019-20


Sem-In Examinations-I,
B. Tech. (CSE), 2018 Batch
II/IV, 4th Semester
18CS3210: ENTERPRISE PROGRAMMING KEY AND
SCHEME
Time: 2 hours Max. Marks: 50
(Assume any missing data suitably and design adequate hypothesis, if required)
Part-A (4X 3M=12M)
Answer ALL Questions
Q. No. 1, 2 from CO1 with BTL 2
Q. No 3, 4 from CO2 with BTL 3
1. Create an HTML page that contains a selection box with a list of 5 countries.
When the user selects a country, its capital should be printed next in the list.
Add CSS to customize the properties of the font of the capital (color,bold and font
size).
Scheme of Question-1:
-> for html structure: 1M
-> for body structure: 1M
-> for javascript function:1M
Solution:
<html>
<head>
<title>Sample Program</title>
<script>
function display()
{
var data = new Array("DELHI","KATHMANDU","Washington, D.C.");
var x = document.getElementById("country").selectedIndex;
document.getElementById("capital").value = data[x]
}
</script>
</head>
<body>
Select Country
<select name="country" id="country" onchange="display()">
<option>INDIA</option>
<option>NEPAL</option>
<option>USA</option>
</select>
Capital
<input type="text" id="capital">
</body>
</html>
2. List out the types of JDBC drivers with neat diagram.
Scheme of Question-2:
-> for Types of drivers:2M
-> for diagram : 1M
Answer:
In JDBC, we have 4 types of drivers:
i) Type-1 driver (JDBC-ODBC Bridge Driver)
diagram:

ii) Type-2 driver( Native API Driver )


diagram:

iii) Type-3 driver ( Network Protocol Driver ):


diagram:
iv) Type-4 driver ( Thin Driver ) :
diagram:

3. Illustrate about Servlet-Collaboration. and write the syntax for the following:
i) how to store String object into request scope.
ii) how can we get RequestDispatcher object
iii) how can we get String object from request scope
Scheme of Evaluation:
-> syntax for i : 1M
-> syntax for ii : 1M
-> syntax for iii: 1M
Solution:
Defintion:
sending a request from one servlet to another servlet is nothing but Servlet
Collaboration.
i) String str = "Welcome";
request.setAttribute("x",str);
ii) RequestDispatcher rd = request.getRequestDispatcher("path of resource");
iii) String obj = (String)request.getAttribute("key");
4. Illustrate about Jsp expressions with example.
Scheme of Evalution:
-> syntax for jsp expressions : 1M
-> for example : 2M
Solution:
syntax:
<%=... %>

-> These are used at outside scriptlet.


-> These are used to send data to the browser.so they are equivalent to JspWriter object.
-> By using these we can access scriptlet data from outside scriptlet

ex:- :
<%
int x = 10;
out.println("value of x from scriptlet:" + x);
%>
<br>
Value of x from template text:<%=x %>
Part-B (4 X 5M=20M)
Answer ALL Questions
Q. No. 5, 6 from CO1 with BTL 2
Q. No 7, 8 from CO2 with BTL 3
5. i) Example a javascript function to convert given lowercase text in textfield into upper
case when we click the button.
ii) Example a javascript function to apply random color to the background of the body
where user clicks the button
Scheme of Evaluation:
-> for change of lowercase text into upper case : 2.5M
-> for change of background random color: 2.5M
Solution:
i)
<html>
<head>
<title>Sample Program</title>
<script>
function change()
{
document.getElementById("txt").value =
document.getElementById("txt").value.toUpperCase();
}
</script>
</head>
<body>
Enter Input:<input type="text" id="txt"><br><br>
<button onclick="change()">Change</button>
</body>
</html>
ii)
<html>
<head>
<title>Sample Program</title>
<script>
function change()
{
/*
var a = Math.floor((Math.random() * 255) + 0);
var b = Math.floor((Math.random() * 255) + 0);
var c = Math.floor((Math.random() * 255) + 0);
document.body.style.backgroundColor = "rgb("+a+","+b+","+c+")"
*/
// or
var clr = document.bgColor=parseInt(Math.random()*999999);
document.color.value=clr;
}
</script>
</head>
<body>
<button onclick="change()">Change Background Color</button>
</body>
</html>
6. Build a JDBC application to display the description of a Employee table using
ResultSetMetaData as shown below:
Name Type
-------- --------
EmpId Number
EmpName Varchar2
EmpDesig Varchar2
EmpSal Number
Scheme of Evaluation:
-> For connection establishment: 2M
-> For statement and resultset and resultsetmetadata objects: 1M
-> for displaying description: 2M
Solution:
import java.sql.*;

class ResultSetMetaDataDemo
{
public static void main(String[] args)throws Exception
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","hai");

String vsql = "select * from employee";

Statement stmt = con.createStatement();


ResultSet rs = stmt.executeQuery(vsql);
ResultSetMetaData rsmd = rs.getMetaData();
int ccount = rsmd.getColumnCount();
System.out.println("total cols:" + ccount);
System.out.println("Name" + "\t" + "Type");
System.out.println("----" + "\t" + "----");

for(int i=1;i<=ccount;i++){
System.out.println(rsmd.getColumnName(i) + "\t" +
rsmd.getColumnTypeName(i));
}
con.close();
}
}
7. Develop a servlet application to exaplain about RequestDispatcher interface.
Scheme of Evaluation:
-> Definition of RequestDispatcher : 2M
-> for Servlet application : 5M
-> for web.xml : 2M
Solution:
Definition:
When multiple servlets are involved in processing single request, we can use
RequestDispatcher interface.
Request dispatcher contains 2 methods. They are
i) include(request,response) ii) forward(request,response)
RDOne.java:
-----------------
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class RDOne extends HttpServlet


{
public void service(HttpServletRequest req,HttpServletResponse res)throws
ServletException,IOException{
System.out.println("rdone service called...");
PrintWriter out = res.getWriter();
res.setContentType("text/html");

//retrieve RequestDispatcher object to call RDTwo


RequestDispatcher rd = req.getRequestDispatcher("/rdtwo");

out.println("<h1>Output from RDOne</h1>");


rd.forward(req,res);
out.println("<h1>Last line of Output from RDOne</h1>");
}
}
RDTwo.java:
------------------
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class RDTwo extends HttpServlet


{
public void service(HttpServletRequest req,HttpServletResponse res)throws
ServletException,IOException{
System.out.println("rdtwo service called...");
PrintWriter out = res.getWriter();
res.setContentType("text/html");

out.println("<h1>Output from RDTwo</h1>");


}
}
Web.xml:
<web-app>
<servlet>
<servlet-name>rdone</servlet-name>
<servlet-class>RDOne</servlet-class>
</servlet>
<servlet>
<servlet-name>rdtwo</servlet-name>
<servlet-class>RDTwo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>rdone</servlet-name>
<url-pattern>/rdone</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>rdtwo</servlet-name>
<url-pattern>/rdtwo</url-pattern>
</servlet-mapping>
</web-app>
8. Implement a Jsp application to explain about the following:
i) Jsp declarations
ii) Error handling
Scheme of Evaluation:
-> for JSP declarations : 3M
-> for Error Handling : 2M
Solution:
i) Jsp Declarations:
syntax:
<%!
statements;
%>
-> these are stored outside _jspService() method developed by jsp compiler.
-> so we can't use implicit variables directly as part of this.
-> But we can use implicit variables by passing as argument for any method which is
available in declarations from scriptlet.

Note: As part of declarations,we can declare any type of data.

ex:- 1: (test.jsp)
<%!
int x = 10;
static int y = 20;
public void mone(JspWriter out)throws Exception{
out.println("<br>inside mone()");
}
public static void mtwo(){
System.out.println("inside mtwo()");
}
%>
<%
int z = 30;
out.println("Value of x :" + x);
out.println("<br>Value of y:" + y);
out.println("<br>Value of z:" + z);
mone(out);
mtwo();
%>

ii) Error Handling:


We can implement a separate setup pages to handle the errors.These pages are
called as error pages.

->As part of the error page,A variable 'exception' can be used without declaring.This
variable is called as implicit variable.

code:- (eh.jsp)
<%@page isErrorPage="true" %>
Our application failed due to:
<%
out.println(exception);
%>
<br>
Please send error info to admin @abc.com.

code:- (at.jsp)
<%@page isErrorPage="false" %>
<%@page errorPage="eh.jsp" %>
Trying to copy...
<%
java.io.FileInputStream fis = new java.io.FileInputStream("one.txt");
//code to open two.text
//code to copy info from one.txt to two.txt.
%>
Part-C (2 X 9M=18M)
Answer ALL Questions
Q. No. 9,10 from CO1 with BTL 2
Q. No. 11,12 from CO2 with BTL 3
9. Develop and demonstrate JavaScript with POP-UP boxes and functions for the
following problems:
a)Input: Click on Display Date button using onclick( ) function
Output: Display datein the textbox
b)Input: A number n obtained using prompt
Output: A multiplication tableof numbers from 1 to 10 of n using alert
c)Input:A number n obtained using prompt and add another number using confirm
Output: Sum of the entire n numbers using alert
Scheme of Evaluation:
-> for onclick evevnt : 3M
-> for multiplication table: 3M
-> for sum of n numbers: 3M
Solution:
9 a)
<html>
<head>
<title>Sample Program</title>
<script>
function display()
{
var d = new Date();
document.getElementById("date").value = d;
}
</script>
</head>
<body>
<button onclick="display()">Click Here</button><br><br>
Date : <input type="text" id="date">
</body>
</html>
9 b):
<script>
var n = parseInt(window.prompt("Enter Range",5));
for(i=1;i<=10;i++)
{
var output = n+"*"+i+"="+(n*i)
alert(output)
}
</script>
9 c):
<script>
var n = parseInt(window.prompt("Enter Range",5));
var arr = new Array();
for(i=0;i<n;i++)
{
arr[i]= parseInt(window.prompt("Enter " +(i+1)+" value"))
}
sum=0;
if(confirm("Sum of Array Values"))
{
for(i=0;i<n;i++)
{
sum = sum+arr[i];
}
alert(sum);
}
</script>

(Or)
10. Build a JDBC application for the following:
i) storing image into DB
ii) retrieving image from DB
Scheme of Evaluation:
i) Storing IMAGE:
-> for connection establishment: 2M
-> for Storing Image into DB : 2.5M
ii) Retreiving IMAGE:
-> for connection establishment: 2M
-> for retrieving Image from DB: 2.5M
Solution:
i)
import java.sql.*;
import java.io.*;

class ImageStoreDBDemo
{
public static void main(String[] args)throws Exception
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","tiger");

String vsql = "insert into imagetable values(?,?,?)";

PreparedStatement pstmt = con.prepareStatement(vsql);

pstmt.setInt(1,1001);
pstmt.setString(2,"Flower1");

//read image from local system


FileInputStream fis = new FileInputStream("flower1.jpg");

pstmt.setBinaryStream(3,fis,fis.available());

int n = pstmt.executeUpdate();

if( n > 0 )
System.out.println("Image stored successfully");

fis.close();

con.close();
}
}
ii)
import java.sql.*;
import java.io.*;

class ImageRetrieveDBDemo
{
public static void main(String[] args)throws Exception
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","tiger");

String vsql = "select * from imagetable";

PreparedStatement pstmt = con.prepareStatement(vsql);

ResultSet rs = pstmt.executeQuery();

if( rs.next() ){
Blob b = rs.getBlob(3);
FileOutputStream fout = new FileOutputStream("1.jpg");
byte barr[] = b.getBytes(1,(int)b.length());
fout.write(barr);
fout.close();
}
con.close();
}
}
11. Differentiate between forward() and sendRedirect(). And develop a servlet application to
explain about response.sendRedirect() method.
Scheme of Evaluation:
-> for difference between forward() and sendRedirect(): 3M
-> for servlet application: 6M
Solution:
Forward() sendRedirect()
The forward() method works at server The sendRedirect() method works at
side. client side.
It sends the same request and It always sends a new request.
response objects to another servlet.
It can work within the server only. It can be used within and outside the
server.

DemoServlet.java:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class DemoServlet extends HttpServlet{


public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter pw=res.getWriter();

response.sendRedirect("https://www.google.com");

pw.close();
}
}
Web.xml:
<web-app>
<servlet>
<servlet-name>demo</servlet-name>
<servlet-class>DemoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>demo</servlet-name>
<url-pattern>/demo</url-pattern>
</servlet-mapping>
</web-app>
(Or)
12. Develop a jsp application to retrieve Product details from database and display all the records
on browser within table tag in center of the webpage.
Scheme of Evaluation:
-> for connection establishment : 3M
-> for getting Statement and ResultSet object: 2M
-> for displaying product data on Browser in table format: 4M
Solution:
retrieveJsp.jsp:
<%@page import="java.sql.*" %>
<%!
Connection con;
Statement stmt;
ResultSet rs;
%>
<%
//connection establishment
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","tiger");
String vsql = "select * from product";
stmt = con.createStatement();
rs = stmt.executeQuery(vsql);
%>
<div style="position:absolute;left:500px;top:150px;">
<table border=1>
<tr>
<th colspan=3>PRODUCT TABLE DATA</th>
</tr>
<tr>
<td><b>PID</b></td>
<td><b>PNAME</b></td>
<td><b>PRICE</b></td>
</tr>
<%
while( rs.next() ){
%>
<tr>
<td><%=rs.getInt(1) %></td>
<td><%=rs.getString(2) %></td>
<td><%=rs.getDouble(3) %></td>
</tr>
<%
}
%>
</table></div>
<%
}catch(Exception e){
try{
con.close();
}catch(Exception e1){}
}

Potrebbero piacerti anche