Vous êtes sur la page 1sur 10

QUESTION-3

PART A:
Code:
<!DOCTYPE html>
<html>
<head>
<title>P10_3a</title>
<SCRIPT LANGUAGE="JavaScript">
function Add(form)
{
form.result.value = parseInt(form.no1.value) +
parseInt(form.no2.value);
}
function Subtract(form)
{
form.result.value = form.no1.value - form.no2.value;
}
</SCRIPT>
</head>
<body>
<!-- code for creation of form not required in asked question..! -->
<form>
<table>
<tr><td width="45" >No1</td><td><input name="no1"
size=25></td></tr>
<tr><td>No2</td><td><input name="no2" size=25></td></tr>
<tr><td>Result</td><td><input name="result" size=25></td></tr>
</table>
<input type="button" value="Add" onClick = "Add(this.form)">
<input type="button" value="Subtract" onClick = "Subtract(this.form)">

</form>
</body>
</html>

PART B:
Code:
<!DOCTYPE html>
<html>
<head>
<title>P10_3a</title>
<SCRIPT LANGUAGE="JavaScript">
function UpperCaseConvertor(form)
{
var s = form.field1.value;
form.field2.value = s.toUpperCase();
}
</SCRIPT>
</head>
<body>
<form>
<input type="text" name="field1"> &nbsp;

<input type="button" value="UPPER CASE CONVERTOR" onClick =


"UpperCaseConvertor(this.form)"> &nbsp;
<input type="text" name="field2">
</form>
</body>
</html>
QUESTION-4
Code:
Multithreaded Server Starter:
import java.io.*;
import java.net.*;
public final class TcpServerStarter
{
public static void main(String[] args) throws IOException
{
ServerSocket serverSocket = null;
boolean listening = true;
try
{
serverSocket = new ServerSocket(7777);
}
catch (IOException e)
{
System.err.println("Could not listen on port: 7777.");
System.exit(-1);
}
while (listening){
new P10_5( serverSocket.accept()).start();
System.out.println("Accepted connection from client");}
serverSocket.close();
}
}

