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>

Dani AI

Generated

Short summary and lesson from the thread: embedding a ServerSocket inside JSP page code is the wrong place for a long‑lived network listener. JSPs (and servlets) run per HTTP request inside container threads; creating a server socket there both risks port conflicts and will block the HTTP response while waiting for a connection, which is why the browser appears to hang. was correct to flag both the blocking accept() behaviour and the bind/rewind problems; is right to have moved to a different design.

Recommended architectures for a chat feature (practical choices):

  • Run a standalone chat server (a separate JVM/process) that accepts socket connections and keeps client state; this cleanly separates duties and makes debugging easier.
  • If the listener must live in the webapp JVM, start it once at deploy time from a ServletContextListener (not from a JSP). Use an ExecutorService to hand off each accepted socket to a worker thread and stop the listener cleanly on shutdown.
  • For browser-based chat today, avoid Java applets entirely. Use WebSockets (server support in modern Java servers) or fallback to long‑polling / SSE if WebSocket is not available.

Implementation notes and gotchas:

  • Do not create the listener in request code. Start one background listener at context initialization, store its reference in the servlet context, and close it on contextDestroyed.
  • Use thread-safe collections (ConcurrentHashMap) for client lists, a thread pool to limit concurrency, and sensible socket timeouts so stray connections don’t block forever.
  • Check for BindException (port already in use), firewalls, and container-level permissions when troubleshooting. Test the server independently (netcat/telnet) before wiring the client.

For anyone embedding chat in a web app (for example ): prefer a WebSocket-based approach with a small, dedicated server component or a context-initialized listener—both are far more robust than putting socket code in JSP.

Recommended Answers

All 4 Replies

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.