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 7 of 7

Thread: A different sort of POST method im implementing... Im a newbie ,please help.

  1. #1
    Junior Member
    Join Date
    Aug 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default A different sort of POST method im implementing... Im a newbie ,please help.

    Hi all,

    Im writing a servlet to communicate to the Gateway..I'll explain it with the code
     
     public void doGet(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException {
            /* Initial Declarations for all the values that are 
             * necessary...
             * It has to be present or the catch blok will execute to 
             * result in errors.
             */
     
            String query_string = new String();
            String and_split[] = new String[15];
            String equal_splt = new String();
            String host_no[] = new String[3];
            String dest_no[] = new String[3];
            String med_code[] = new String[3];
            String sys_time[] = new String[3];
            res.setContentType("text/html");
     
            query_string = req.getQueryString();
            try {
            and_split = query_string.split("&");
             if (and_split.length == 4) {
                String send_no = and_split[0].toString();
                String rec_no = and_split[1].toString();
                String code = and_split[2].toString();
                String time = and_split[3].toString();
            host_no = and_split[0].toString().split("=");
            dest_no = and_split[0].toString().split("=");
            med_code = and_split[0].toString().split("=");
            sys_time = and_split[0].toString().split("=");
            }
            }
            catch (NullPointerException e) {
                System.out.println("error");
            }
             handle_db(host_no, dest_no, med_code, sys_time);
     
            }

    --> My first one is(Coming from ruby, after the .split("&") i converted the resulting array with .toString() as i needed to further split it)
    That didnt work.

    --> Secondly this is the get request.. Now
     
    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
     
        }
    ---> the doGet() forwards the values to a function called handle_db() that does further processing...
    ----> This generates a value(Single value)
    ----> How do i post this value back to the Gateway?
    eg, (Supposing the gateway url is ; www.testgateway.com/somepath)
    how do i post the data back to that particular url... Secondly.. How do i construct the url..)


    Im so far familiar with POST using forms.. But now this has to be done through url...
    Dont have much idea can anyone please help.

    Thanks
    Last edited by wrapster; August 7th, 2009 at 11:19 AM. Reason: incomplete


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: A different sort of POST method im implementing... Im a newbie ,please help.

    Hello,

    First of all, reading parameter from a URL using Java and HttpServletRequest is done in a different way.

        final String parameter = request.getParameter("parameterName");

    Second of all, you dont need to initialise a string by doing new String().

    Third, to write out a response you simple output stuff on the response like so.

        response.getWriter().write("Write back anything you like here");
        response.getWriter().flush();

    // Json

  3. #3
    Junior Member
    Join Date
    Aug 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: A different sort of POST method im implementing... Im a newbie ,please help.

    I realized the first part after I started using netbeans....
    the getParameter is what i use.
       response.getWriter().write("uname=valid");
        response.getWriter().flush();

    Secondly youre telling me that if the response method is so written ,where do i specify the url for the destination?
    Or is it that i use " .write(cPanelŽ) "
    something like this, although im assuming its wrong....

    Scenario..
    1 --> cPanelŽ
    2 ---> cPanelŽ

    Now this is how the gateway will actually be sending me the data(through url)
    1 is used by gateway to send data to me...
    Here How do i accept and extract the data? (so far i've dealt with Forms for this purpose...)I've never directly added it to the url...I know if i do so I'll have to use the GET method but how do i ensure i send this url to 1 from 2 .. 1 is where my app is running and thats where the data will be extracted.

    After the data is received I need to process the data ,now i get a result saying "uname=valid"...
    I need to send it back to 2 , how do I set it up now?

    gateway -------(sends data in the format specified in 1) -------------> my app

    gateway <-----(my app sends uname=valid in the format specified in 2) --------- my app

    Thats all i need to know...
    can you please help me?

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: A different sort of POST method im implementing... Im a newbie ,please help.

    No, basically you have to think of this somewhat different, the gateway in this case is acting like a web browser. It connects to the server by a url with parameters in it. The server then process these parameters and return a website. This website could be an XML document or just a plain text document with your return in it. In the gateway you can then parse the response from the server and do whatever you want with it.

    // Json

  5. #5
    Junior Member
    Join Date
    Aug 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: A different sort of POST method im implementing... Im a newbie ,please help.

    Yeah, I got that understanding by reading up on a few things...

    and then I was able to resolve my issue.
    I wrote a simple server( my own psuedo gateway for now) and a client...
    The gateway sends some data to the client it verifies and responds back...
    Im able to collect the response at the gateway...

    This is what I used response.sendRedirect(<gateway url?name=value>) I got this working...
    But by default sendRedirect sends the data as GET ...I dont want the data to be visible...
    and if i use the RequestDispatcher().forward thats wont work as its specific to the context within the servlet....


    So is there a way I can send the data to the gateway (using sendRedirect() ) as POST?
    I dont want to use forms....

    Regards...

  6. #6
    Junior Member
    Join Date
    Oct 2009
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: A different sort of POST method im implementing... Im a newbie ,please help.

    so inside the doPost method we can calll do Get method
    vice versa
    as per u'r requirement u can use.
    for sending request back u can use RequestDispatcher interface

  7. #7
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: A different sort of POST method im implementing... Im a newbie ,please help.

    To send this on as a post you'd have to open up a URLConnection of some sort or use the Apache HTTPClient to create a POST http client and then issue the post to another url.

    Redirect is a response sent back to the browser telling it to do a GET, you cannot do this for a POST.

    // Json

Similar Threads

  1. Replies: 8
    Last Post: August 6th, 2009, 01:35 PM
  2. [SOLVED] Implementing push button
    By IDK12 in forum AWT / Java Swing
    Replies: 2
    Last Post: July 10th, 2009, 10:13 AM
  3. I'm a newbie
    By r12ki in forum Member Introductions
    Replies: 2
    Last Post: June 1st, 2009, 06:38 AM
  4. Insert sort algorithm
    By Alysosh in forum Algorithms & Recursion
    Replies: 1
    Last Post: May 26th, 2009, 09:28 AM
  5. Problem while implementing a basic user interface menu
    By Rastabot in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 3rd, 2009, 04:38 PM