I recently visited heroku.com site and tried to deploy my first java program there , I actually had a good start using their java deployment tutorial, and had it run ok. now I have a server code which I need to deploy there , I tried to follow the example but I had some question in mind like,
1- what will be the host in this case , I already tried the app link as if its the host but it throws errors ,
here is my sample server code
public class DateServer {
/** Runs the server. */
public static void main(String[] args) throws IOException {
ServerSocket listener = new ServerSocket(6780);
try {
while (true) {
Socket socket = listener.accept();
try {
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println(new Date().toString());
} finally {
socket.close();
}
}
} finally {
listener.close();
}
}
}
here is my client code
public class DateClient {
/** Runs the client as an application. First it displays a dialog box asking for the IP address or hostname of a host running the date server, then connects to it and displays the date that it serves. */
public static void main(String[] args) throws IOException {
//I used my serverAddress is my external ip address
Socket s = new Socket(serverAddress, 6780);
BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
String answer = input.readLine();
JOptionPane.showMessageDialog(null, answer);
System.exit(0);
}
}
I followed this tutorial https://devcenter.heroku.com/articles/java at their site to upload my server code is there something else I need to do ?!
thanks in advance
Deploy Your Code To deploy your app to Heroku, use the git push command to push the code from your local repository's main branch to your heroku remote. For example: $ git push heroku main Initializing repository, done.
You can deploy your free app as many times as you need to, (we love continuous deployment practices), and as long as you have dyno hours, your app will be live and publicly accessible.
On Heroku, your application must bind to the HTTP port provided in the $PORT environment variable. Given this, the two major problems in your application code above are 1) you are binding to a hardcoded port (6780
) and 2) your application is using TCP instead of HTTP. As shown in the tutorial, use something like Jetty to accomplish the HTTP equivalent of your application and use System.getenv("PORT")
to bind to the right port, like this:
import java.util.Date;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.*;
public class HelloWorld extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.getWriter().print(new Date().toString());
}
public static void main(String[] args) throws Exception{
Server server = new Server(Integer.valueOf(System.getenv("PORT")));
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
server.setHandler(context);
context.addServlet(new ServletHolder(new HelloWorld()),"/*");
server.start();
server.join();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With