For Avatar2008 Only III

profilekpecet
project_x.doc

1

Copy/paste your answer to questions 1-3 under the word: Answer in this document.

Zip your folder of question 4 and post it.

Answers

Question 1: Add code to the class: MyClient in example 3 of lecture 1 to impose a timeout of 5 seconds. If a timeout does occur, a message “timed out on receiver” should be displayed and the client should be terminated.

Note: This question is similar (not the same) as exercise 4 in chapter 4 of your book.

Note: Only copy/paste the class: MyClient under the word: Answer.

Hints:

1. You need to call the java method: setSoTimeout(5000) of class: DatagramSocket.

2. You need an extra catch-block with the exception class: SocketTimeoutException.

Answer:

Question 2: In a client-serve program we normally run the server first and then the client. If we run the client first we get error message. We like to run the client first and then the server. Change the class: MyClient in example 1 of lecture 2 (which is on sockets) to avoid any exception error. In other words as long as the server is not connected the client stays on with no error.

Note: Only copy/paste the class: MyClient under the word: Answer.

Hints: The client has to loop around until the server gets connect. You may need to include more try-catch block(s) or/and more specific exception class(s). Run the client many times to make sure you do not get any type of exception errors.

Answer:

Question 3 Using RMI, write an application for a prototype opinion poll system. Assume that only one issue is being polled. Respondents may choose yes, or no. Write a client-server application to accept the votes, keep the tally (in memory), and provide the current counts to those who are interested.

Note: Fill the following classes. Do not change the name of the classes and the interface. Copy/paste your answer under the word: Answer.

Note: This question is a shorten version of exercise: 9 on page 230 of your book.

Note: You do not need to make a number of client programs. Make one server and one client programs. Keep the server running and run the client several times. For example I run the client three times as follows. Note that this is just a sample example. Your answer must work for any number of clients:

Enter port: What is your vote?[yes|no] : yes

Yes = 1; No = 0

Enter port: What is your vote?[yes|no] : no

Yes = 1; No = 1

Enter port: What is your vote?[yes|no] : no

Yes = 1; No = 2

Server Side:

import java.rmi.*;

import java.rmi.server.*;

import java.rmi.registry.Registry;

import java.rmi.registry.LocateRegistry;

import java.net.*;

public class Chapter7Problem9Server{

public static void main(String args[]){

}

}

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

import java.rmi.*;

import java.rmi.server.*;

public class VoteServerImpl extends UnicastRemoteObject implements VoteServerInterface {

}

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

import java.rmi.Remote;

public interface VoteServerInterface extends Remote {

public String castVote(char vote )throws java.rmi.RemoteException;

}

Client Side:

import java.rmi.*;

import java.util.*;

public class Chapter7Problem9Client{

public static void main(String args[]){

}

}

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

import java.rmi.Remote;

public interface VoteServerInterface extends Remote {

public String castVote(char vote )throws java.rmi.RemoteException;

}

Answer:

Question 4: Use the following html document and change the servlet program to write a servlet using an input text file (and not cookie class) with a list of names and passwords to do the following:

1. The user enters a name (e.g. John Doe) and a password (e.g. abc). The name and the password are not in the text file. The server should respond the page:

image1.png

2. The user enters a name (e.g. John Doe) and a password (e.g. abc). The name and the password are in the text file. The server should respond the page:

image2.png

Use the following html document and change the servlet program.

<!-- Main.html -->

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Question 4</title>

</head>

<body>

<form action="http://localhost:8080/Project/Main">

<center>

<h1> Please Login</h1>

<br><br>

Name:

<input name="name" type="text" value="">

<br>

Password:<input name="password" type="password" value="">

<br><br>

<input name="login" type="Submit" value=" Submit ">

</center>

</form>

</body>

</html>

//On each line of the file there is: name password

import java.io.*;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

@WebServlet("/Main")

public class Main extends javax.servlet.http.HttpServlet

implements javax.servlet.Servlet {

public Main() {

super();

}

protected void doGet(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException{

String name = request.getParameter("name");

String password = request.getParameter("password");

// Complete the rest of this method. You may need an auxiliary private boolean method to find out

// whether or not a user name and a password are in the file to return true or false

}

}

Note: The content of the variables: name and password are the name and the password the user enters. For example if the user enters: John Doe for the name and: abc for the password the content of the variable: name is John Doe and the content of the variable: password is abc.

The above html document displays the following page:

image3.png

If I enter: John Doe for the name and abc for the password the page is:

image4.png

Note: The name of your servlet class must be: Main.

Note: Do not make a different html document. Do not change my html. Use it.

Note: Each user name and the password must be on the same line and separated by a space in the input file. Name your input text file: in.txt .

Note: Copy/paste your java class under the word: Answer.

Answer