Meaningless Drivel is fun!*
The moose likes Tomcat and the fly likes Get String from servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Products » Tomcat
Bookmark "Get String from servlet" Watch "Get String from servlet" New topic
Author

Get String from servlet

Andrew Cho
Ranch Hand

Joined: Jun 29, 2011
Posts: 50
Hey all,

I would like to get a string from a servlet to a java application from a different server.
Here is the servlet code:


and here's the application:


When I run it I get

Any ideas?
Andrew
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Just send the string. That's what HTTP is good at. Don't mess about with object serialization.
Andrew Cho
Ranch Hand

Joined: Jun 29, 2011
Posts: 50
Ok thanks.
I don't really know how to just send a string from a servlet though.
Would that just look like

?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Well, no, that won't even compile. Just send the string to the response's output stream. (Which you already know how to find.)
Andrew Cho
Ranch Hand

Joined: Jun 29, 2011
Posts: 50
Would it be like this?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

I don't want to play a guessing game here. I already recommended not using object serialization, so no, code which uses object serialization wouldn't be what I would recommend. Really. Just send the string to the response. Don't try to make it so complicated.
Andrew Cho
Ranch Hand

Joined: Jun 29, 2011
Posts: 50
Thanks Paul. Sorry for being such a begineer. So with this servlet in the server, what should I use to call it? the code I have in my application does not work with the servlet.
I Still get the invalid stream header error. I read somewhere that if I were to only use either the input or the output, I wouldn't have to deal with the header, but that appears to be false
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Andrew Cho wrote:Thanks Paul. Sorry for being such a begineer. So with this servlet in the server, what should I use to call it?


You should use a URL which refers to the servlet. Yikes! You seem to have interpreted that as to use a URL which refers to the class which implements the servlet:


You don't want to do that. In fact you don't even want to put the class in a place where it can be accessed from the web.

But this is getting ridiculous. This process of you making wild guesses and us trying to correct them isn't working. It's pretty clear that you haven't read any tutorials about servlets at all. I would seriously recommend you go off and find some. Hopefully they will have examples which you can try out for yourself to get the hang of it. As it is you're speeding down the highway while asking "Isn't there some way to stop this truck?"
Andrew Cho
Ranch Hand

Joined: Jun 29, 2011
Posts: 50
ok paul. I read up on servlets and got mine running on localhost just how I want it. However, when I put it on the web server, Tomcat will have to be running all of the time right? and if the server needs to reboot, tomcat would have to be restarted? the problem with this is that i don't manage the server that this servlet is going onto, so i couldn't constantly be starting up tomcat
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Yes, all of that is correct. However if you have a business requirement to have Tomcat always running, or as near as possible, then you would have to arrange that with the owner of the server. Having Tomcat start up automatically isn't difficult, at least not to someone who runs servers for a living.
Andrew Cho
Ranch Hand

Joined: Jun 29, 2011
Posts: 50
OK good to know. Do you mean it's easy to have tomcat startup when the server is started up or when the servlet needs to be called? the only reason I ask is because if the server stays on but tomcat is shut down for some reason, then it would stay down until manually restarted or the server is rebooted, right?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61313
    
  66

You have complete control over stopping and starting tomcat. It's just a program. You do not have to shutdown or reboot the server to stop and start Tomcat.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Andrew Cho
Ranch Hand

Joined: Jun 29, 2011
Posts: 50
Oh sorry, I knew that. I was just wondering if there was a way to startup tomcat when a servlet has been attempted to be accessed. But how would I go about having tomcat start during the server startup? Also it seems like tomcat is set to exit on close, but having the window open constantly would be easy to close, therefore shutting down tomcat. is there anyway to close the window and keep tomcat running?

thanks again for your help guys.

Andrew
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16101
    
  21

If nothing is actively listening on port 8080 (or whatever port you've assigned to Tomcat), there's no way to startup Tomcat just because a request has been directed to that port. In order for a request to be processed, it needs to be routed to an app by the OS network stack.

There are actually 2 ways of doing that.

The first way is for an application to already be listening and waiting for incoming traffic.

The second way is to delegate the listening job to a dispatcher app. This app does the listening for you, then launches the target server app and routes the request to the launched app. This approach is known as the "super-server" approach, and on Unix/Linux, is handled by a daemon known as inetd (or xinetd in newer versions).

The super-server approach isn't viable for Tomcat, since it would require launching Tomcat on every incoming request, and even a simple app in a minimal Tomcat requires a LOT of overhead to setup and start. The hits on the hardware would be major and the delays would be intolerable. The super-server isn't used that much these days since the discrepancy between launching apps versus having them listen is now significant and the need to spare memory by not running listeners full-time is minimal. The super-server was never intended to manage heavy-duty apps, anyway. It's best for lightweight infrequent processes.

So, to summarize, you do need a Tomcat up and running!

When you start talking about closing windows and having Tomcat shutdown, you're pretty much admitting that you're running Tomcat under Microsoft Windows. There are ways in Windows to run Tomcat without the need to keep it in an open Window. Several of them, in fact, including options on the START command.

The most popular way, however, is to run Tomcat as a Windows Service. The additions required to run Tomcat as a Windows Service are packaged with the Windows version of Tomcat. Using that feature, you can start Tomcat automatically when the machine starts up. Or manually from the Windows service manager, if you prefer.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
GeeCON Prague 2014
 
subject: Get String from servlet