hello,
On my client side a standalone program made in swing i have created two URLS which i have to use it for reading an ObjectInputStream coming from authentication sevlet & write an ObjectOutputStream to posting message servlet respectively.
Both the servlets name are mapped in an web.xml file.
So please tell me what could be wrong in my code.
This is the code for my Client-side :
Client.java
Code:
package com.swing;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.io.IOException;
import java.net.*;
import com.swing.MainFrame;
import com.swing.Message;
import com.swing.User;
import com.swing.ChatConstants;
public class Client implements ChatConstants
{
JFrame fm;
JPanel pnl1;
JPanel pnl2;
JPanel pnl3;
JPanel pnl4;
JPanel pnl5;
JButton newId;
JButton signIn;
JButton cancel;
JLabel newUser;
JLabel id;
JLabel password;
JLabel registered;
JTextField txtid;
JPasswordField txtpwd;
URL url;
URLConnection con;
URL url2;
URLConnection con2;
String nick;
String pwd;
ObjectInputStream in=null;
ObjectOutputStream out=null;
boolean connected = false;
Thread thread;
//MessageDispatcher _dispatcher;
String userName;
User user;
public static Client client;
public void gui()
{
//gui() part
fm.setSize(400,300);
fm.setLocation(300,200);
fm.setResizable(false);
fm.setVisible(true);
}
class Chat implements ActionListener,Runnable //,ChatConstants
{
public void actionPerformed(ActionEvent ae)
{
try
{
nick=txtid.getText();
nick=nick.trim();
pwd=txtpwd.getText();
pwd=pwd.trim();
url = new URL("http://localhost:8080/OurChatApp/servlet/login"+"?"+"nickname="+nick+"&"+"pwd="+pwd);
con=url.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setUseCaches(false);
System.out.println("successfully connected");
System.out.println("Inside Connect method");
in = new ObjectInputStream(con.getInputStream());
//out = new ObjectOutputStream(con.getOutputStream());
connected = in.readBoolean();
if(connected==true)
{
thread = new Thread(this,"USER THREAD");
thread.start();
System.out.println("Thread created");
user = (new User(nick,ONLINE));
setUser(user);
sendClientLogin();
}
else
{
JOptionPane.showMessageDialog(null,"Invalid User","Information",JOptionPane.INFORMATION_MESSAGE);
}
}
catch(MalformedURLException m)
{
System.out.println("URL error...."+ m);
}
catch(IOException i)
{
System.out.println("IO error"+i);
}
}
public void run()
{
System.out.println("Inside run()");
}
}//Chat inner class over
//main()
public static void main(String[] args)
{
client = new Client();
client.gui();
}
//setUser()
public void setUser(User user)
{
this.userName = user.toString();
this.user = user;
System.out.println("Name: "+this.userName);
}
//sendClientLogin()
public void sendClientLogin()
{
Message message = new Message(CLIENT_LOGIN);
//message._message = _password;
message._user = user;
try
{
System.out.println("Inside sendClientLogin()");
url2 = new URL("http://localhost:8080/OurChatApp/servlet/mainServlet");
con2 = url2.openConnection();
con2.setDoOutput(true);
con2.setDoInput(true);
con2.setUseCaches(false);
//System.out.println("successfully connected to second servlet");
out = new ObjectOutputStream(con2.getOutputStream());
sendMessageToServer(message);
}
catch(java.io.IOException ie)
{
System.out.println("sendClientLogin() exception caught"+ie);
}
}
//sendMessageToServer
public void sendMessageToServer(Message message) throws java.io.IOException
{
System.out.println("Inside sendMessageToServer()");
byte[] data;
//PACK THE USERNAME AND HOST ADDRESS EVERYTIME
message._username = userName;
//message._host = _address;
data = ChatUtils.objectToBytes(message);
out.write(data,0,data.length);
out.flush();
System.out.println("After sending MessageTo Server");
}
}
Now i've created two URLs to communicate with as mentioned previously.
The code for both the servlets are as follows
LoginServlet.java(the authentication servlet)
Code:
package com.swing.servlet;
import java.io.*;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.swing.*;
public class LoginServlet extends HttpServlet
{
boolean flag = false;
String s1,s2;
int i = 1800;
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
doPost(req,res);
}
public void doPost(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
throws ServletException, IOException
{
System.out.println("Inside doPost()");
String s = "jdbc dbc:chat";
contextPath = httpservletrequest.getContextPath();
s1 = httpservletrequest.getParameter("nickname");
s2 = httpservletrequest.getParameter("pwd");
s1 = s1.trim().toLowerCase();
if(s2.length() > 0)
s2 = s2.trim().toLowerCase();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connection = DriverManager.getConnection(s);
Statement statement = connection.createStatement();
for(ResultSet resultset = statement.executeQuery("select * from Login"); resultset.next()
{
String s3 = resultset.getString(1);
String s4 = resultset.getString(2);
if(s1.equals(s3) && s2.equals(s4))
{
flag = true;
}
}
}//try ends here
catch(SQLException sqlexception)
{
System.out.println("Error...." + sqlexception);
}
catch(ClassNotFoundException classnotfoundexception)
{
System.out.println("Error...." + classnotfoundexception);
}
if(flag=true)
{
try
{
System.out.println("Control is here now...1");
HttpSession httpsession = httpservletrequest.getSession(true);
int i = 1800;
String s2 = getServletContext().getInitParameter("sessionTimeout");
System.out.println("Control is here now...5");
if(s2 != null)
try
{
i = Integer.parseInt(s2);
i *= 60;
}
catch(NumberFormatException numberformatexception)
{ }
httpsession.setMaxInactiveInterval(i);
httpsession.setAttribute("nickname", s1);
System.out.println("Control is here now...6");
out = new ObjectOutputStream(httpservletresponse.getOutputStream()); out.writeBoolean(flag);
out.flush();
out.close();
}//try ends
catch(Exception e)
{
System.out.println("Exception thrown in LoginServlet: " + e);
e.printStackTrace();
}
}//if ends here
else
{}
}//do post ends here
private String contextPath;
ObjectOutputStream out=null;
}
The code for second servlet MainServlet.java((the message posting servlet) is as follows:
Code:
package com.swing.servlet;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.swing.*;
import com.swing.ChatUtils;
import com.swing.Message;
import com.swing.ChatConstants;
public class MainServlet extends HttpServlet implements ChatConstants
{
ObjectInputStream in = null;
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
System.out.println("Inside MS's doPost()");
try
{
byte[] data;
in = new ObjectInputStream(req.getInputStream());
data = new byte[MAX_MESSAGE_SIZE];
in.read(data);
Message message = ((Message)ChatUtils.bytesToObject(data));
System.out.println("Joined client "+message._username);
}
catch(Exception e)
{
System.out.println("MainServlet's doPost() exception caught"+e);
}
}
}
The problem is that MainServlet.java is nothing showing on the server's console although from the Client.java every stmt is executing successfully.
Please reply.
Thanking you,
Jignesh