Multithreaded Server:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public final class P10_5 extends Thread {
private final Socket socket;
public P10_5(Socket socket) {
setName("P10_5");
this.socket = socket;
}
public void run() {
BufferedReader in = null;
PrintWriter out = null;
int n = 3;
String request;
try {
// Get the input and output streams
in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
class dic {
String word, mean;
dic(String w, String m) {
word = w;
mean = m;
}
}
dic d[] = new dic[4];
d[0] = new dic("coincidence", "itifaq");
d[1] = new dic("thanks", "shukriya");
d[2] = new dic("problem", "masla");
d[3] = new dic("night", "raat");
while ((request = in.readLine()) != null) {
for (int i = 0; i < n; i++) {
if (request.equals(d[i].word)) {
out.println(d[i].mean);
break;
}
if (i == n - 1) {
out.println("No knowledge for " + request);
break;
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (socket != null) {
socket.close(); // Close the socket (closing the socket also
closes the input and output streams)
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

Client:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public final class P10_5Client {
private void process() {
Socket socket = null;
BufferedReader in = null;
PrintWriter out = null;
try {
/* Establish a TCP connection with the server running locally on the
port number 7777.
* You can write "localhost" instead of "127.0.0.1".
* If the server is running on a remote computer, replace "127.0.0.1"
with the server's IP address or hostname
*/
socket = new Socket("127.0.0.1", 7777);
// Get the input and output streams
in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
Scanner user = new Scanner(System.in); // Scanning for user input
String input;
while (true) {
System.out.print("Enter String: ");
input = user.next(); // Hold the input from the user
if (input.equals("end")) {
System.out.println("You ended...");
break;
} else {
out.println(input); // Send it to the server
System.out.println("Name: " + input + " Meaning: " +
in.readLine());
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (socket != null) {
socket.close(); // Close the socket (closing the socket also
closes the input and output streams)
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new P10_5Client().process();
}
}

QUESTION-5
PART B:
Code:
import java.io.*;
class CopyFile {
public static void main(String args[])
throws IOException {
int i;
FileInputStream fin;
FileOutputStream fout;
try {
// open input file
try {
fin = new FileInputStream(args[0]);
} catch (FileNotFoundException e) {
System.out.println("Input File Not Found");
return;
}
// open output file
try {
fout = new FileOutputStream(args[1]);
} catch (FileNotFoundException e) {
System.out.println("Error Opening Output File");
return;
}
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Usage: CopyFile From To");
return;
}
// Copy File
try {
do {
i = fin.read();
if (i != -1) {
fout.write(i);
}
} while (i != -1);
} catch (IOException e) {
System.out.println("File Error");
}
fin.close();
fout.close();
}
}

QUESTION-5
PART B:
Code:
interface Computer {

String ComputerModel = "Dell";


String Version = "P4";

void setModel(String m);

void setType(String t);

void setPrice(double p);

String getComputerModel();

String getVersion();

String getModel();

String getType();

double getPrice();
}

abstract class ComputerAccessories implements Computer {

private String model;


private String type;
private double price;

public ComputerAccessories(String m, String t, double p) {


setModel(m);
setType(t);
setPrice(p);
}

public void setModel(String m) {


model = m;
}

public void setType(String t) {


type = t;
}

public void setPrice(double d) {


price = d;

public String getComputerModel() {


return ComputerModel;
}

public String getVersion() {


return Version;
}

public String getModel() {


return model;
}

public String getType() {


return type;
}

public double getPrice() {


return price;
}

public void PCInfo() {


System.out.println("Computer Model: " + ComputerModel);
System.out.println("Version: " + Version);

public abstract String getName();

public abstract String getExtra();


}

class keyboard extends ComputerAccessories {

String cable;

public keyboard(String m, String t, double p, String n) {


super(m, t, p);
cable = n;

public String getName() {


return "KEYBOARD";
}

public String getExtra() {


return "Cable = " + cable;
}

class CD_ROM extends ComputerAccessories {

boolean b;
public CD_ROM(String m, String t, double p, boolean n) {
super(m, t, p);
b = n;
}

public String getName() {


return "CD ROM";
}

public String getExtra() {


if (b == true) {
return "RW";
} else {
return "not RW";
}
}

class mouse extends ComputerAccessories {

String port;

public mouse(String m, String t, double p, String n) {


super(m, t, p);
port = n;
}

public String getName() {


return "MOUSE";
}

public String getExtra() {


return "Port # " + port;
}
}

class P10_5b {

public static void main(String args[]) {


Computer C[] = new Computer[3];
C[0] = new keyboard("dell", "input", 100.00, "usb");
C[1] = new CD_ROM("dell", "input", 100.00, true);
C[2] = new mouse("dell", "input", 100.00, "8080");

for (int i = 0; i < 3; i++) {


ComputerAccessories CA = (ComputerAccessories) C[i];
CA.PCInfo();
System.out.println(CA.getName() + ":");
System.out.println("Model: " + CA.getModel()
+ " Device Type: " + CA.getType()
+ " Price: " + CA.getPrice()
+ " Extra: " + CA.getExtra() + "\n");
}
}
}

QUESTION-6
PART B:
Code:
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;
import java.sql.*;

public class Servlet 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. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h4>HTML Form</h4>");
out.println("<form action = 'Servlet'>");
out.println("<table>");
out.println("<tr><td>Name: </td><td><input name =
'cname'/></td></tr>");
out.println("<tr><td>Address: </td><td><input name =
'address'/></td></tr>");
out.println("<tr><td>Phone: </td><td><input name =
'phone'/></td></tr>");
out.println("<tr><td>Comments: </td><td><input name =
'comments'/></td></tr>");
out.println("<tr><td></td><td align = 'right'><input type = 'submit'
value = 'Submit'/></td></tr>");
out.println("</table>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn = DriverManager.getConnection("Jdbc:Odbc:Shop");
String s1 = request.getParameter("cname");
String s2 = request.getParameter("address");
double s3 = Double.parseDouble(request.getParameter("phone"));
String s4 = request.getParameter("comments");
String sql = "SELECT * FROM Customer";
PreparedStatement pStmt = cn.prepareStatement(sql,
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = pStmt.executeQuery();
rs.moveToInsertRow();
rs.updateString("name", s1);
rs.updateString("address", s2);
rs.updateDouble("phone", s3);
rs.updateString("comments", s4);
rs.insertRow();
cn.close();
} catch (Exception e) {
System.out.println(e);
}
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}

QUESTION-7
Code:
HTML Form:
<!DOCTYPE html>
<html>
<head>
<title>HTML Form</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action = "process.jsp">
RollNo: <input name = "t1"/><br><br>
Name: &nbsp;&nbsp;<input name = "t2"/><br><br>
<input type = "submit" value = "Submit">
</form>
</body>
</html>

RETRIEVE AND SAVE IN COOKIES:


<%--
Document : process
Created on : 16-Nov-2014, 22:17:28
Author : ali
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>process</title>
</head>
<body>
<%
try {
// get request parameters
String roll_no = request.getParameter("t1");
String name = request.getParameter("t2");
Cookie cookie1 = new Cookie("cookieRollNo", roll_no);
response.addCookie(cookie1);
Cookie cookie2 = new Cookie("cookieName", name);
response.addCookie(cookie2);
} catch (Exception e) {
System.out.print("Exception" + e);
}
%>
All retrieved values saved!
<br>To see values saved in session...<br>
<form action="view.jsp" >
<input type=submit value="Click here...">
</form>
</body>
</html>

VIEW COOKIES:
<%--
Document : view
Created on : 16-Nov-2014, 22:17:47
Author : ali
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>view</title>
</head>
<body>
<%
try {
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
%>
Name:
<%=cookies[i].getName()%>
<br >
Value:
<%=cookies[i].getValue()%>
<br >
<% }
}
} catch (Exception e) {
System.out.print("Exception" + e);
}
%>
</body>
</html>

Vous aimerez peut-être aussi