DA374A Network Applications Preparation for Lab2 Programming Web Applications
Task1: A Proxy Server
In this task, you shall extend your TCP server code to build a proxy server. This proxy server should handle requests from TCP clients who have no http support (such as low end microcontrollers used in smart home or personal devices like an MP3 player).
Your proxy server forwards the client requests to appropriate web resources and relay the responses it receives back to the requesting client. In particular, the proxy server is capable of performing the following tasks:
A: Handling non web clients:
- Accept and interpret requests from TCP clients (such as GETFILE fileURL). The client can be a command line or a swing application.
- Forwarding the request from its clients to servers (in the original URL from the client) and relaying the response back to the clients. You can use the attached method for downloading files from web resources.
- The proxy is used to download files that are:
- Smaller than(<16KB.
- Created after January 10, 2015.
In order to meet these requirements, your server should check the details of the requested file before downloading it.
4. Serve the requested URL from a local copy if available. The proxy server caches copies of web requests before forwarding them to the client. If a client requests the same URL next time, the proxy server can serve the cached copy, instead of accessing the main server.
5. Serves the requested URL from the local copy, ONLY IF THE LOCAL COPY IS RECENT (NOT OLDER THAN 2 DAYS).
6. Maintain a list of blacklisted websites to filter client requests (such as parental control). In case a client attempts to access an inappropriate website, the proxy server shall inform the client that the site is blocked (or deemed inappropriate by the admin).
B: (Optional) Handling requests from HTTP Clients
This task is similar to Task A above. It accepts and interprets http requests from web clients (such as GET, POST, HEAD, …). The client in this case is a web browser (e.g., Internet Explorer, Google Chrome, etc.).
Task2: JEE Web Application
2a) One-tier
In this task, you shall redesign your TCP server in Lab1 as a web server. The difference is, the entire process takes place using the http protocol. The server accepts client web requests via JSP pages and sends back the appropriate response.
- You shall create appropriate user interface in Server1 using JSP for handling client requests (at least 4 JSP pages).
- The JSP pages shall forward the client requests to a servlet
- The servlet handles the request and forwards the result to an appropriate JSP page (different
from the request page).
Create JSP pages with appropriate input fields for each of the following functions (one JSP for each):
Client Browser
Server1 (JSPs, Servlets)
Server2 (Web Service)
– – –
Create
• • • •
Maths (ADD, MULTIPLY, LOG)
Conversion (Temperature: C->F, Currency: SEK->DKK, length: feet->meter ) Statistics (MAX(list), SUM (list), average(list)
A menu page (JSP) from where the client can choose one of the above functions.
A JSP page for each of the above function. These pages are called from the menu page. A result page which shall be used to display the result of the requested function.
Based on the client’s choice, the appropriate JSP from the above is shown to the client
- – When the client submits the request on one of the pages, the request is passed to an appropriate servlet.
- – The servlet performs the necessary calculation and passes the response to the result page and the information is rendered on the client browser.
Browser JSP(input) Servlet JSP (result) Browser
2b) 2-tier
In this case, you modify the program in (2a) above such that Server1 is used as a front-end machine to receive the client requests and forward it to another (Server2). All calculations are performed on Server2. The servlet on Server1 shall:
- – Accept user input from an appropriate JSP page,
- – Invokes an appropriate Web Service method in Server2 and
- – Returns the result it received from Server2 back to the client.
/* A method to download a file from a web server */
// The method below downloads a file located at the URL ‘webAddress’ and copies it to a disk file with full path given by ‘destination’.
// Remember that when you write the destination path you have to use double slash ‘\\’ if you have a windows client machine (e.g. ‘c:\\users\\DT540C\\index.html’.
private void downloadFile(
String webAddress, String destination) {
URL Url;
URLConnection uCon = null;
InputStream is = null;
OutputStream outStream = null;
byte[] buf;
int ByteRead,ByteWritten=0;
boolean exists = false;
long lastupdated = 0;
System.out.println(“Creating URL ….” + webAddress + “\t Local = ” + destination);
try {
Url= new URL(webAddress); File fileDir = new File(destination); exists = fileDir.exists(); if (exists) {
System.out.println("File already Exists! Download
CANCELLED!"); return;
} outStream = new BufferedOutputStream(new
FileOutputStream(destination)); uCon = Url.openConnection();
is = uCon.getInputStream(); buf = new byte[2048]; while ((ByteRead = is.read(buf)) != -1) {
outStream.write(buf, 0, ByteRead); ByteWritten += ByteRead; if (System.currentTimeMillis() - lastupdated >5000) {
System.out.println(ByteWritten+1 + " bytes read..."); lastupdated = System.currentTimeMillis();
} }
System.out.print("\nDownload from URL Successful!\t"); System.out.println("File: \""+ destination + "\"\n No ofbytes :" + ByteWritten);
} catch (Exception e) {
System.out.println("--fileUrl-- ERROR: " + fAddress + "\t Local Name = " + destination); e.printStackTrace();
exists = true; }
finally { try {
if (!exists) { is.close();
outStream.close(); }
} catch (IOException e) {
e.printStackTrace(); }
} }