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:
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 |
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
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
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.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
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
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
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
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
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) 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
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
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
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
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: