Link to home
Start Free TrialLog in
Avatar of FTIISD
FTIISD

asked on

servlet - forward post to another server

Hello,

I have the following servlet that is called from an html form using post:

package com.test.web;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class OrderSelect extends HttpServlet {
      public void doPost(HttpServletRequest request,
                                    HttpServletResponse response)
                                    throws IOException, ServletException {
            
            String sSize = request.getParameter("size");
            String sType = request.getParameter("types");
            String sProductInfo = request.getParameter("productinfo");
            String qty = request.getParameter("qty");

            double fPrice;            
            String sProduct;
            
            sProduct = "Type: " + sType + "  Size: " + sSize + "  Shirt: " + sProductInfo;
            
            if(sType == "Unisex")
            {
                  fPrice = 20.00;
            }else{
                  fPrice = 25.00;
            }
            
            if(sSize == "XX-Large + $3.50 USD" || sSize == "XXX-Large + $3.50 USD")
            {
                  fPrice += 3.50;
            }

            //request.setAttribute("price", fPrice.toString());
            request.setAttribute("product", sProduct);
            request.setAttribute("userid", "uid123");
            
            RequestDispatcher view = request.getRequestDispatcher("http://www.123site.com/cf/add.cfm");

            view.forward(request, response);
      }
}

Question 1:
I understand that the above code is meant to forward both the request and the response object to a jsp file, on my local server.  What i need to do (I believe) is post all of these variables to another server using the post method. (I am assuming this is the same as forwarding both the request and the response objects to a remote server.  This server is not using jsp, but just needs a request the same as if i was posting it through an html form.

Question 2:
Why can't I cast a double into a string using: request.setAttribute("price", fPrice.toString()); and how do i do it?

I am new to this so please be gentle.

Thanks for your help.
Avatar of bloodredsun
bloodredsun
Flag of Australia image

Question 1:

Request Dispatcher only works when you are forwarding to a page/servlet inside your own server. To post to another server try using HttpUrlConnection.

Question 2:
double is a primitive (i.e. it has no methods) so you cannot call toString() on it. A quick work around would be this
 request.setAttribute("price", fPrice + "" );
ASKER CERTIFIED SOLUTION
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial