|
 |
|
 |
 |
|
04-12-2005, 05:17 AM
|
#1 (permalink)
|
|
Code Monkey
Join Date: Apr 2005
Posts: 68
|
URL query
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
__________________
|
|
|
04-12-2005, 06:29 AM
|
#2 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,114
|
When you say "server's console", you mean you're looking at the catalina.out file (or stdout.log, I can't remember which)? All servlet output is put there. Well, that's assuming you're using Tomcat.
|
|
|
04-12-2005, 07:04 AM
|
#3 (permalink)
|
|
Code Monkey
Join Date: Apr 2005
Posts: 68
|
URL Query
Hello Sir,
Thanks for the reply.
I saw the log file but no clue is obtained from there.
Alos all the statements from the Client.java after creating the connection & sending the data through ObjectOutputStream are getting printed & no error /exception is coming.
Also the LoginServlet's is working fine .
Only the MainServlet's messages are not getting printed on Apache Server's console.
So kindly help me rectify my code .
Please reply as soon as possible.
Thanking you,
Jignesh
__________________
|
|
|
04-12-2005, 09:15 AM
|
#4 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,114
|
If nothing is showing up on the server end, are you sure that there is anything actually being sent there?
|
|
|
04-12-2005, 09:30 AM
|
#5 (permalink)
|
|
Code Monkey
Join Date: Apr 2005
Posts: 68
|
URL Query
Sir sending is an afterward matter.
The first thing is that after creating an URL & opening an connection to that URL using openConnection i'm not able to get whether the servlet is getting called or not.
In the servlet code as specified by me(MainServlet.java) i've wrote some messages for indication that the servlet is called but i'm not getting anything printed for that servlet on the Apache's console.
Thanking you,
Jignesh
__________________
|
|
|
04-12-2005, 11:31 AM
|
#6 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,114
|
Ok, at a further glance, the problem is that you've defined "doPost" in your MainServlet, but not a "doGet". By default, all HTTP requests will be of a "GET" nature. Your loginServlet defines "doGet", so that's why it works.
|
|
|
04-12-2005, 12:00 PM
|
#7 (permalink)
|
|
Code Monkey
Join Date: Apr 2005
Posts: 68
|
Sir,
I've tried overriding doGet() also & then calling doPost() from doGet().But the condition is not improving.
So how to approach????
Thanking you,
Jignesh
__________________
|
|
|
04-12-2005, 12:46 PM
|
#8 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,114
|
Could you post your updated code?
|
|
|
04-12-2005, 12:52 PM
|
#9 (permalink)
|
|
Code Monkey
Join Date: Apr 2005
Posts: 68
|
This is the updated code of my MainServlet.java
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;
boolean done = false;
Message message;
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
System.out.println("DEBUG>>> " + this.getClass() +": doGet() called.");
doPost(req, res);
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
System.out.println("Inside MS's doPost()");
try
{
in = new ObjectInputStream(req.getInputStream());
byte[] data = new byte[512];
int i;
while( (i = in.read(data,0,512)) >= 0 )
{
message = (Message)ChatUtils.bytesToObject(data);
}
//data = new byte[MAX_MESSAGE_SIZE];
//in.read(data);
System.out.println("Joined client "+message._username);
}
catch(Exception e)
{
System.out.println("MainServlet's doPost() exception caught"+e);
}
}
}
__________________
|
|
|
04-12-2005, 01:27 PM
|
#10 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,114
|
And you say the LoginServlet works fine? You see the debugging output show up in the logs?
|
|
|
04-12-2005, 01:34 PM
|
#11 (permalink)
|
|
Code Monkey
Join Date: Apr 2005
Posts: 68
|
Sir,in the log file nothing regarding debugging is getting printed & yes LoginServlet.java works fine because all the messages which i have placed in that servlet are getting printed on the Server's console.
As no errors are displayed or exceptions are thrown therfore there is no clue avilable in logs.
Thanking you,
Jignesh
__________________
|
|
|
04-12-2005, 01:51 PM
|
#12 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,114
|
Ok, then. Is the Servlet properly defined in the web.xml file?
|
|
|
04-13-2005, 12:27 AM
|
#13 (permalink)
|
|
Code Monkey
Join Date: Apr 2005
Posts: 68
|
Yes sir,here is my web.xml
Code:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<context-param>
<param-name>sessionTimeout</param-name>
<param-value>30</param-value>
</context-param>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.swing.servlet.LoginServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>mainServlet</servlet-name>
<servlet-class>com.swing.servlet.MainServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/servlet/login</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>mainServlet</servlet-name>
<url-pattern>/servlet/mainServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
Thanking you,
Jignesh
__________________
|
|
|
04-14-2005, 07:00 AM
|
#14 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,114
|
Ok, it looks like it was defined correctly. Have you tried loading it up in a web-browser? I know it's not suppose to be used through a web-browser, but it should at least call the "doGet" method, so you can see the debugging output.
|
|
|
04-14-2005, 01:22 PM
|
#15 (permalink)
|
|
Code Monkey
Join Date: Apr 2005
Posts: 68
|
Now sir i got the connection to the servlet correctly.But when i'm sending some information to the MainServlet.java using ObjectOutputStream & receiving it in the servlet using ObjectInputSream I am getting the error:
"Cannot write output after reading input".
As in my Client.java i've previously used ObjectInputStream to read response from LoginServlet.java,so is it due to that or some other reason.
So how to approach.
Waiting for your reply,
Jignesh
__________________
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 06:38 PM.
|
Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle
|
 |
|