aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Communication between a Swing Client and a Servlet via HttpURLConnection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Communication between a Swing Client and a Servlet via HttpURLConnection " Watch "Communication between a Swing Client and a Servlet via HttpURLConnection " New topic
Author

Communication between a Swing Client and a Servlet via HttpURLConnection

Dupont Girard
Greenhorn

Joined: Mar 21, 2011
Posts: 24
Hello, i am trying to send a request to a servlet using HttpURLConnection

the request and JAXB part are perfectly sent, But my servlet do not react.

This is my XML without a <welcome-file>



On my Servlet i am just putting a System.out.println("hello") in the Post part in order to be sure that he receive something, but nothing.
I tried the url in a browser just to be sure that it's correct and it seems to be, because i got my System.out.println on the screeen

I will be thankfull if you help me to resolve this
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38873
    
  23
Don't know, but this is too difficult to be a "beginning" question, so I shall move it.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12788
    
    5


It is likely that your servlet was never loaded because you have not put your class in a package.

The JVM looks for classes that don't have a package in the "current" directory - something you have no control over in Tomcat.

Bill
Dupont Girard
Greenhorn

Joined: Mar 21, 2011
Posts: 24
Thanks for replying. Here is my new XML




but nothing happened
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12788
    
    5
Still likely that the servlet is never loaded. There are probably informative messages in the Tomcat log files.

How is this "Servlet" package defined in your servlet class?

What is the layout of directories and files for the "Serv" web application this servlet is supposed to live in?

Is this your first servlet project?

Bill
Dupont Girard
Greenhorn

Joined: Mar 21, 2011
Posts: 24
I already use Servlet for J2EE projects, but this is my fisrt time to try it with a Swing client.
First i lunch tomcat, even Client is sending requests, logs on apache don't change. "seems he receive nothing, but the url is correct because he run perfectly on a browser and my servlet "serv" react well"
i create a package "Servlet" wich contain my servlet "Serv" i agree with you if my naming is not good, but it's just to do some tests

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61224
    
  66

Roberto Garcias wrote:i agree with you if my naming is not good, but it's just to do some tests

Famous last words. Always follow good practices and always follow good naming conventions. It's a good habit to get into.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Dupont Girard
Greenhorn

Joined: Mar 21, 2011
Posts: 24
Okey Sherif, i will
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12788
    
    5
but the url is correct because he run perfectly on a browser and my servlet "serv" react well"


You know - if you had said in the original post that you can use a browser and that specific URL to get a response from the servlet, it would have saved a couple of days of misdirection.

The default method for HttpURLConnection is GET, to send data in the body of a request, you need to use POST - see the setRequestMethod()

Which methods does your servlet provide for?

Bill
Dupont Girard
Greenhorn

Joined: Mar 21, 2011
Posts: 24
Thanks for replying, i put some System.out.println on my Servlet's GET and POST methods, but it didn''t react, enven with setRequestMethod("POST") and setRequestMethod("GET")
Madhan Sundararajan Devaki
Ranch Hand

Joined: Mar 18, 2011
Posts: 312

I believe, your servlet works when invoked from a browser but does not when invoked from the client as the client may not be using the HTTP protocol. In your Swing client, please try to set the HTTP protocol for communicating with the servlet.


S.D. MADHAN
Not many get the right opportunity !
Dupont Girard
Greenhorn

Joined: Mar 21, 2011
Posts: 24
Thanks Devaki. I tried to shut down Tomcat, and run the Swing client, as i supposed it gives me exceptions instead of sending the request as he do when Tomcat runs



i don't know what should i add to be sure that i set the HTTP protocol
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12788
    
    5
Taking another look at your original code post, I see that you are sending a request but not doing anything to recover the response!

You should at least be doing getResponseCode - it could be trying to tell you something.

Bill
Dupont Girard
Greenhorn

Joined: Mar 21, 2011
Posts: 24
Yeah Bill you're right, I add an InutputStream and it works
Thanks
Roberti Lani
Greenhorn

Joined: Mar 23, 2011
Posts: 1
Dupont Girard wrote:Yeah Bill you're right, I add an InutputStream and it works
Thanks


I would like to see your code, because i have the same probleme

My swing :



I can see a xml in outputstream in the swing.
In getResponseCode i have 200 so it is ok. And i think in the servlet the connexion is ok, because i can open an other swing with the error generated by

InputStream is=req.getInputStream();
try {

JAXBContext jc;

jc = JAXBContext.newInstance("info");

Unmarshaller unmarshaller = jc.createUnmarshaller();
info.Utilisateur us = (info.Utilisateur) unmarshaller.unmarshal(is);
...

error : javax.xml.bin.unmarshallExption .. org.xml.sax.SAXParseException: Premature end of file.

thanks
majid nakit
Ranch Hand

Joined: Jun 26, 2001
Posts: 164
Hi,

Can somone please give me an example (swing,servlet) communication for login from swing ?

Thanks, your help is appreciated.

majid
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Communication between a Swing Client and a Servlet via HttpURLConnection