Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > Java
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 04-10-2005, 12:54 AM   #1 (permalink)
j.gohel
Code Monkey
 
Join Date: Apr 2005
Posts: 68
j.gohel is on a distinguished road
JSP code problem

Hello,

I am making a chat application using JSP & Servlets.In that in my
one-to-one chat module currently i'm workin on i'm facing smal problem as follows:

This code is for sending the message to a particular user through my JSP page send121.jsp using the servlets.

Ii've used the concept of XMLHttpRequest in this code.
The code for "send121.jsp" is as follows:


Code:
<%@ page errorPage="error.jsp" import="java.util.Set,java.util.ArrayList,java.util.Iterator,java.util.Map,com.chat.*"%> <% String nickname = (String)session.getAttribute("nickname"); ArrayList messageList=null; String mess = null; String msg = null; if(nickname != null) { %> <HTML> <HEAD> <script language="javascript"> var theURL = "<%=request.getContextPath()%>/servlet/oto?message="; var http = getHTTPObject(); function handleHttpResponse() { if (http.readyState == 4) { } } function callServlet(formElement) { var searchQuery = document.forms[0].elements[formElement].value; http.open("GET", theURL + escape(searchQuery), true); http.onreadystatechange = handleHttpResponse; http.send(null); } function getHTTPObject() { var xmlhttp; try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { xmlhttp = false; } } if (!xmlhttp && typeof XMLHttpRequest != 'undefined') { try { xmlhttp = new XMLHttpRequest(); } catch (e) { xmlhttp = false; } } return xmlhttp; } </script> </HEAD> <BODY onLoad="document.msg.messagebox.focus();" bgcolor="#FFFFFF" div align="center"> <center> <table border=0 width=640> <tr> <td> <table border=0 bgcolor=white width=100% class=content cellpadding=8> <form name="send" action="#" onSubmit="return false;"> <tr> <td align="right">< b> Send: </b>< /td> <td align="left" > <input type=text name=txtfield size="40" maxlength="60" onBlur="callServlet(1);" > </td> </tr> <tr > <td valign="top" align="right"<b> Messages: </b> </td> <td align="left" > <TEXTAREA cols="30" rows="5" wrap="VIRTUAL" name="comment"><%//=msg%></TEXTAREA > </td> </tr> </form> </table> <% messageList = (ArrayList)request.getAttribute("message"); if(messageList!=null) { for(int i = 0; i <messageList.size() ; i++) { mess = (String) messageList.get(i); msg = nickname + "->" + mess; System.out.println(msg); } } %> </td> </tr> </table> </center> </BODY> </HTML> <% } %>


In this when the 'onBlur' event is executed the callservlet()Function of javascript is called, through which my OnetoOneServlet .java gets the message parameter,that message parameter's value i'm storing in an ArrayList using a plain old java class namely OnetoOne.java.


the code for both the servlet & plain java class i'm providing below.


OnetoOneServlet.java

Code:
import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import com.chat.OnetoOne; public class OnetoOneServlet extends HttpServlet { String nickname; HttpSession session; String msg; public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { session = req.getSession(); nickname = (String)session.getAttribute("nickname"); msg = req.getParameter("message"); if (nickname != null && nickname.length() > 0) { OnetoOne oto = new OnetoOne(); List result = oto.getMessages(msg); req.setAttribute("message",result); getServletContext().getRequestDispatcher("/send121.jsp").forward(req, res); } }//doGet() over }



OnetoOne.java

Code:
import java.util.*; public class OnetoOne { public List getMessages(String message) { List msg = new ArrayList(); msg.add(message); return msg; } private final void _mththis() { messages = new LinkedList(); messages_size = 25; } public OnetoOne() { _mththis(); } private String name; private List messages; private int messages_size; }

Now the problem i'm facing is that the message is getting stored in the ArrayList but on the JSP side i'm not able to get it printed in the TextArea provided nor i'm able to get it on the server console for verifying purpose.


So please help me sort out the solution to this problem.

Thanking you,
Jignesh
__________________
j.gohel is offline   Reply With Quote
Old 04-10-2005, 07:45 AM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
At a glance, I can't spot anything going wrong in the Servlet or JSP. However, if I had to venture a guess, you might want to double-check this:

Code:
function callServlet(formElement) { var searchQuery = document.forms[0].elements[formElement].value; http.open("GET", theURL + escape(searchQuery), true); http.onreadystatechange = handleHttpResponse; http.send(null); }
You're doing stuff there that I'm not familiar with. Is it Javascript or ActiveX? Further, I'm not sure you're forming the HTTP request correctly in that bit of code, so the might explain the problems.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 04-10-2005, 10:16 AM   #3 (permalink)
j.gohel
Code Monkey
 
Join Date: Apr 2005
Posts: 68
j.gohel is on a distinguished road
Hello,

Thanks for the reply.
Its actually XMlHttpRequest or rather called as Ajax(Asynchronous Javascript & XML) and as per my thinking there is no problem with the callServlet() method.Its working fine because i'm able to send the message from the textfield successfully & it is getting stored in the arraylist but the problem lies in retrieving that message from the arraylist & puttiing that retrieval code at the proper place in the "send121.jsp"

So where to put that retrieval code in "send121.jsp" & also how to make it display in the textarea.

Thanking You,
Jignesh
__________________
j.gohel is offline   Reply With Quote
Old 04-10-2005, 10:45 AM   #4 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
If this is a chat application, I assume you're taking input from several sessions and coordinating them, right? Why not store the ArrayList in the application scope (you can access the Context object by calling getServletContext() ) ? That way all the sessions can access the variable. Try doing that, then simply retrieving the List in the JSP and process it on the fly . . .
__________________
GitS
Belisarius is offline   Reply With Quote
Old 04-15-2005, 11:57 AM   #5 (permalink)
j.gohel
Code Monkey
 
Join Date: Apr 2005
Posts: 68
j.gohel is on a distinguished road
JSP Code Problem

Hello sir,
Thanks for the reply.

I'm maintaining my sessions using Servlets & using it all over my application.

Sir i'would like to ask you that is it correct to use XMLHttpRequest as i've seen some examples on the internet using this concept.But in all of them common thing was that they were only "GETTING" from the server side like the search engines & not posting anything.

So is it possible to use Ajax concept to "POST" on the Serverside as at present i'm not able to even store the messages on the server side which previously got stored.

OR if you could tell me that is it possible to use SIMULTANEOUSLY :the HTML forms action attribute to call a servlet & make available the page for sending the message continuosly in front of the user as its for a Chat Application.

So please guide me how to approach.

Waiting for your reply.....

Thanking you,
Jignesh
__________________
j.gohel is offline   Reply With Quote
Old 04-15-2005, 12:34 PM   #6 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
I've honestly never looked at Ajax before, so I can't comment on what it can or can't do, sorry.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 04-15-2005, 01:14 PM   #7 (permalink)
j.gohel
Code Monkey
 
Join Date: Apr 2005
Posts: 68
j.gohel is on a distinguished road
If you no Know any other person who can help me then please provie me his contact information or resources regarding this topic then please inform me

Thanking you,Jignesh
__________________
j.gohel is offline   Reply With Quote
Old 04-15-2005, 02:07 PM   #8 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
There was a Slashdot article on it a while back. I'm afraid that's the best I can do.
__________________
GitS
Belisarius is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hashing problem jodders Standard C, C++ 1 02-09-2005 01:51 PM
BB Code Parsing with JSP sde Java 0 01-13-2005 07:00 AM
Cisco Code breaking sde Code Newbie News 0 05-21-2004 07:10 AM
omfg... well, i may need help just understanding this problem... .pakmon. Standard C, C++ 3 01-08-2004 08:44 AM
An Introduction to XHTML/CSS Rie HTML / CSS 0 03-07-2003 06:50 PM


All times are GMT -8. The time now is 03:26 AM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle