Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: problem with a java client connecting to servlet

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    29
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default problem with a java client connecting to servlet

    Hi all,
    I am trying to create my first servlet with a basic client that sends some data to the servlet and gets a reply back.
    I want to do that in order do go further where i want to develop an android app that communicates with a server. I actually did a small application that does prety much what i need, but i used sockets, and there is no way i can find to post that java socket server on the internet in order to connect with my android application. So from what i have read online, i figured that i need a servlet in order to have the ability to publish it online.
    Most of the servlet examples i found were communicating with a web browser (something that i dont need) and i found this tutorial with a client sending a string to the server and gets a reply.
    This is the servlet code from the tutorial:
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    public class HelloJavaWorld extends HttpServlet {
        private final static String _USERNAME = "username";
     
        protected void doPost( HttpServletRequest request, HttpServletResponse response )
            throws ServletException, IOException {
     
            PrintWriter out = response.getWriter();
            String username = request.getParameter( _USERNAME );
     
            response.setContentType("text/html");
            out.println("");
            out.println("");
                out.println("HelloWorld");
            out.println("Hello " + username + "!");
        } 
    }
    and this is the client
    import java.io.*;
    import java.net.*;
    public class ContactServlet {
        public static void main( String [] args ) {
     
            try {            
                URL url = new URL(" http://127.0.0.1:8080/test/FileCounter");
                URLConnection conn = url.openConnection();
                conn.setDoOutput(true);
     
                BufferedWriter out = 
                    new BufferedWriter( new OutputStreamWriter( conn.getOutputStream() ) );
                out.write("username=javaworld\r\n");
                out.flush();
                out.close();
                BufferedReader in = 
                    new BufferedReader( new InputStreamReader( conn.getInputStream() ) );
     
                String response;
                while ( (response = in.readLine()) != null ) {
                    System.out.println( response );
                }
                in.close();
            }
            catch ( MalformedURLException ex ) {
                // a real program would need to handle this exception
            }
            catch ( IOException ex ) {
    System.out.println("ex.getMessage());
     
                // a real program would need to handle this exception
            }
        }
    }

    The thing is that i can not sent data from the client. The server is working OK, from a browser i get this
    HTML Code:
    HelloWorld Hello null!
    since i dont sent anything to get a reply.
    From the java client i get this exception
    Server returned HTTP response code: 405 for URL: http://127.0.0.1:8080/test/FileCounter
    If i delete all the commands that have to do with the transmision from the client, i.e.
      BufferedWriter out = 
                    new BufferedWriter( new OutputStreamWriter( conn.getOutputStream() ) );
                out.write("username=javaworld\r\n");
                out.flush();
                out.close();
    I am geting the same result as the web browser
    HelloWorld
    Hello null!

    Does anyone have ANY idea why this is happening? I dont know much about servlets (as you might guessed) and a ready tutorial shouldn't give me this much trouble.
    thanks in advance,
    ilias


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 832 Times in 772 Posts
    Blog Entries
    5

    Default Re: problem with a java client connecting to servlet

    Is the HelloJavaWorld class mapped to the name test/FileCounter? The default behavior for doPost is to return a 405 - based upon the behavior alone it seems as if the result you see in browse (GET) and without writing to the OutputStream (GET) is from a call to the doGet method of a servlet named FileCounter. Presumably, this servlet contains no doPost so when you try to access it this way by writing to the OutputStream (POST) a 405 is returned

  3. #3
    Junior Member
    Join Date
    Aug 2014
    Posts
    29
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: problem with a java client connecting to servlet

    Thanks, that did the trick. while i was trying to fix it, i must have switched doPost with doGet. now i am at least reading back from the servlet. BUT, for some reason the value returned to the client is null.
    so the result is just
    HelloWorld
    Hello null!

    i have figured out a way to print the variables of the servlet, (using system,out.println() saving to tomcat8-stdout.(date).log on the log folder of tomcat)
    and it turns out that
    request.getParameter( _USERNAME )
    returns null for some reason.
    So there must be a problem transmitting the data to the servlet.
    any ideas?
    Last edited by skarosg3; September 15th, 2014 at 02:07 AM.

  4. #4
    Junior Member
    Join Date
    Aug 2014
    Posts
    29
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: problem with a java client connecting to servlet

    I dont get it!
    request.getContentLength()
    returns the actual length of the string transmitted, but there is no way to get the String, since
    request.getAttribute(_USERNAME)
    returns null!

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 832 Times in 772 Posts
    Blog Entries
    5

    Default Re: problem with a java client connecting to servlet

    Read about http GET and http POST protocols. It is how you send the data that matters: depending upon which you choose to use- one expects parameters via query string in the URL and the other via forms.

  6. #6
    Junior Member
    Join Date
    Aug 2014
    Posts
    29
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: problem with a java client connecting to servlet

    So stupid of me!!
    I have been working all this time on my 10' netbook, and didnt see that i changed one crusial line!
    now that i sat on my proper desktop i saw the mistake in 2 mins!!!
    Thanks alot.
    Do you know if this is the best way to communicate between a server and an android client?
    I have been trying to see what is the best protocol, but i just cant figure it out.
    I should post a line on the android forum, i guess.

    Thanks again

Similar Threads

  1. Replies: 1
    Last Post: October 7th, 2013, 11:36 AM
  2. java Client Server program by using socket problem
    By Mad Engineer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 31st, 2013, 10:57 AM
  3. Problem connecting email-client
    By Mustelmia in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 9th, 2013, 05:01 PM
  4. Connecting to my torrent client (I use it for legal stuff only!)
    By jdruwe in forum Java Theory & Questions
    Replies: 0
    Last Post: August 9th, 2012, 10:16 AM
  5. ha-jndi client is servlet
    By supriya ramjee in forum Web Frameworks
    Replies: 0
    Last Post: July 31st, 2009, 02:02 AM