• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Running Servlets in web server

 
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am new to JEE. I have a server java file which runs as a thread in servlet and I have uploaded both to my server. I don't have any idea on how to run this servlet so that my client could connect with it. Please help me, I am breaking my head with this for 2 days.

P.S : Both my client and server java file have socket connections.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this the same problem as the one you asked about in https://coderanch.com/t/621510/JME/Mobile/Chat-Server-JME? In other words, the thread is sort of an HTTP server? If so, my question would still be why you're creating an HTTP server inside of an HTTP server.
 
Ranch Hand
Posts: 558
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Udayakumar,

When you say you uploaded the servlet to the server, do you mean a proper deployment as defined by spec. Also what server is that? Is that just a HTTP Server or a web server defined by spec. If you have used a webserver like Tomcat, all you need to do is bundle your project as a war file and deploy to the tomcat.(refer Tomcat documentation or any examples) and once the application is deployed, if your servlet is properly designed then it will be ready to accept any HTTP requests. All you need is a browser to connect to the servlet.

What is the Thread java file you are referring? What is the intention of that file ?

Thanks


Unable to delete the post and hence striking
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ulf,

Ya the same problem. I don't get what you mean by

In other words, the thread is sort of an HTTP server? If so, my question would still be why you're creating an HTTP server inside of an HTTP server.



My problem is I need to connect two clients over the internet, for that I have the server file which I showed you in the example.


Kumar Raja,

What happened? I don't know about proper deployment that is why I asked question here else I would have continued in the JME forum shown by Ulf. My program runs fine in localhost. I used Glassfish Server 4.0 bundled with Netbeans to test the servlet
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's start a bit simpler, then: the client and the server that you're implementing are supposed to communicate via HTTP - true or false?
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ulf,

Yes.

This is a simple chat application but the client here is JME application. I have uploaded the server java file and the servlet file to my web server. The application works well in localhost but did not work when I replace localhost and give either my server IP or my website address.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My point is, you should not start a new thread for HTTP communication. Servlets already do all that - change your server-side code to be a servlet instead, then you don't have to do all that HTTP handling.

What's more , your code will break as soon as it's running on a server where port 80 is used by a proper web server. I'm guessing that may be the issue you're running into now. Although it would be surprising if the code could even open a ServerSocket on port 80 in that case - I would have expected an exception in that case.
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ulf,

Thanks again. But could you tell me how to run my servlet in my web server. Because if I run my servlet file through browser, the code is displayed (which we all know by now). So can you please help me in this regards.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A web server can't run servlets - you need a servlet container. How you would integrate the two depends on which web server and which servlet container you want to use.

But now I'm confused - in https://coderanch.com/t/621510/JME/Mobile/Chat-Server-JME you posted code that runs in a servlet, so apparently you already have a servlet container running?
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ulf,

I didn't know that. This was my problem Thanks for the help anyways. If I run my servlet after deploying in a java web host which contains Glassfish server, would it work properly or should i do some more work there.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, GlassFish can run servlets.
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ulf,

I am using jelastic layershift for running the servlet. I can run the servlet in a web browser but when i give the same address without the servlet name in my program, it shows Unknown Host Exception. If I convert it to a ip in websites, it shows cannot be resolved to ip
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does "in my program" mean you're trying programmatic access of that servlet? If so, make sure that there is network connectivity between the client where the code runs, and the server it's trying to access. Start by running a Ping from the client to the server.
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ulf,

Pinging the site works fine. But when I connect to it through program, it shows javax.microedition.io.ConnectionNotFoundException: error 10061 in socket:pen.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So your "program" is an app running on a JME device? And pinging the server from the device works?
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ulf,

Ya, the program is an app and I pinged from command prompt, it worked fine
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I pinged from command prompt, it worked fine


Just to be perfectly clear on this, because it matters: this command line from which you pinged the host was running on the device? It didn't run on your desktop computer?
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ulf,

I used my command prompt to ping the web server. Now it doesn't show any error or connect.

As I told you earlier both my client and server files connect through sockets. Is it possible for these to connect through sockets if we use servlet as a medium to connect them over internet?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I'm going to assume that it is not assured that you device has connectivity to the server you're trying to reach. There could be many reasons for that. Let's start with some simple questions:

1) What kind of general network connectivity does your device have? 3G/3G? Wifi? If Wifi, to a local network of yours, or some public network?

2) Where is your server located from a network point of view? Somewhere where it is publicly reachable? Or on some private network? If the latter, is it reachable form the public internet?

So... TellTheDetails, the more the better.
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ulf,

1) I am not using a mobile device, I am just using the emulator provided by net beans.

2) I think its publicly reachable. I don't know how to determine it. Jelastic is the server name and it is a free Java web host . I can ping it from command prompt from my machine as I told you earlier.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see, yes, you did mention that. The important point is: can that emulator reach that server at all via TCP? So far it all points to no, it can't. So that's more of an emulator or NetBeans problem than a servlet problem (and I don't know anything about either, so I can't help with that).

A little hint for your future questions: be precise and accurate in what you write. I asked two times specifically about pinging the server from the device. The first time you said yes to both of those (even though no would have been correct on both counts), the second time you avoided both parts, even though I indicated in bold what was important about the question. The easier you make it for people to help you, the more likely it is that help will come.
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ulf,

I am Sorry for the miscommunication that happened. I should have told you the clear plot. Anyways thanks for the help. I tried writing a client server ordinary java programs that behave in the same way. They can connect to with each other in localhost. When i turn the server to a servlet, it connects when it is in localhost. But it doesn't connect when i move the servlet to a java web host and try to access it. What could be the problem? Any ideas?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds like the same issue - no network connectivity between the client and the server. There could be lots of reasons for that. But what does "it doesn't connect" mean? What exactly is the code trying to do, and what exactly is the result? TellTheDetails, lots of it. For example, at some point you were talking about ports - which ports are you using? Web hosts do not generally open ports other than 80 and 443, and both will be taken by the web server.
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ulf,

1) My client code and server code connect to each other using sockets to transfer data between them.

2) When I run the servlet in the web server,it runs and shows the lines default lines which we would print using PrintWriter. My client also runs but doesn't do anything. It stops at the dos.writeUTF() line since it doesn't have any output stream to write in.

3) I used ports 5555, 8080, 8082,6231. I will try 80 and 443.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I used ports 5555, 8080, 8082,6231. I will try 80 and 443.


Ports 80 and 443 will likely not work, because they're used by the web server, as I said in my last post. In the other topic you were doing some weird thing with starting a thread which then opened a ServerSocket on port 80; I explained there why that wouldn't work, and what you needed to do instead.

As to the other ports, your code might not be allowed to open those in an environment like Jelastic; check their documentation or ask their customer service.
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ulf,

Yes. I changed my connection in a way that the Servlet contains the Server Socket coding. I will try contacting the Jelastic members.
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got the following reply from jelastic admin

In the trial you have the following architecture:

[ Internet ]
|
\/
[ Jelastic Resolver ]
|
\/
[ Your environment ]

This is because your environment only contains local IP addresses (10.x) so is not directly accessible across the Internet. So the Jelastic Resolver receives connections from the Internet and acts as a proxy to pass requests to your environment. It only proxies ports 80 and 443 (443 is only if the Jelastic Shared SSL feature is enabled).

Unfortunately this is not suitable for socket connections.

To use sockets you need to add a public IP address directly to your application server - then you may connect to the public IP and use any port that you like.

 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But it seems that this point is moot, because you converted your ServerSocket code to be a servlet instead - so you can get to it using regular HTTP.
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ulf,

Using HTTP, Can we send data between clients with our servlet acting as server?
 
Saloon Keeper
Posts: 28121
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:My point is, you should not start a new thread for HTTP communication. Servlets already do all that - change your server-side code to be a servlet instead, then you don't have to do all that HTTP handling.



Servlets are FORBIDDEN from creating threads, per the J2EE and JEE specifications. That's because (sorry, Ulf!), servlets don't "already do all that", but instead run under a thread that the server assigns from a Thread Pool. When the service method has completed, the thread is returned to the pool. Meaning that if the thread that the servlet ran under becomes baggaged with add-ons (such as child threads), it will be in violation of the basic assumption of a Pool that any object acquired from a Pool is fundamentally identical (interchangeable) to any other pool object. And makes it quite likely that at some point the server will leak resources and/or crash.

You can safely span threads in a servlet's init() method, but that won't help in handling a specific HTTP request.

HTTP is not really a very good chat protocol. In a chat server, ideally, you want something where messages can depart and arrive basically asynchronously over a continuous connection. HTTP is a protocol where you make a connection, send a request, receive a response, then close the connection. Repeat. Continuity has to be faked by using session IDs in cookies or as URL affixes. The HTTP-based chat servers you see are mostly using AJAX, not a server-side chat thread.

Upcoming improvements to JEE include websockets, which are a little more suited for such purposes, but websockets are a little short on standardization and support last time I looked.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HTTP is a client-server protocol - the servlet acts as the server in this case. It's certainly possible to exchange data between two clients that both access the same server.

Think of these forums here - I (a client) write a message (data) which is stored by the forum software (the servlet-based server). Then you (another client) can retrieve the data.
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ulf,

I have a doubt, can the server (say this forum) send the message (data) to only the specified person (me). I mean like our private moosages. How do these redirect them from one client (you) to another (me)?
 
Tim Holloway
Saloon Keeper
Posts: 28121
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got a little sloppy there. What I meant was continuous-connection protocol, such as traditional "client/server" applications use. Where the client is dedicated to a single server (usually) and (more importantly), the client connects to the server and stays connected for the duration of the session. HTTP does not do that. It connects only for the duration of the request/response cycle, then disconnects. An HTTP session, therefore does not consist of a single continuous connection, but instead as a series of short connections.

The overhead for establishing a connection, identifying and validating a user, and resolving server-side user session state is a lot more than what you'd get if you simply logged in and kept an open connection, even when you allow for various kludges intended to minimize the effect. Which is why HTTP is not an ideal chat protocol.
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tim,

We can use cookies to maintain session right?

And how do companies like Whatsapp or Viber provide messaging or chat over internet when http is not suitable for chat?
 
Tim Holloway
Saloon Keeper
Posts: 28121
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Crudely speaking, sessions are local data structures maintained on the server. To maintain the illusion of a continuous connection on a protocol that has none, the client and server pass an identifier back and forth (the session ID). The session ID is simply a hash key used by the server to locate the user's session data in a collection of sessions. This key has no meaning in itself, is more or less random (cannot be guessed by outside parties) and actually changes if you switch from http to https, even though the session itself does not.

The popular way to exchange this ID with the client is via cookies, but users may have cookies disabled, in which case, the session ID must be carried by the URL (jsessionid component).

Note that use of cookies is controlled in certain countries. You may be required to obtain user permission.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Partheban Udayakumar wrote:can the server (say this forum) send the message (data) to only the specified person (me). I mean like our private moosages. How do these redirect them from one client (you) to another (me)?


Let's be precise in wording. These forums work via HTTP, in which case the server can't really send anything to a client by itself. I access the server to write a PM to you, and some time later you access the server and can then retrieve the PM. So there is also no "redirect" - the data exists on the server, and can be accessed by clients. That's the request/response nature of HTTP.

HTTP, web browser and web server are not an environment like the mobile phone system with its SMS and push notifications. You can emulate that to some degree if both clients have an open browser window to the server, but it's a bit of a hack.
 
Tim Holloway
Saloon Keeper
Posts: 28121
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:
HTTP, web browser and web server are not an environment like the mobile phone system with its SMS and push notifications. You can emulate that to some degree if both clients have an open browser window to the server, but it's a bit of a hack.



In other words, the browser has to use AJAX or a plugin such as a Java Applet or Flash.
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ulf,

I understand your point but I have few doubts.

1) The client who has to retrieve data from the server has to use his GET method right? Correct me if I am wrong.

2) The client who posts the data has to login to the server first. so there will be a post method. How will he send his message now that is after he uses his POST method for connection.

Tim,

Thanks for the help. Think this as a messaging service over internet, do you think we still need to use AJAX for this?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1) The client who has to retrieve data from the server has to use his GET method right? Correct me if I am wrong.
2) The client who posts the data has to login to the server first. so there will be a post method. How will he send his message now that is after he uses his POST method for connection.


There are odd question that indicate that you may not really understand HTTP yet. Both sender and receiver need to log in, and yes, that would be done via POST. Sending the message would also involve a POST. But generally, interaction with a web site is a mix of many GETs and many POSTs - but that is not an important point when designing it.

Think this as a messaging service over internet, do you think we still need to use AJAX for this?


Need to? No. AJAX only works if you have an open web browser window to the server anyway. So if two clients have open windows to the server simultaneously, then the receiver can be notified of the message fairly quickly, and that would involve AJAX or one of the other methods Tim mentioned. But if the receiver does not have an open browser window, the message will remain on the server until the receiver connects again (minutes, hours or days later).
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ulf,

As I told you, I am totally new to JEE. Please enlighten me on this.
 
Warning! Way too comfortable! Do not sit! Try reading this tiny ad instead:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic