Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Communication between a Swing Client and a Servlet via HttpURLConnection

 
Dupont Girard
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49812
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replying. Here is my new XML




but nothing happened
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65122
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dupont Girard
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okey Sherif, i will
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 312
Java MS IE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dupont Girard
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah Bill you're right, I add an InutputStream and it works
Thanks
 
Roberti Lani
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

Thanks, your help is appreciated.

majid
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic