【问题标题】:Sending form data from a jsp to a web service client (SOAP)将表单数据从 jsp 发送到 Web 服务客户端 (SOAP)
【发布时间】:2012-10-10 15:37:31
【问题描述】:

基本上我有一个连接到数据库的网络服务,它有一个插入方法。 我在 jsp 中创建了一个注册表单,我需要将用户插入到 registration.jsp 表单中的数据发送到我的 Web 服务。因此,我在单击提交按钮时使用了 Web 服务客户端调用作为registration.jsp 的操作。

我不知道如何获取用户在表单中输入的数据并将其传递给 Web 客户端,然后再将其插入到我的 Web 服务数据库中。

这是我的代码:

    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title> Registration</title>
    </head>
    <body>
        <h1> Welcome</h1>


        <form name="test" action="Wclient2.jsp" method="POST" enctype="multipart/formdata">

                   <table border="0">
                <thead>
                    <tr>
                        <th>Register Here</th>
                      <th></th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>First Name:</td>
                        <td><input type="text" name="fname" value="" size="50" /></td>
                    </tr>
                    <tr>
                        <td>Last Name:</td>
                        <td><input type="text" name="lname" value="" size="50" /></td>
                    </tr>
                    <tr>
                        <td>Email:</td>
                        <td><input type="text" name="email" value="" size="50" /></td>
                    </tr>
                    <tr>
                        <td>Password:</td>
                        <td><input type="password" name="password" value="" size="50"/>      </td>
                    </tr>

                    <tr>
                        <td></td>
                        <td> <input type="submit" value="submit" name="submit" /> </td>
                    </tr>
                </tbody>
            </table>

        </form>
    </body>
</html>

这是网页客户端点击提交按钮后的代码

注册(action="Wclient2.jsp" method="POST")

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title> Enroll</title>
    </head>
    <body>
        <h1> Welcome</h1>

        <form name="test"<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>

           <%-- start web service invocation --%><hr/>
        <%        

            try {
                mypack.Bioweb_Service service = new mypack.Bioweb_Service();
                mypack.Bioweb port = service.getBiowebPort();
                // TODO initialize WS operation arguments here

                java.lang.String fname = "";
                java.lang.String lname = "";
                java.lang.String email = "";
                java.lang.String password = "";

                // TODO process result here

                java.lang.String result = port.insert(fname, lname, email, password);
                out.println("Result = " + result);

            } catch (Exception ex) {
                // TODO handle custom exceptions here
            }
        %>
        <%-- end web service invocation --%><hr/>

    </body>
</html>

我不知道如何从registration.jsp 中获取fname 的参数以放入Web 客户端。 当我写的时候:

java.lang.String fname = request.getParameter("fname");

为了将文本字段 fname 的值导入我的 Web 服务,在我的数据库中创建了一行,但插入的是 NULL,而不是用户在表单中输入的值。

请帮忙。

【问题讨论】:

    标签: java jsp soap netbeans-7 webservice-client


    【解决方案1】:

    如果您使用 request.getParameter("fname") 只要您向 FirstName 提供输入,我看不出您的客户端代码 (html/jsp) 有任何问题。要隔离问题,请在 jsp 中打印 fname 的值

    String fname =request.getParameter("fname") ;
    System.out.println("Value of first name " +fname) // this will print your console
    java.lang.String result = port.insert(fname, lname, email, password);
    

    如果这能正确打印您的输入,您需要查看服务实现以检查它是否正确读取并处理输入。如果是 fname is null ,那么我们可以回到 HTML/JSP 再看看。

    更新: 要详细查看请求,您可以使用以下代码

    Enumeration reqParams= request.getParameterNames();
    while (reqParams.hasMoreElements()) {
        String key= (String) reqParams.nextElement();
        String val = request.getParameter(element);
        System.out.println(" Key ==> "+key+" , Value ==> "+val);
    
    }
    

    试试这个,看看你是否获得了你在 html 表单中输入的任何输入的值。

    萨西斯

    【讨论】:

    • 嗨,satheesh,感谢您的回复。我试过你说的,但是,它仍然给我NULL,我不明白为什么!输出:名字的值 null 你能帮帮我吗?
    • 编辑了我之前的答案;但是我没有发现您的代码有任何问题,除了您的 jsp 中有一个不正确的标签 (
      ),这是微不足道的,不应该引起任何问题.
    • 再次感谢,我删除了表单并创建了另一个,我想确实是一个不正确的标签,现在正在保存。 :)
    【解决方案2】:

    这不是我真正的专业领域,但您可以调用 action="pass.php" 而不是调用 action="Wclient2.jsp" 然后创建一个 php 文件来获取变量并重定向到 Wclient2.jsp 传递查询字符串中的变量。

    例如在 pass.php 文件中:

    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    
    header( 'Location: http://www.yoursite.com/path/Wclient2.jsp?fname=' .fname. '&lname=' .lname );
    

    这可能是一种非常奇怪(或新手)的方式来完成它,但我确实知道它有效。 希望其他人会有更合适的答案,但如果没有人足够快地回复,我希望这会有所帮助:)

    【讨论】:

    • 您好,感谢您的回复,但我不能在这个中使用 php,必须是 jsp/html。我确定这是我犯的一些非常愚蠢的错误,也许只是一行代码,但我似乎找不到它! :(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-21
    • 2013-04-11
    • 2021-12-07
    • 2011-10-11
    • 1970-01-01
    • 1970-01-01
    • 2012-08-29
    相关资源
    最近更新 更多