I have one index.jsp
file, one connect.java
servlet and one Server.java
file which creates a chat server.
index.jsp
<form action="connect">
<textarea name="chat-window" ></textarea>
<input name="port-number" placeholder="enter-port"/>
<select name="action-type">
<option> start </option>
<option> stop </option>
<option> refresh </option>
</select>
<button name="apply-btn" type="submit"> apply </button>
</form>
connect servlet
// creates new thread(runnable) to run Server.java and
// passes portNumber, printWriter object generated by response.getWriter() to Server.java
Server.java
// creates serverSocket on portNumber
try {
serverSocket = new ServerSocket(portNumber);
} catch (IOException e) {
showException("Server.startRunning(): new ServerSocket() ", e);
}
printWriter.println("Server is online at " + portNumber + " \n");
It prints Server is online at XXXX
But, after this, i think printWriter
becomes unusable, maybe entire link of index.jsp
page to threaded Server.java
object gets broken. Because, form action="connect"
is completely performed and server is started and it doesn't wait for clients
to get connected.
now, if i add following to Server.java
while(true) {
try {
clientSocket = serverSocket.accept();
printWriter.println("Client Connected.");
// JOptionPane.showMessageDialog("Client Connected.", null);
} catch() {
// Exception handling mechanism
}
}
and if, now, client
connects to Server
, printWriter
statement doesn't print anything (maybe because the actual web-page needs to be refreshed, but why? how can i make it dynamic if this is the problem? ).
I can verify that client
gets connected successfully because if i uncomment the JOptionPane
statement, it shows when a client
gets connected. So, no issues on client-side.
How can i make the printWriter
statement work? Somehow, keep the connection between JSP
and Server.java
alive. Make the Servlet/JSP continuously listen to Server.java
- I didn't have a perfect title for this question.
- If this is impossible, kindly write an alternative.