Hi, I'm new to jsp
I'm writing a java code of simple chat application that consists of an applet on client side and a java server page on server side.
My applet is fine but the jsp code shows unusual errors and in most of the cases the browser simply hangs up

Here's my code
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>CHAT</title>
</head>
<body>
<%@ page import="java.net.*" %>
<%@ page import="java.io.*" %>
<%
ServerSocket socket = new ServerSocket(8282);

Socket insocket = socket.accept( );

BufferedReader in = new BufferedReader (new
InputStreamReader(insocket.getInputStream()));
PrintWriter myout = new PrintWriter (insocket.getOutputStream(),
true);

String instring = in.readLine();
out.println("The server got this: " + instring);
insocket.close();
%>
<applet name='isteapplet' code='chatclient2.class' width=400 height=500>
</applet>
</body>
</html>

This is just so wrong on so many different levels.

First of all, neither a servlet, nor a JSP has any business, what-so-ever even attempting to open a socket, much less a serversocket.

You do realise that a JSP/Servlet is meant to be run mulitple times, right? And a serversocket can only be opened once, and every other attempt to open it will cause a bind exception, right?

You also realise that accept blocks until a connection is made, right? And since (I assume) it is suppossed to be the applet that makes the connection, and accept is called before enough of the html is served to the browser to even know that it is suppossed to start an applet, that is impossible for the applet to make the connection, therefore, the JSP never finishes, your browser neven gets a complete html, and everything deadlocks, right?

You either need to write a complete, standalone server for the ServerSocket (and whatever service it is suppossed to provide), or you will have to write a class that will be kicked of in the Application context by a ContextListener, to provide this service.

commented: I always learn something new from you... +6

Thnx a lot I've realized that thing much earlier than ur post..I'm doing my project in a different way and
that's actually working
Thnx again

It couldn't have been much earlier since they were made on the same day, but OK, at least you've realised your mistake, regardless of how or why.

Hey guys i m new 2 jsp n doing a project in JSP which is based on College Campus n i wanna embed a simple Chat application can u help me to find some examples.......i dont have ne idea how 2 go abt it........

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.