MCS-051 ASSIGNMENT SOLUTION (2019-20)

If you have any queries please leave a message here
Your Message
×


Q1. a) What are the main objectives of session tracking? What are the two ways to handle session tracking ? Explain with the help of an example.

Answer : - HTTP is a “stateless” protocol. Each time a client retrieves a Web page, the client opens a separate connection to the Web server and the server does not keep any record of previous client request.

Session tracking enables you to track a user's progress over multiple servlets or HTML pages, which, by nature, are stateless.

A session is defined as a series of related browser requests that come from the same client during a certain time period.

When there is a need to maintain the conversational state, session tracking is needed. For example, in a shopping cart application a client keeps on adding items into his cart using multiple requests. When every request is made, the server should identify in which client’s cart the item is to be added. So in this scenario, there is a certain need for session tracking.

Solution is, When a client makes a request it should introduce itself by providing unique identifier every time.


Cookies

A cookie is a small piece of data sent from a website and stored in a user’s web browser while the user is browsing that website.

A cookie’s value can uniquely identify a client, so cookies are commonly used for session management. Browser stores each message in a small file, called cookie.txt. When you request another page from the server, your browser sends the cookie back to the server. Cookies have lifespan and are flushed by the client browser at the end of lifespan.

Simple Example of Servlet Cookies

TestServlet.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class TestServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
{
try
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print("<form action='http://localhost:9000/myApp/FirstServlet' method='post'>");
out.print("<p>Name : <input type='text' name='userName'/></p>");
out.print("<input type='submit' value='GO'/>");
out.print("</form>");
out.close();
}
catch(Exception e) { System.out.println(e); }
}
}

FirstServlet.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class FirstServlet extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response)
{
try
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String s = request.getParameter("userName");
out.print("Welcome " + s);
//Creating cookie object
Cookie c = new Cookie("uname", s);
//Adding cookie in the response
response.addCookie(c);
//Creating submit button
out.print("<form action='http://localhost:9000/myApp/SecondServlet'>");
out.print("<p><input type='submit' value='GO'></p>");
out.print("</form>");
out.close();
}
catch(Exception e) { System.out.println(e); }
}
}

SecondServlet.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SecondServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
{
try
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Cookie c[] = request.getCookies();
out.print("Hello " + c[0].getValue());
out.close();
}
catch(Exception e) { System.out.println(e); }
}
}


HttpSession Interface

In such case, container creates a session id for each user.The container uses this id to identify the particular user.An object of HttpSession can be used to perform two tasks :

  1. Bind objects
  2. View and manipulate information about a session, such as the session identifier, creation time, and last accessed time.

Example of using HttpSession

TestServlet.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class TestServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
{
try
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print("<form action='http://localhost:9000/myApp/FirstServlet' method='post'>");
out.print("<p>Name : <input type='text' name='userName'/></p>");
out.print("<input type='submit' value='GO'/>");
out.print("</form>");
out.close();
}
catch(Exception e) { System.out.println(e); }
}
}

FirstServlet.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class FirstServlet extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response)
{
try
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String s = request.getParameter("userName");
out.print("Welcome " + s);
HttpSession session = request.getSession();
session.setAttribute("uname", s);
out.print("<form action='http://localhost:9000/myApp/SecondServlet'>");
out.print("<p><input type='submit' value='GO'></p>");
out.close();
}
catch(Exception e) { System.out.println(e); }
}
}

SecondServlet.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SecondServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
{
try
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession(false);
String s = (String)session.getAttribute("uname");
out.print("Hello " + s);
out.close();
}
catch(Exception e) { System.out.println(e); }
}
}




Q1. b) What are the major differences between a session and a cookie ?

Answer : -

Session

A session creates a file in a temporary directory on the server where registered session variables and their values are stored. This data will be available to all pages on the site during that visit.

A session ends when the user closes the browser or after leaving the site, the server will terminate the session after a predetermined period of time, commonly 30 minutes duration.


Cookies

Cookies are text files stored on the client computer and they are kept of use tracking purpose. Server script sends a set of cookies to the browser. For example name, age, or identification number etc. The browser stores this information on a local machine for future use.

When next time browser sends any request to web server then it sends those cookies information to the server and server uses that information to identify the user.




Q2. Assume there is a table named students which created is Oracle database having the following fields :

a) Enter at least 5 records and write a servlet code which will display all the fields of the above table and all records entered in the table in a tabular form.

Answer : - CREATE TABLE students (enrolment_no INT, s_name VARCHAR2(50) NOT NULL, name_of_school VARCHAR2(100) NOT NULL, programme VARCHAR2(100) NOT NULL, year_of_admission INT NOT NULL, date_of_birth DATE NOT NULL, PRIMARY KEY(enrolment_no));

INSERT INTO students VALUES (105508015, 'Amit Das', 'School of Computer Sciences', 'Master of Computer Application', 2010, TO_DATE('12-09-1980','DD-MM-YYYY'));

INSERT INTO students VALUES (105508045, 'Manirash Das', 'School of Computer Sciences', 'Bachelor of Computer Application', 2010, TO_DATE('10-05-1993','DD-MM-YYYY'));

INSERT INTO students VALUES (105508022, 'Debabrata Panchadhyay', 'School of Computer Sciences', 'Master of Computer Application', 2014, TO_DATE('26-08-1991','DD-MM-YYYY'));

INSERT INTO students VALUES (140504028, 'Ankitaa Das', 'School of Computer Sciences', 'Master of Computer Application', 2018, TO_DATE('18-07-1999','DD-MM-YYYY'));

INSERT INTO students VALUES (150508042, 'Prankrishna Banik', 'School of Computer Sciences', 'Bachelor of Computer Application', 2015, TO_DATE('03-11-1991','DD-MM-YYYY'));


DisplayRecord.java

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import oracle.jdbc.driver.*;
public class DisplayRecord extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String url="jdbc:oracle:thin:@localhost:1521:xe";
String user="system";
String password="9748516231";
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM students");
out.println("<center><p><table border='3' cellpadding='5'>");
out.println("<tr>");
out.println("<th>Enrolment No.</th>");
out.println("<th>Student Name</th>");
out.println("<th>Name of School</th>");
out.println("<th>Programme</th>");
out.println("<th>Year of Admission</th>");
out.println("<th>Date of Birth</th>");
out.println("</tr>");
while(rs.next())
{
out.println("<tr>");
out.println("<td>" + rs.getString("enrolment_no") + "</td>");
out.println("<td>" + rs.getString("s_name") + "</td>");
out.println("<td>" + rs.getString("name_of_school") + "</td>");
out.println("<td>" + rs.getString("programme") + "</td>");
out.println("<td>" + rs.getString("year_of_admission") + "</td>");
out.println("<td>" + rs.getString("date_of_birth") + "</td>");
out.println("</tr>");
}
conn.close();
out.println("</table></p></center>");
}
catch(Exception e) { e.printStackTrace(); }
}
}




Q2. b) Answer the following SQL queries related to the student table.

(i) List all the student enrolled in a school of computer sciences in the year 2016.

Answer : - SELECT * FROM students WHERE name_of_school='School of Computer Sciences' AND year_of_admission=2016;


(ii) Count the number of students registered in BCA Programme.

Answer : - SELECT COUNT(enrolment_no) FROM students WHERE programme='Bachelor of Computer Application';


(iii) List all the students below the age of 25 years.

Answer : - SELECT * FROM students WHERE date_of_birth > TO_DATE('16-10-1994','DD-MM-YYYY')




Q3. Differentiate between the two different types of servlets? What are the two interfaces in the servlet API.

Answer : -

Generic Servlet

GenericServlet Example

import java.io.*;
import javax.servlet.*;
public class GenericServletExample extends GenericServlet
{
public void service(ServletRequest request, ServletResponse response)throws IOException,ServletException
{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.print("<html><body><center><h2>GenericServlet Example</h2></center></body></html>");
}
}

HttpServlet Class

HttpServlet Example

import java.io.*;
import javax.servlet.http.*;
public class HttpServletExample extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)throws IOException
{
PrintWriter out=response.getWriter();
out.print("<html><body><center><h2>HttpServlet Example</h2></center></body></html>");
}
}




Q4. a) Explain the role of JSP in design of dynamic website.

Answer : -




Q4. b) Define custom tags in JSP and describe components that make up a tag library in JSP.

Answer : - A custom tag is a user-defined JSP language element. When a JSP page containing a custom tag is translated into a servlet, the tag is converted to operations on an object called a tag handler. The Web container then invokes those operations when the JSP page’s servlet is executed.

To write a custom tag, you can simply extend SimpleTagSupport class and override the doTag() method, where you can place your code to generate content for the tag.

Create "Hello" Tag

Consider you want to define a custom tag named <ex:Hello> and you want to use it in the following fashion without a body −

<ex:Hello />

