Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Q1 Write the difference between Buffered Versus Unbuffered Queries. Q2 Define Co

ID: 3693262 • Letter: Q

Question

Q1 Write the difference between Buffered Versus Unbuffered Queries. Q2 Define Cookies and give an example as a coding (servlet) for a cookies? Q3 List what the XML document consists of, then select which of the following is a "well formed" XML document:

1. <?xml version="1.0"?>

<note>

<to>Tove</to>

<from>Jani</from>

<heading>Reminder</heading>

<body>Don't forget me this weekend!</body>

</note>

2. <?xml version="1.0"?> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body>

3. <?xml version="1.0"?> <note> <to age="29">Tove</to> <from>Jani</from> </note>

4. <?xml version="1.0"?> <note> <to age=29>Tove</to> <from>Jani</from> </note>

Q4: Write a simple servlet that displays the following:

Java EE http://localhost:8080/test-app/hello Eclipse File Edit Navigat Serch Project Run Window Help Quick Access l.: E: 1 | |-Java EE Quick Access . EE Hello.java 3 Project Explorer Servers test-app HelloWorld java |test3 java HelloCounte... Hello.java An outline is not available. http://localhost:8080/test-app/hello Hello World! My Name is:.... Ema: you@yourdomain.com R Markers Properties Servers× Data Source Explorer Snippets Console Remote Systems Tomcat v7.0 Server at localhost [Started, synchronized] mailto:you@yourdoain.com

Explanation / Answer

1) Buffered and Unbuffered Queries :

Unbuffered queries execute the query and then return a resource that points to the result set. This uses less memory, and allows MySQL to continue executing the query as the result set is used. It also increases load on the server.

Buffered query results are stored in memory, which allows additional operations like counting the number of rows, and moving the current result pointer.and Buffered queries are default .

2) Cookies in servlet:

Cookies are the small piece of code that can be store at client side(Browser) and also cookies are the example for session management.

actually session managment is devided into different categories like sessions and cookies etc.

session also same as the cookies but cookie values are store at client side and session values at server side

Cookie Program:

Creation of Cookies:

create a servlet class in default package

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
*
* @author Laasya
*/
public class CookiesEx extends HttpServlet {


protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
Cookie c1=new Cookie("CompanyName","Chegg");
Cookie c2=new Cookie("Company","services");
response.addCookie(c1);
response.addCookie(c2);
out.println("Successfully cookies are added at client side");
}
}


}

Retrieve Cookies from request(client)

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
*
* @author Laasya
*/
public class RetrieveCookies extends HttpServlet {

  
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
Cookie c[]=request.getCookies();
for(int i=0;i<c.length;i++)
{
out.println(c[i].getName()+" ");
out.println(c[i].getValue()+" ");
}
}
}

  
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
  
<servlet>
<servlet-name>CookiesEx</servlet-name>
<servlet-class>CookiesEx</servlet-class>
</servlet>
<servlet>
<servlet-name>RetrieveCookies</servlet-name>
<servlet-class>RetrieveCookies</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CookiesEx</servlet-name>
<url-pattern>/CookiesEx</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>RetrieveCookies</servlet-name>
<url-pattern>/RetrieveCookies</url-pattern>
</servlet-mapping>
  
</web-app>

3) XML:

XML stands for extensible markup language.it mainly used for store and transporting information from one system to another .xml contains user defined tags.

well formed document rules:

for example:

<?xml version="1.0"?>

<note>

<to>Tove</to>

<from>Jani</from>

<heading>Reminder</heading>

<body>Don't forget me this weekend!</body>

</note>

The above XML document is well formed .

<?xml version="1.0"?> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body>

It is not a well formed XML document because there is no one root element.

<?xml version="1.0"?> <note> <to age="29">Tove</to> <from>Jani</from> </note>

The above XML document is well formed .

<?xml version="1.0"?> <note> <to age=29>Tove</to> <from>Jani</from> </note>

It is not a well formed XML document because age attribute value should be in double quotes.

4)

registration.html:

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<form action="Display" method="get">
Username:<input type="text" name="user"/><br>
Address:<input type="text" name="addr"></br>
Mobile:<input type="text" name="no"/></br>
Email:<input type="text" name="email"/>
<input type="submit" value="register"/>
</form>
</center>
</body>
</html>

Display.java:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
*
* @author Laasya
*/
public class Display extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
String name=request.getParameter("user");
String addr=request.getParameter("addr");
String num=request.getParameter("no");
String email=request.getParameter("email");
out.println("Hello World");
out.println("My name is:"+name);
out.println("Address:"+addr);
out.println("Mobile:"+num);
out.println("Email:"+email);
  
}
}

}