Current location: Hot Scripts Forums » Programming Languages » Everything Java » Have a look at my JSP code which is not giving me the desired output


Have a look at my JSP code which is not giving me the desired output

Reply
  #1 (permalink)  
Old 04-07-05, 02:32 AM
j.gohel j.gohel is offline
New Member
 
Join Date: Apr 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Have a look at my JSP code which is not giving me the desired output

Hello sir,

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.

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>
<% 
				
	}
			
%>
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. <pr> 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

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

Last edited by MadDog; 04-07-05 at 03:54 AM. Reason: Tag your code!
Reply With Quote
  #2 (permalink)  
Old 04-07-05, 03:55 AM
MadDog MadDog is offline
Code Master
 
Join Date: Aug 2003
Posts: 935
Thanks: 0
Thanked 0 Times in 0 Posts
j.gohel, Welcome to the Board!

When posting code please use the forums CODE tags so it formats the code and doesnt make the page extremly long. Thanks.
__________________
Drew Gauderman
ASP - MSSQL Coder / Buisness Owner / Coder for Hire!
MSN-ICQ-AIM-YIM in Profile

http://www.iportalx.net an easy ASP portal system.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Classified Ads skipper23 Perl 3 11-22-05 02:22 AM
JSP is not working on the live server braveheart Everything Java 0 03-09-05 08:03 PM
How to sale php code to customer without giving him code pradeep_soft PHP 4 03-12-04 12:10 PM
Classified Ads skipper23 Perl 2 12-30-03 03:43 AM


All times are GMT -5. The time now is 05:30 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.