The Artima Developer Community
Sponsored Link

Java Answers Forum
Code not giving desired output

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Jignesh J Gohel

Posts: 1
Nickname: jignesh
Registered: Apr, 2005

Code not giving desired output Posted: Apr 7, 2005 9:58 AM
Reply to this message Reply
Advertisement
Hello,

I am making a chat application using JSP & Servlets.I have written the code for my one-to-one chat module which is not working fine.So please have a
look at this code to rectify the errors due to which i'm not getting the desired output.

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

This time i've used the concept of XMLHttpRequest in this code.the code for "send121.jsp" is as follows:



<%@ 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>
<%

}

%>




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



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




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 ArrayListbut 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 if you could help me sort out the where the problem lies in my code I will be grateful to you.

Thanking you,
Jignesh

Topic: Issues Initialzing and Accessing Integer Array Previous Topic   Next Topic Topic: Why Object class has wait(), notify() & notifyall() method

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use