调用远程Java Servlet [英] Calling a Remote Java Servlet

查看:202
本文介绍了调用远程Java Servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含表单的jsp页面,它应该将表单数据发送到远程servlet,然后计算它,然后将其作为XML返回。它工作正常,但目前我正在创建一个只适用于本地servlet的实例和调度程序,而我希望它可以使用远程servlet。

I have a jsp page which holds a form, it is supposed to send off the form data to a remote servlet, which calculates it, and then returns it as XML. It works, but at the moment I'm creating an instance and dispatcher which only works with local servlets whereas I want it to work with a remote servlet.

我之前是告诉我 HTTPClient 会这样做,但这件事已经变得如此令人头疼,它对于我想要做的事情来说,这似乎是一种彻底的过度杀伤力。必须有一些简单的方法,而不是使用所有这些jar组件和依赖项?

I was previously told that HTTPClient would do this, but this thing has become such a headache and it seems like a complete overkill for what I want to do. There must be some simple method as opposed to faffing around with all these jar components and dependencies?

请尽可能给出示例代码,我真的是Java的新手,更多的PHP家伙:P

Please give sample code if possible, I'm really a complete novice to Java, much more of a PHP guy :P

推荐答案

借助一些在线资源计算出来。必须首先收集提交的值(request.getParamater(bla)),构建数据字符串(URLEnconder),启动URLConnection并告诉它打开与指定URL的连接,启动OutputStreamWriter然后告诉它添加数据字符串(URLEncoder),然后最终读取数据并打印出来......

Figured it out with the help of some online resources. Had to first collect the submitted values (request.getParamater("bla")), build the data string (URLEnconder), start up a URLConnection and tell it to open a connection with the designated URL, startup an OutputStreamWriter and then tell it to add the string of data (URLEncoder), then finally read the data and print it...

以下是代码的要点:

String postedVariable1 = request.getParameter("postedVariable1");
String postedVariable2 = request.getParameter("postedVariable2");

//Construct data here... build the string like you would with a GET URL     
String data = URLEncoder.encode("postedVariable1", "UTF-8") + "=" + URLEncoder.encode(postedVariable1, "UTF-8");
data += "&" + URLEncoder.encode("postedVariable2", "UTF-8") + "=" + URLEncoder.encode(submitMethod, "UTF-8");

    try {
        URL calculator = new URL("http://remoteserver/Servlet");
        URLConnection calcConnection = calculator.openConnection();
        calcConnection.setDoOutput(true);
        OutputStreamWriter outputLine = new OutputStreamWriter(calcConnection.getOutputStream());
        outputLine.write(data);
        outputLine.flush();


        // Get the response
        BufferedReader streamReader = new BufferedReader(new InputStreamReader(calcConnection.getInputStream()));
        String line;
        //streamReader = holding the data... can put it through a DOM loader?
        while ((line = streamReader.readLine()) != null) {
            PrintWriter writer = response.getWriter();
            writer.print(line);
        }
        outputLine.close();
        streamReader.close();

    } catch (MalformedURLException me) {
        System.out.println("MalformedURLException: " + me);
    } catch (IOException ioe) {
        System.out.println("IOException: " + ioe);
    }

这篇关于调用远程Java Servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