Set 21 – Servlets, JSP, JSTL Interview Questions

1. Choose the right option to fill the blanks to get the values entered by the client as per the following URL.
a) URL when client clicks the submit button

http://localhost:8080/DemoServlet?t1=Lux&t2=5

b) Servlet snippet

import javax.servlet.*;	import javax.servlet.http.*;  import java.io.*;
public class DemoServlet extends HttpServlet
{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException
    {
        PrintWriter out = response.getWriter();
        ----------------------------------------   // blank
        ----------------------------------------   // blank
        out.println(valueEntered1 + ", " + valueEntered2);
        out.close(); 
    }
} 

1. String valueEntered1 = request.getParameter(“Lux”);
String valueEntered2 = request.getParameter(“5”);

2. String valueEntered1 = request.getParameter(“?t1”);
String valueEntered2 = request.getParameter(“?t2”);

3. String valueEntered1 = request.getParameter(“t1”);
String valueEntered2 = request.getParameter(“t2”);

4. String valueEntered1 = request.getParameter(“field1”);
String valueEntered2 = request.getParameter(“field2”);

2. Choose the right option to fill the blank to get the values entered by the client as per the following URL.

a) URL when client clicks the submit button

http://localhost:8080/DemoServlet?t1=Lux&t2=5

b) Servlet snippet

import javax.servlet.*;	import javax.servlet.http.*;  import java.io.*;
public class DemoServlet extends HttpServlet
{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, java.io.IOException
    {
        java.io.PrintWriter out = response.getWriter();
        ------------------------------------    // blank

        Object fieldObject = e.nextElement();        
        String fieldValue = request.getParameter((String) fieldObject);
        out.println("
Value you entered: " + fieldValue); Object fieldObject1 = e.nextElement(); String fieldValue1 = request.getParameter((String) fieldObject1); out.println("
Value you entered: " + fieldValue1); } }

1. java.util.Enumeration e = request.getParameterValues();
2. java.util.Enumeration e = request.getParameterFields();
3. java.util.Enumeration e = request.getParameterNames();
4. java.util.Enumeration e = request.getNames();

3. Choose all the right options that are true for the following snippet of code.

a) HTML snippet that calls the following servlet:


Enter Password

b) Following is the servlet snippet:

import javax.servlet.*;	  import javax.servlet.http.*;    import java.io.*;
public class DemoServlet extends HttpServlet
{
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException
    {
         response.setContentType("text/html");
         PrintWriter out = response.getWriter();
         if(request.getParameter("password").equals("tcs"))
         {
             out.println("Following are Employee records
"); out.println(" "); out.println(""); out.println(""); out.println(""); out.println("
100, S N Rao, 5500.00
101, Jyostna, 6500.00
103, Srinivas, 7500.00
"); } else { out.println("Your password is " + request.getParameter("password") + " and is invalid"); } out.close(); } }

1. above code sends records to the client if password is valid
2. code contains errors and servlet does not compile
3. HTML code cannot be embedded in a servlet
4. code does not contain errors

4. Embed the HTML code in servlet sending code to client changing background color of browser to red. Choose all the right options that can fill the blanks.

a) HTML snippet

Enter Login Password

b) Servlet snippet

import javax.servlet.*;	import javax.servlet.http.*;    import java.io.*;
public class DemoServlet extends HttpServlet
{
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException
    {
         PrintWriter out = response.getWriter();
         if(request.getParameter("password").equals("tcs"))
         {
             out.println("Password is valid");
         }
         else
         {
             ---------------------------------    // blank 1
             ---------------------------------    // blank 2
        }
         out.close();
    }
}

1. out.println(“<html> <body bgcolor=”red”> <h3>Password is Invalid”);
out.println(“</h3> </body> </html>”);

2. out.println(“<html> <body bgcolor=red>” + <h3>”Password is Invalid”);
out.println(“</h3> </body> </html>”);

3. out.println(“<html>” + “<body bgcolor=red>” + “<h3>Password is Invalid”);
out.println(“</h3> </body> </html>”);

4. out.println(“<html> <body bgcolor=red> <h3>Password is Invalid”);
out.println(“</h3> </body> </html>”);

5. What could be the blank in the following snippet of servlet code to make it a life cycle method? Choose the right option.

import javax.servlet.*;         import javax.servlet.http.*;       import java.io.*;
public class DemoServlet extends HttpServlet
{
    ---------------------------------         // blank of code here

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException
    {
        // business logic 
    }
    public void destroy()
    {
        // cleanup code here
    }
}  

1. public void init(ServletConfig config) {
// initialization values for servlet
}

2. public void init(ServletContext context) {
// initialization values for servlet
}

3. public void init(Servlet servlet) {
// initialization values for servlet
}

4. public void init(HttpSession session) {
// initialization values for servlet
}

6. What could be the URL that can map exactly to the getParameter() methods. Choose all the right options.

import javax.servlet.*;	import javax.servlet.http.*;    import java.io.*;
public class DemoServlet extends HttpServlet
{
  public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String str1 = request.getParameter("user");   
    String str2 = request.getParameter("pass"); 
    out.println("Your user name is " + str1 + "
and password is "+ str2); out.close(); } }

1. http://lorvent:8080/jasmine?flower=user&plant=pass
2. http://lorvent:8080/jasmine?user=flower&pass=plant
3. http://lorvent:8080/jasmine?user=user&pass=pass
4. http://lorvent:8080/jasmine?flower&plant=user&pass

7. Choose all the right options that are true for the following snippet code.

a) HTML snippet

Enter Login Password

b) Servlet snippet

import javax.servlet.*;	import javax.servlet.http.*;    import java.io.*;
public class Validate extends HttpServlet
{
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException
  {
   response.setContentType("text/html");
   PrintWriter out = response.getWriter();
   if(request.getParameter("password").equals("tcs"))
   {
     out.println("Password is valid");
   }
   else
   {
     out.println(" 

Login V alidation

"); out.println("
"); out.println("Enter User Name
"); out.println("Enter Password
"); out.println(""); out.println("
"); } out.close(); } }

1. above code sends a form to client created dynamically
2. code does not have errors
3. code contains errors and thereby form is not sent to client
4. html code cannot be put inside a service() method

8. Choose the right option that is true for the following servlet.

a) web.xml entry for DemoServlet


        abc
        DemoServlet
    

    
        abc
        /jasmine/*
    

b) Servlet code

import javax.servlet.*;    import javax.servlet.http.*;      import java.io.*;  import java.util.*;
public class DemoServlet extends HttpServlet
{
    public void service(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException
    {
        PrintWriter out = response.getWriter();
        String name = getServletName();
        out.println(name);
        out.close();
     }
}  

1. abc
2. DemoServlet
3. jasmine
4. does not compile as getServletName() is called without object

9. Read the snippets carefully.

a) HTML snippet that invokes the following servlet

 
     
Enter First Number
Enter Second Number

b) Servlet snippet

import javax.servlet.*;	     import javax.servlet.http.*;      import java.io.*;
public class Validate extends HttpServlet
{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException
    {	
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      double fn = Double.parseDouble(request.getParameter("firstNumber"));
      double sn = Double.parseDouble(request.getParameter("secondNumber"));
      ---------------------------------------   // blank
      out.close();  			
    }
}  

Create a dynamic VIEW to send values and product of two numbers to client. Choose all the right options that are capable to fill the blank.

1. out.println(“<body> <h3>”);
out.println(“<br>Your first number: ” + fn);
out.println(“<br>Your second number: ” + sn);
out.println(“<br>Product is ” + fn*sn);
out.println(“</h3> </body>”);

2. out.println(“<body> <h3>”);
out.println(“Your first number: ” + fn + “<br>Your second number: ” + sn);
out.println(“<br>Product is ” + fn*sn);
out.println(“</h3> </body>”);

3. out.println(“<body> <h3>”);
out.println(“<br>Your first number: ” + fn);
out.println(“<br>Your second number: ” + sn);
out.println(“<br>Product is ” + fn*sn + “</h3> </body>”);

4. none of above

10. Read the snippets carefully.

a) HTML snippet

Enter Login Password

b) Servlet snippet

import javax.servlet.*;	import javax.servlet.http.*;    import java.io.*;
public class Validate extends HttpServlet
{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException
    {	
        PrintWriter out = response.getWriter();
        String password = request.getParameter("password");      
        if(! password.equals("tcs")) 
        {
          ---------------  // blank here, choose the all right options that can
        }        	   // send error message to client
        else
        {
          out.println("You are permitted to access the database");
        }
        out.close();
    }
}  

1. response.sendError(407, “Requires Authentication. Failed to Login”);
2. response.sendError(407);
3. response.redirectError(407, “Requires Authentication. Failed to Login”);
4. response.redirectError(407);

11. Choose all the right options that can fill the blank to compile successfully.

a) HTML snippet

b) Servlet

import javax.servlet.*;	import javax.servlet.http.*;    import java.io.*;
public class Servlet1 extends HttpServlet
{
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException
    {
        RequestDispatcher rd = request.getRequestDispatcher("Servlet2");
        request.setAttribute("price", "100");
        -------------------    // blank to call other servlet Servlet2
    }            				
}
					
import javax.servlet.*;	import javax.servlet.http.*;    import java.io.*;
public class Servlet2 extends HttpServlet
{
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException
    {
        PrintWriter out = response.getWriter();
        out.println(request.getAttribute("price"));
    }            				
}

1. rd.include(request, response);
2. rd.forward(request, response);
3. rd.includeServlet(request, response);
4. rd.forwardServlet(request, response);

12. Choose all the right options that can fill the blank to compile and send response.

import javax.servlet.*;	import javax.servlet.http.*;    import java.io.*;
public class Servlet1 extends HttpServlet
{
    public void service(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException
    {
        PrintWriter out = response.getWriter();
        out.println("Hello 1");
        RequestDispatcher rd = -----------------   	// blank
        rd.include(request, response); 
        out.println("Hello 2");
    }
}  

1. request.getRequestDispatcher(“Servlet2”);
2. response.getRequestDispatcher(“Servlet2”);
3. getServletConfig().getRequestDispatcher(“Servlet2”);
4. getServletContext().getRequestDispatcher(“/Servlet2”);

13. Read the code and choose the right option that can fill the two blanks.

import java.sql.*;
public class JDBCAccess
{
    public static void main(String args[])
    {
       try
       { 
           Class.forName("oracle.jdbc.driver.OracleDriver");
           Connection con  = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tiger");
           Statement stmt = con.createStatement();
           int x = stmt.executeUpdate("insert into Employee values(100, 'S N Rao', 5500.50)");
           System.out.println("No of records inserted: " + x);
           stmt.close();   con.close();
       }
       catch(---------------------  e)   {   }    // blank 1 here
       catch(---------------------  e)   {   }    // blank 2 here
   }
}  

1. ClassNotFoundException, SQLException
2. DriverNotFoundException, SQLException
3. JDBCException, SQLException
4. ClassNotFoundException, DatabaseAccessException

14. Read carefully and choose the right option that is true.

import java.sql.*;
public class DatabaseAccess
{
    public static void main(String args[]) throws Exception
    {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","tiger");
        Statement stmt=con.createStatement();
        con.setAutoCommit(false);
        stmt.addBatch("insert into Employee values(100, 'S N Rao', 2000.50)");
        stmt.addBatch("Delete from Employee where empsal > 8000");
        stmt.addBatch("update Employee set empsal=empsal+500 where empid=100");
        stmt.executeBatch();
        System.out.println("Batch updates done");
        con.commit();                         	
        con.setAutoCommit(true);
        stmt.close();	con.close();
    }	
}

1. code is erroneous as setAutoCommit() method does not exist with Connection interface
2. code is erroneous as commit() method does not exist with Connection interface
3. setAutoCommit() and commit() methods are defined in Statement interface
4. No errors exist and the code compiles successfully

15. Read the snippets of code carefully and choose the right option that is true.

a) HTML snippet that calls the following servlet


Enter Employee ID
Enter Employee Name
Enter Employee Salary

b) Following is the servlet snippet:

import javax.servlet.*;	   import javax.servlet.http.*;    import java.io.*;      import java.sql.*;
public class Validate extends HttpServlet
{
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException
    {
        PrintWriter out = response.getWriter();
        int eid = Integer.parseInt(request.getParameter("id"));
        String ename = request.getParameter("name");
        double esalary = Double.parseDouble(request.getParameter("salary"));
        try
        {
             Class.forName("oracle.jdbc.driver.OracleDriver");
             Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","tiger","tiger");
             Statement stmt=con.createStatement();
             stmt.executeUpdate("insert into Employee values("+eid +", '"+ ename +"'," + esalary + ")");
             out.println(ename + " record inserted");
        }
        catch(Exception e) {  out.println("Problem in access: " + e);  }
    }
}

1. code does not contain errors and record will be inserted
2. code does not compile as insert statement is erroneous
3. code does not compile as ename is not parsed to string
4. code compiles but throws exceptions as out, stmt and con are not closed

16. Read the code and choose the right option that is true.

a) HTML file


  

Getting Great or Small form Server

Enter Name
Enter Salary

b) JSP file: AllTheThree.jsp


Deciding Great or Small

<%! String str1, status; public void display(double sal) { if(sal >= 75000) status="Great"; else status="Small"; out.println(" You are " + status); } %> <% str1 = request.getParameter("nameField"); String str2 = request.getParameter("salaryField"); double salary = Double.parseDouble(str2); %> <% out.println("Hello Mr." + str1); display(salary); out.println(" man"); %>

1. The code does not compile as out object cannot be used from declaration.
2. Two scriptlets in the same page raises error in translation stage
3. str1 is not declared in scriptlet but used and is error
4. none of above

17. Read the code and choose the right option that is true.

a) HTML snippet


    Would you like to get  Today's particulars  sir.

b) DisplayDate.jsp



       <head> Learning JSP </head>


<%@ page import="java.util.*" %> <% Date d=new Date(); out.println("Today is " + d); %>
Today's Particulars: <%= new Date() %>

1. java.util package scope does not exist in expression
2. <a> tag cannot call a JSP file
3. JSP does not support <div> tag of HTML 4
4. No problem with the code, translates, compiles and executes successfully

18. Read the code and choose the right option that is true.

a) BMA.jsp


<%
          application.setAttribute("goldrate", "3575"); 
          application.setAttribute("silverrate", new Integer(42));
%>
         Global Gold and Silver values are set.

b) Customer.html


      

Hyderabad Shopping Mall

Gold Ornament Weight
Silver Article Weight

c) GoldSilver.jsp


Hyderabad Shopping Mall

RTC x Roads
Hyderabad

<% double gw = Double.parseDouble(request.getParameter("goldbox")); double sw = Double.parseDouble(request.getParameter("silverbox")); String str1= (String) application.getAttribute("goldrate"); Integer i1= (Integer) application.getAttribute("silverrate"); int grate = Integer.parseInt(str1); int srate = i1.intValue(); %> Total to pay Rs.<%= gw*grate + sw*srate %>

Thank You Visit Again

1. No problem with the code, translates, compiles and executes successfully
2. Scope of gw, sw, grate and srate does not exist in expression
3. JSP does not support <div> tag of HTML 4
4. Values set with application object in BMA.jsp cannot be used in GoldSilver.jsp

19. Choose the right option that is true regarding the following code.

a) File Name: Holidays.html


      

Do not waste semester holidays. Learn a computer language

b) HTML snippet

Enter H. No.
Enter Street No.
Enter Area
Enter City

c) Demo.jsp snippet:


Following are student particulars 
<% String str[] = request.getParameterValues("saddress"); %>
H. No. Street No. Area City
<%= str[0] %> <%= str[1] %> <%= str[2] %> <%= str[3] %>


<%@ include file="Holidays.html" %>

1. code is without errors and works fine
2. code does not work as include directive syntax is wrong
3. code does not work as expression syntax is wrong
4. code does not work as table values syntax is wrong

20. Read the snippets of code carefully. Employee table contains three fields of empid (of type number), empname (of type varchar) and empsal (of number(6,2)).

a) HTML snippet that calls the following JSP:


b) Records.jsp snippet


<%@ page import="java.sql.*" %>
<%
   try
   {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","system","tiger");
    Statement stmt=con.createStatement();
    ResultSet res = stmt.executeQuery("select * from Employee");             
    while(res.next()
    {
      out.println(------ + " : " + ------ + " : " + ------ + "
"); } res.close(); stmt.close(); con.close(); } catch(Exception e) { out.println("Problem in access: " + e); } %>

What could be the three blanks to retrieve the records just for sending to client.
Choose all the right options.

1. res.getInt(1), res.getString(2), res.getDouble(3)
2. res.getInt(“empid”), res.getString(“empname”), res.getDouble(“empsal”)
3. res.getString(1), res.getString(2), res.getString(3)
4. res.getString(“empid”), res.getString(“empname”), res.getString(“empsal”)

21. Read the snippets and choose the right option that is true.

a) HTML snippet

 
    Enter User Name  

b) Servlet snippet

import javax.servlet.*;	  import javax.servlet.http.*;    import java.io.*;
public class Servlet1 extends HttpServlet
{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException
    {
        RequestDispatcher rd = request.getRequestDispatcher("Demo.jsp");
        rd.include(request, response);
    }
}

c) Demo.jsp snippet:


 <%
       String str1 = request.getParameter("user");
       out.println(str1);
%>

1. does not compile as request object values of Servlet1 cannot be retrieved from JSP
2. getRequestDispatcher() parameter cannot be a JSP file or HTML file and can be a servlet only
3. “user” scope does not exist in JSP page
4. Code is fine without errors and user name is echoed to client

22. Read the snippets and choose the right option.

a) HTML snippet

Enter Password

b) Servlet1

import javax.servlet.*;	import javax.servlet.http.*;    import java.io.*;
public class Servlet1 extends HttpServlet
{
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException
  {
   if(request.getParameter("pass").trim().toUpperCase().equals("TCS"))
   {
     response.getWriter().println("Login is Successful");
   }
   else
   {
     -------------------------      // blank to send Failure.jsp file to client
   }
  }
}

c) Failure.jsp snippet


     Your Login Failed

1. response.sendRedirect(“Failure.jsp”);
2. response.sendFile(“Failure.jsp”);
3. response.send(“Failure.jsp”);
4. response.disptch(“Failure.jsp”);

23. Read the snippets and choose the right option for the blank.

a) HTML snippet

Enter User Name

b) Servlet1

import javax.servlet.*;	import javax.servlet.http.*;    import java.io.*;
public class Servlet1 extends HttpServlet
{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException
    {
          PrintWriter out = response.getWriter();
          String strPass = request.getParameter("pass");
          HttpSession session = request.getSession();
          if(session != null)
          {
               if(strPass.equals("tcs"))
               {
                    session.setAttribute("password", strPass);
                    out.println("session value is set");
               }
               else
               {
	            out.println("session value is not set");
               }
          }
          out.close();
   }
}

b) Servlet2

import javax.servlet.*;	import javax.servlet.http.*;    import java.io.*;
public class Servlet2 extends HttpServlet
{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException
    {
          HttpSession session = request.getSession();
          if(session != null)
          {
              Object  xyz = session.getAttribute("password"); 
              response.getWriter().println(xyz);  
          }
          session.invalidate();
    }
}

1. session object scope of Servlet1 does not exist in Servlet2
2. getAttribute(“password”) return type is wrong
3. code works fine without errors
4. none of above

24. Choose all the right options that are true.

a) HTML snippet

Enter Item Name
Enter Item Quantity

b) Servlet1 snippet

import javax.servlet.*;	import javax.servlet.http.*;    import java.io.*;
public class Servlet1 extends HttpServlet
{
    public void service(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException
    {
         String name = request.getParameter("itemName");
         String qty = request.getParameter("itemQuantity");
         HttpSession session = request.getSession();
         session.setAttribute("itemN", name);
         session.setAttribute("itemQ", qty);
         String strID1 = session.getId();
    }
}

c) Demo.jsp


<%
        String str1  = (String) session.getAttribute("itemN");
        String str2  = (String) session.getAttribute("itemQ");
%>
Item Name is <%= str1 %> 
Item Quantity is <%= str2 %>
<% String strID2 = session.getId(); %> Session ID is <%= strID2 %>

1. strID1 and strID2 will have the same value
2. session of servlet will have scope in JSP
3. code snippets do not have errors
4. none of above

25. Choose the right option to set the timeout to the session that fits in the blank given in the servlet.

a) HTML snippet that calls the following servlet


Enter Password

b) Following is the servlet snippet

import javax.servlet.*;	import javax.servlet.http.*;    import java.io.*; 
public class DemoServlet extends HttpServlet
{
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException
  {
    HttpSession session = request.getSession();  
    if((session != null)  && (! session.isNew()))
    {    
      ----------------------  // set the timeout for the session
    }
    String pass = request.getParameter("password").trim().toUpperCase();
    if(pass.equals("TCS"))
    {
      session.setAttribute("password", pass);
      response.sendRedirect("Connect.jsp");
    }
                    // some business logic here
    session.invalidate();
  }
}

1. session.setMaxAge(30*60);
2. session.setAge(30*60);
3. session.setMaxInactiveInterval(30*60);
4. session.setInactiveInterval(30*60);

26. Read the snippets of code carefully and choose the right option that is true.

a) Login.html snippet calling GetRecord.jsp:


Enter Employee ID
Enter Login Password

b) Following is the GetRecord.jsp snippet:


<%@ page import ="java.sql.*" %>
<%
  int eid = Integer.parseInt(request.getParameter("id"));
  if(request.getParameter("password").trim().equals("tcs"))
  {
    try
    {
     Class.forName("oracle.jdbc.driver.OracleDriver");
     Connection con=DriverManager.getConnection
("jdbc:oracle:thin:@localhost:1521:orcl","scott","tiger");
     Statement stmt=con.createStatement();
     ResultSet res = stmt.executeQuery("select empid, empname, empsal from Employee where empid="+eid);
     res.next();
     out.println(res.getInt("empid") + ", " + res.getString("empname") + ", " + res.getDouble("empsal")); 
    }
    catch(ClassNotFoundException e) {  out.println("JDBC Driver not found: " + e);  }
    catch(SQLException e) {  out.println("Problem in access: " + e);  }
  }
  else
  {
    response.sendRedirect("Login.html");
  }
%>

1. code does not have errors, executes fine and sends record info to client
2. sendRedirect(“Login.html”) cannot be used with response object
3. catching exceptions is not valid
4. res.next() is not required as there is only one record by Employee id “eid”

27. Read the snippets of code carefully and fill in the blank with right option that can insert a record.

a) Login.html snippet calling Cookies.jsp:


 
    Enter Employee ID 
Enter Employee Name
Enter Employee Salary

b) Following is the Insert.jsp snippet:


<%@ page import="java.sql.*" %>
<%
try
{
     Class.forName("oracle.jdbc.driver.OracleDriver");
     Connection con=DriverManager.getConnection
("jdbc:oracle:thin:@localhost:1521:orcl","scott","tiger");
  PreparedStatement pst = con.prepareStatement("insert into Employee(empid, empname, empsal) values(?, ?, ?)");   
    int num = Integer.parseInt(request.getParameter("idField"));
    String name = request.getParameter("idField");
    double sal = Double.parseDouble(request.getParameter("salaryField"));
          
    pst.setInt(1, num);
    pst.setString(2, name);
    pst.setDouble(3, sal);
    ------------------------------   // blank
    out.println("One record inserted");
    pst.close();  	con.close();  
}
catch(Exception e)  { out.println("Record not inserted. Check problem for:
" + e.getMessage()); } %>

1. pst.execute();
2. pst.executeUpdate();
3. pst.executeStatement();
4. pst.executeInsert();

28. Client is sent table data using JSTL . Choose the correct option to fill the blank.
Necessary imports done. JSTL core library prefix is “c” and sql library prefix is “sql”.

    
      
 
      
         SELECT * from Employee;
      
       
            

1. rowdata.rows
2. rowdata.tablerows
3. rowdata.rowscount
4. rowdata.rowsdata

29. Choose the right option that could be the output for the following JSTL snippet of code.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>






1. compilation error as the syntax of if tag is incorrect.
2. No output
3. Big Man
4. false

30. Choose the right option to create an object of CallableStatement to execute the procedure from a Java Application.

a) Procedure Name: first_pro()

create or replace procedure first_pro(x in number, y out number) as
begin
y:= x * x;
end;

b) Java application that executes the above procedure

import java.sql.*;
public class CallProcedure     
{
  public static void main(String args[]) throws Exception     
  {
       Class.forName("oracle.jdbc.driver.OracleDriver");
     Connection con=DriverManager.getConnection
("jdbc:oracle:thin:@localhost:1521:orcl","scott","tiger");
       ---------------------   // blank to create CallableStatement
       cst.registerOutParameter(2, Types.INTEGER);  
       cst.setInt(1, 10); 
       cst.execute();
       int k = cst.getInt(2);
       System.out.println("Result is " + k); 
       cst.close();
       con.close();
   }     
}   

1. CallableStatement cst = con.preparedCall(” { call first_pro( ?, ? ) } “);
2. CallableStatement cst = con.getCallableStatement(” { call first_pro( ?, ? ) } “);
3. CallableStatement cst = con.prepareCall(” { call first_pro( ?, ? ) } “);
4. CallableStatement cst = con.prepareStatement(” { call first_pro( ?, ? ) } “);

SOLUTIONS

1. 3 2. 3 3. 1, 4 4. 3, 4 5. 1
6. 2, 3 7. 1, 2 8. 1 9. 1, 2, 3 10. 1, 2
11. 1, 2 12. 1, 4 13. 1 14. 4 15. 1
16. 1 17. 4 18. 1 19. 1 20. 1, 2, 3, 4
21. 4 22. 1 23. 3 24. 1, 2, 3 25. 3
26. 1 27. 2 28. 1 29. 2 30. 3

Leave a Comment

Your email address will not be published. Required fields are marked *