Magazine
 

Tips & Tricks

content-disposition allows the servlet to specify information about the file’s presentation. This is used to indicate that the content should be opened separately not in the browser; it should not be displayed automatically, suggesting the file name. In this example, the file will be downloaded with the name “MyJar.jar”.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SendJar extends HttpServlet{
public void doGet(HttpServletRequest req,
HttpServletResponse res) throws
ServletException, IOException{
res.setContentType(“application/jar”);
res.addHeader(“Content-Disposition”,
“attachment; filename=MyJar.jar”);
ServletContext sc = getServletContext();
InputStream is = sc.getResourceAsStream(“/
MyJar.jar”);
int len = 0;
byte[] buffer = new byte[1024];
OutputStream os = res.getOutputStream();
while((len = is.read(buffer)) != -1){
os.write(buffer,0,len);
}
os.flush();
os.close();
}
}

4. Chat Server

Chat server is a standalone application that is

  the combination of two-application, server application (which runs on server side) and client application (which runs on client side). This application is used for chatting in LAN. You must be connected with the server before chatting and then your message will be broadcast to every client. This application uses some core java features like swing, collections, networking, I/O streams and threading also. In this application we have one server and number of clients (which are to be communicated with each other). To make a server we have to run the MyServer.java file at any system on the network that we want to make server and for client we have to run MyClient.java file on the system that we want to make client. To run the whole client operation we can run the Login file (Login.java). It can directly call client file (MyClient.java).

Client Side application
Before programming the functionality of client side application we need to take the identification of the user, for example user name so that it can be used further in the next client application. So in this application, “Login.java” creates the login frame that consists of one textfield and the login button. After hitting the login button it shows the next frame that is Client Frame that consists of one textfield to write the message, one send button to send it and two list boxes, one is to show all the messages and the other to show all the user names. This frame has one more button that is Logout button for terminating the chat.

Login.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
// Login class which takes a user name and
passes it to client class
public class Login implements ActionListener{
JFrame frame1;
JTextField tf;
JButton button;
JLabel heading;
JLabel label;

 
 
Nov 2007 | Java Jazz Up |60
previous
index
next
 
View All Topics
All Pages of this Issue
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69   Download PDF