C# 到 Java 代码

发布于 2025-01-06 00:24:22 字数 1681 浏览 1 评论 0原文

我有一小段用 C# 给出的代码:

protected void Page_Load(object sender, EventArgs e)
    {
        try{
        string sFind = Request.QueryString["F"];
        string sFindBy = Request.QueryString["FB"];

        Response.ContentType = "text/xml";
        //Simulate Resonse (throw exeption on error) -------------------------------->

        XmlDocument docRet = new XmlDocument();
        XmlElement docElement = (XmlElement)docRet.AppendChild(docRet.CreateElement("Request"));
        generateResponseParamss(docElement, "Language", "heb");
        generateResponseParamss(docElement, "IMTSI", "972.001.000000081");
        generateResponseParamss(docElement, "Mobile", "0544490540");
        generateResponseParamss(docElement, "Email", "[email protected]");
        generateResponseParamss(docElement, "FirstName", "גדי");
        generateResponseParamss(docElement, "LastName", "גדיגדי");

        docRet.Save(Response.OutputStream);

        }catch(Exception ex){
            ShowErrorAsXml(Response, ex);
        }
    }

    public void generateResponseParamss(XmlElement docElement, string sParamsName, string sParamsValue)
    {
        XmlElement newNode = (XmlElement)docElement.AppendChild(docElement.OwnerDocument.CreateElement("Param"));
        newNode.SetAttribute("Name", sParamsName);
        newNode.SetAttribute("Value", sParamsValue);
    }

我想将此代码转换为 Java,我想我需要创建一个通过 URL 行(“GET”)接收参数的 servlet,但是 C# 代码行我最困难的是:

docRet.Save(Response.OutputStream);

哪个命令在 Java 中执行相同的操作。

I have a small piece of code I was given in C#:

protected void Page_Load(object sender, EventArgs e)
    {
        try{
        string sFind = Request.QueryString["F"];
        string sFindBy = Request.QueryString["FB"];

        Response.ContentType = "text/xml";
        //Simulate Resonse (throw exeption on error) -------------------------------->

        XmlDocument docRet = new XmlDocument();
        XmlElement docElement = (XmlElement)docRet.AppendChild(docRet.CreateElement("Request"));
        generateResponseParamss(docElement, "Language", "heb");
        generateResponseParamss(docElement, "IMTSI", "972.001.000000081");
        generateResponseParamss(docElement, "Mobile", "0544490540");
        generateResponseParamss(docElement, "Email", "[email protected]");
        generateResponseParamss(docElement, "FirstName", "גדי");
        generateResponseParamss(docElement, "LastName", "גדיגדי");

        docRet.Save(Response.OutputStream);

        }catch(Exception ex){
            ShowErrorAsXml(Response, ex);
        }
    }

    public void generateResponseParamss(XmlElement docElement, string sParamsName, string sParamsValue)
    {
        XmlElement newNode = (XmlElement)docElement.AppendChild(docElement.OwnerDocument.CreateElement("Param"));
        newNode.SetAttribute("Name", sParamsName);
        newNode.SetAttribute("Value", sParamsValue);
    }

And I would like to turn this code to Java, I think I need to create a servlet that receives parameter through the URL line ("GET"), but the C# code line that I'm most dificult with is:

docRet.Save(Response.OutputStream);

Which command do the same in Java.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陈甜 2025-01-13 00:24:23

查看本教程。 搜索 StreamResult结果 = new StreamResult(System.out);.

将该行更改为 new StreamResult(new PrintStream(response.getOutputStream()));

Look at this tutorial. Search for StreamResult result = new StreamResult(System.out);.

Change that line to have new StreamResult(new PrintStream(response.getOutputStream()));

今天小雨转甜 2025-01-13 00:24:23

有几种方法可以做到这一点。您使用什么类来存储 xml 响应?

There are several ways to do that. What class are you using for storing the xml response?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文