1

I have a small chatting App in Java SE for Desktop, I want to implement the same for Web as well, I have ChatServer as :

public class ChatServer extends Thread {
public static final int DEFAULT_PORT = 6035;


public static String initServer() {
    String msg = "";
    int port = DEFAULT_PORT;
    ServerSocket serverSocket = null;
    Socket socket = null;
    try {
        serverSocket = new ServerSocket(port);
        msg = "Listening on port " + port;
        while (true) {
            socket = serverSocket.accept();
            /*System.out.println("Connection receive from "
                    + socket.getRemoteSocketAddress());*/
            ChatHandler handler = new ChatHandler(socket);
            handler.start();
        }


    } catch (IOException ioe) {
        System.err.println("Usage: java ChatServer [port]");
        System.err.println("Where options include:");
        System.err.println("\tport the port on which to listen.");
        msg = "Usage: java ChatServer [port]";
        System.exit(0);
    } finally {
        try {
            serverSocket.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

    return msg;
}
}

Now to call this in index.jsp, I have :

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1><%=pkj.com.lenvel.ChatServer.initServer() %></h1>
</body>
</html>

the above code is not showing any message and I'm still confused, where to implement code for Client?

2
  • 1
    what do you mean by client? the client is your browser Commented Apr 8, 2014 at 7:40
  • I'm in doubt that you should create a socket within a JSP. Can you create a single class that handles all the connection? Commented Apr 8, 2014 at 15:13

1 Answer 1

2

Try this

Server (YourServer.java)

import java.io.*;
import java.net.*;

public class YourServer
{    
    public static void main(String[] args ) 
    {
        try {    
            ServerSocket socket = new ServerSocket(8765);

            Socket insocket = socket.accept( );

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

            String instring = in.readLine();
            out.println("The server got this: " + instring);
            insocket.close();
        }
        catch (Exception e) {} 
     } 
}

Client.Jsp

<%@ page import="java.io.*, java.net.*" %>
<HTML>
    <HEAD>
        <TITLE>Creating Client/Server Applications</TITLE>
    </HEAD>
        <BODY>
        <H1>Creating Client/Server Applications</H1>
        <% 
        try{
            int character;
            Socket socket = new Socket("127.0.0.1", 8765);
            InputStream inSocket = socket.getInputStream();
            OutputStream outSocket = socket.getOutputStream();
            String str = "Hello!\n";
            byte buffer[] = str.getBytes();
            outSocket.write(buffer);
            while ((character = inSocket.read()) != -1) {
                out.print((char) character);
            }
            socket.close();
        }
        catch(java.net.ConnectException e){
        %>
            You must first start the server application 
            (YourServer.java) at the command prompt.
        <%
        }
        %>
    </BODY>
</HTML>

Taken from this link

Sign up to request clarification or add additional context in comments.

4 Comments

First of All Thank you very much, now on more thing I wanna ask is how to execute the class containing main() on Apache Tomcat Server?
if you are using eclipse, right click on yourserver.java and run it as an application, that's all.
OK, but What I'm trying to ask actually is I have a free hosting server on the Net which is Apache Tomcat Server, now how can I run that server.
run it as a simple java program by command prompt. steps , javac YourServer.java , java YourServer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.