To create a custom JSP tag, you must first create a Java class that acts as a tag handler. Let us now create the HelloTag class as follows −

import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;
public class HelloTag extends SimpleTagSupport
{
public void doTag() throws JspException, IOException
{
JspWriter out = getJspContext().getOut();
out.println("Hello Custom Tag !");
}
}

The above code has simple coding where the doTag() method takes the current JspContext object using the getJspContext() method and uses it to send "Hello Custom Tag !" to the current JspWriter object

Let us compile the above class and copy it in a directory available in the environment variable CLASSPATH. Finally, create the following tag library file : <Tomcat-Installation-Directory>webapps\ROOT\WEB-INF\custom.tld.

<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<short-name>Example TLD</short-name>
<tag>
<name>Hello</name>
<tag-class>HelloTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>

Let us now use the above defined custom tag Hello in our JSP program as follows −

<%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%>
<html>
<head><title>Sample Custom Tag</title></head>
<body>
<ex:Hello />
</body>
</html>

Call the above JSP and this should produce the following result −

Hello Custom Tag !




Q5. Assume there is product table in the company’s database created in Oracle with the following fields :

Write a code connecting JSP to Oracle database through JDBC and perform the following operations :

Insert 10 records in the database.

Answer : -

InsertProduct.jsp

<html>
<head><title>Insert New Product</title></head>
<body>
<%@page import="java.sql.*"%>
<%@page import="oracle.jdbc.driver.*"%>
<%
String url = "jdbc:oracle:thin:@localhost:1521:xe";
String user = "system";
String password = "9748516231";
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(url, user, password);
Statement stmt = con.createStatement();
stmt.executeUpdate("CREATE TABLE product (product_id INT PRIMARY KEY, product_name VARCHAR2(50) NOT NULL, year_of_manufacturing INT NOT NULL, product_cost NUMBER(10,2) NOT NULL)");
stmt.executeUpdate("INSERT INTO product VALUES(1, 'MSI B360 GAMING PLUS Motherboard', 2016, 10780.00)");
stmt.executeUpdate("INSERT INTO product VALUES(2, 'MSI Motherboard Z370-A PRO', 2017, 11500.00)");
stmt.executeUpdate("INSERT INTO product VALUES(3, 'ASUS ROG STRIX B365-G Gaming Motherboard', 2016, 12000.00)");
stmt.executeUpdate("INSERT INTO product VALUES(4, 'MSI MEG Z390 GODLIKE Gaming Motherboard', 2018, 65000.00)");
stmt.executeUpdate("INSERT INTO product VALUES(5, 'HyperX Fury 2400MHz 8GB DDR4 RAM', 2017, 4590.00)");
stmt.executeUpdate("INSERT INTO product VALUES(6, 'CORSAIR 3000MHz 16GB Vengeance LPX DDR4 RAM', 2017, 5800.00)");
stmt.executeUpdate("INSERT INTO product VALUES(7, 'G.SKILL TridentZ RGB Series 3000MHz 8GB DDR4 RAM', 2017, 4500.00)");
stmt.executeUpdate("INSERT INTO product VALUES(8, 'Intel Xeon E5-2650V4 2.2GHz 12-Core Processor', 2016, 152565.00)");
stmt.executeUpdate("INSERT INTO product VALUES(9, 'Intel 16-Core i9-9960X Processor', 2018, 160000.00)");
stmt.executeUpdate("INSERT INTO product VALUES(10, 'Intel Core i7-7820X Processor', 2017, 60999.00)");
con.close();
}
catch(Exception e) { e.printStackTrace(); }
%>
<center><h2>10 Records are Inserted Successfully</h2></center>
</body>
</html>




Modify these records.

Answer : -

UpdateProduct.jsp

<html>
<head><title>Modify Product Records</title></head>
<body>
<%@page import="java.sql.*"%>
<%@page import="oracle.jdbc.driver.*"%>
<%
String url = "jdbc:oracle:thin:@localhost:1521:xe";
String user = "system";
String password = "9748516231";
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(url, user, password);
Statement stmt = con.createStatement();
stmt.executeUpdate("UPDATE product SET product_name='Intel 16-Core i9-9960X X-series Processor' WHERE product_id=9");
stmt.executeUpdate("UPDATE product SET product_cost=62590 WHERE product_id=10");
con.close();
}
catch(Exception e) { e.printStackTrace(); }
%>
<center><h2>2 Records are Modified Successfully</h2></center>
</body>
</html>




Display the product name which was manufactured before 2017.

Answer : -

DisplayProductName.jsp

<html>
<head><title>Display Product Name</title></head>
<body>
<center><table border="3" cellpadding="10">
<tr><th>Product Name</th></tr>
<%@page import="java.sql.*"%>
<%@page import="oracle.jdbc.driver.*"%>
<%
String url = "jdbc:oracle:thin:@localhost:1521:xe";
String user = "system";
String password = "9748516231";
String SQL = "SELECT product_name FROM product WHERE year_of_manufacturing < 2017";
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(url, user, password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(SQL);
while(rs.next())
{
%>
<tr><td><%= rs.getString("product_name") %></td></tr>
<%
}
con.close();
}
catch(Exception e) { e.printStackTrace(); }
%>
</table></center>
</body>
</html>




Display the product ID which costs more than Rs. 50000 and manufactured after 2017.

Answer : -

DisplayProductId.jsp

<html>
<head><title>Display Product ID</title></head>
<body>
<center><table border="3" cellpadding="10">
<tr><th>Product ID</th></tr>
<%@page import="java.sql.*"%>
<%@page import="oracle.jdbc.driver.*"%>
<%
String url = "jdbc:oracle:thin:@localhost:1521:xe";
String user = "system";
String password = "9748516231";
String SQL = "SELECT product_id FROM product WHERE product_cost > 50000 AND year_of_manufacturing > 2017";
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(url, user, password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(SQL);
while(rs.next())
{
%>
<tr><td><%= rs.getString("product_id") %></td></tr>
<%
}
con.close();
}
catch(Exception e) { e.printStackTrace(); }
%>
</table></center>
</body>
</html>




Q6. What is the need of connecting servlet and JSP and explain the process through a programme template.

Answer : -




Q7. Describe the following HTTP authentication mechanism for authentication of a user to a web server :

(i) HTTP authentication

Answer : - Basic authentication is the default when you do not specify an authentication mechanism.

When basic authentication is used, the following actions occur :

  1. A client requests access to a protected resource.
  2. The web server returns a dialog box that requests the user name and password.
  3. The client submits the user name and password to the server.
  4. The server validates the credentials and, if successful, returns the requested resource.


(ii) HTTPS client authentication

Answer : - HTTPS Client Authentication is a more secure method of authentication than either basic or form-based authentication. It uses HTTP over SSL (HTTPS), in which the server authenticates the client using the client’s Public Key Certificate (PKC). Secure Sockets Layer (SSL) technology provides data encryption, server authentication, message integrity, and optional client authentication for a TCP/IP connection.

Before using HTTP Client Authentication, you must make sure that the following actions have been completed :

Certificate-Based Mutual Authentication

  1. A client requests access to a protected resource.
  2. The web server presents its certificate to the client.
  3. The client verifies the server’s certificate.
  4. If successful, the client sends its certificate to the server.
  5. The server verifies the client’s credentials.
  6. If successful, the server grants access to the protected resource requested by the client.

Username and Password-Based Mutual Authentication

  1. A client requests access to a protected resource.
  2. The web server presents its certificate to the client.
  3. The client verifies the server’s certificate.
  4. If successful, the client sends its user name and password to the server, which verifies the client’s credentials.
  5. If the verification is successful, the server grants access to the protected resource requested by the client.




Q8. Discuss advantage of using entity bean for database operations over directly using JDBC API. When would one need to be used over the other?

Answer : -




Q9. (i) Explain the difference between external entity and internal XML entities.

Answer : -

Internal Entity
External Entity

If an entity is declared within a DTD it is called as internal entity.

Syntax

<!ENTITY entity_name "entity_value">

In the above syntax −

  • entity_name is the name of entity followed by its value within the double quotes or single quote.
  • entity_value holds the value for the entity name.

The entity value of the Internal Entity is de-referenced by adding prefix & to the entity name i.e. &entity_name.


Employee.xml

<?xml version="1.0" ?>
<!DOCTYPE Employee
[
<!ELEMENT Employee (#PCDATA)>
<!ENTITY ID "E-00045">
<!ENTITY Name "Amit Das">
<!ENTITY Salary "45000">
]>

<Employee>
&ID;
&Name;
&Salary;
</Employee>

If an entity is declared outside a DTD it is called as external entity. You can refer to an external Entity by either using system identifiers or public identifiers.

Syntax

<!ENTITY entity_name SYSTEM "URI/URL">

In the above syntax −
  • entity_name is the name of entity.
  • SYSTEM is the keyword.
  • URI/URL is the address of the external source enclosed within the double or single quotes.


Document.dtd

<!ELEMENT Employee (ID, Name, Salary)>
<!ELEMENT ID (#PCDATA)>
<!ELEMENT Name (#PCDATA)>
<!ELEMENT Salary (#PCDATA)>


Employee.xml

<?xml version="1.0" ?>
<!DOCTYPE Employee SYSTEM "Document.dtd">
<Employee>
<ID>E-00045</ID>
<Name>Amit Das</Name>
<Salary>45000</Salary>
</Employee>




Q9. (ii) How Java Beans and enterprise java beans are different?

Answer : -




Q10. (i) Design XML DTD for an organisation that contains employee information. An employee element has fields: first name, last name, age, address and department. The address element has fields: cities, state and pin code.

Answer : -

Document.dtd

<!DOCTYPE Employee
[
<!ELEMENT Employee (FirstName, LastName, Age, Address, Department)>
<!ELEMENT FirstName (#PCDATA)>
<!ELEMENT LastName (#PCDATA)>
<!ELEMENT Age (#PCDATA)>
<!ELEMENT Address (City, State, Pincode)>
<!ATTLIST Address AddressType (Permanent|Present) "Permanent">
<!ELEMENT City (#PCDATA)>
<!ELEMENT State (#PCDATA)>
<!ELEMENT Pincode (#PCDATA)>
<!ELEMENT Department (#PCDATA)>
]>

Employee.xml

<?xml version="1.0" ?>
<!DOCTYPE Employee SYSTEM "Document.dtd">
<Employee>
<FirstName>Debabrata</FirstName>
<LastName>Panchadhyay</LastName>
<Age>28</Age>
<Address AddressType="Present">
<City>Kolkata</City>
<State>West Bengal</State>
<Pincode>700047</Pincode>
</Address>
<Department>JAVA</Department>
</Employee>




Q10. (ii) How can we check whether the DTD is correct? How many elements should you have in a DTD?

Answer : - A DTD is a Document Type Definition.

A DTD defines the structure and the legal elements and attributes of an XML document.

The purpose of a DTD is to define the structure of an XML document. It defines the structure with a list of legal elements :

<!DOCTYPE Person
[
<!ELEMENT Person (FirstName, MiddleName?, LastName)>
<!ELEMENT FirstName (#PCDATA)>
<!ELEMENT MiddleName (#PCDATA)>
<!ELEMENT LastName (#PCDATA)>
]>

The DTD above is interpreted like this :

Internal DTD
External DTD

If the DTD is declared inside the XML file, it must be wrapped inside the definition :

Person.xml

<?xml version="1.0" ?>
<!DOCTYPE Person
[
<!ELEMENT Person (FirstName, MiddleName?, LastName)>
<!ELEMENT FirstName (#PCDATA)>
<!ELEMENT MiddleName (#PCDATA)>
<!ELEMENT LastName (#PCDATA)>
]>

<Person>
<FirstName>Debabrata</FirstName>
<LastName>Panchadhyay</LastName>
</Person>
<Person>
<FirstName>Kedar</FirstName>
<MiddleName>Nath</MiddleName>
<LastName>Panchadhyay</LastName>
</Person>

If the DTD is declared in an external file, the definition must contain a reference to the DTD file :

Document.dtd

<!ELEMENT Person (FirstName, MiddleName?, LastName)>
<!ELEMENT FirstName (#PCDATA)>
<!ELEMENT MiddleName (#PCDATA)>
<!ELEMENT LastName (#PCDATA)>


Person.xml

<?xml version="1.0" ?>
<!DOCTYPE Person SYSTEM "Document.dtd">
<Person>
<FirstName>Debabrata</FirstName>
<LastName>Panchadhyay</LastName>
</Person>
<Person>
<FirstName>Kedar</FirstName>
<MiddleName>Nath</MiddleName>
<LastName>Panchadhyay</LastName>
</Person>




Q11. Define the basic security concepts.

Answer : -



ABOUT US

QuestionSolves.com is an educational website that helps worldwide students in solving computer education related queries.

Also, different software like Visual Studio, SQL Server, Oracle etc. are available to download in different versions.

Moreover, QuestionSolves.com provides solutions to your questions and assignments also.


MORE TOPIC


Windows Command

UNIX Command

IGNOU Assignment Solution

IGNOU Question Paper Solution

Solutions of Different Questions


WHAT WE DO


Website Devlopment

Training

Home Learning

Provide BCA, MCA Projects

Provide Assignment & Question Paper Solution


CONTACT US


Follow Us