java 将表单数据从 jsp 发送到 Web 服务客户端 (SOAP)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12811813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending form data from a jsp to a web service client (SOAP)
提问by Div
Basically I have a web service connected to a database, that has an insert method. I've created a registration form in a jsp and I need to send data the user inserted in the registration.jsp form to my web service. So I used a web service client invocation as the action to the registration.jsp when clicking the submit button.
基本上我有一个连接到数据库的 Web 服务,它有一个插入方法。我已经在 jsp 中创建了一个注册表单,我需要将用户插入到 registration.jsp 表单中的数据发送到我的 Web 服务。因此,当单击提交按钮时,我使用 Web 服务客户端调用作为对 registration.jsp 的操作。
I dont know how to get the data the user entered in the form and pass it to the web client who in turn insert it in my web service database.
我不知道如何获取用户在表单中输入的数据并将其传递给 Web 客户端,后者又将其插入到我的 Web 服务数据库中。
Here are my codes:
这是我的代码:
<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>
And this is the code for the web client upon clicking the submit button of the
这是单击 Web 客户端的提交按钮后的代码
registration( action="Wclient2.jsp" method="POST")
注册(动作=“Wclient2.jsp”方法=“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>
I dont know how to get the parameters for fname from the registration.jsp to put in the web client. When I write:
我不知道如何从registration.jsp 中获取fname 的参数放入Web 客户端。当我写:
java.lang.String fname = request.getParameter("fname");
java.lang.String fname = request.getParameter("fname");
to get the value of textfield fname into my web service, in my database a row is created but NULL is inserted instead of the values my user entered in the form.
要将 textfield fname 的值放入我的 Web 服务中,在我的数据库中创建了一行,但插入了 NULL 而不是我的用户在表单中输入的值。
Please help.
请帮忙。
回答by Satheesh Cheveri
I dont see any issue with your client side code(html/jsp) if you use request.getParameter("fname") as long you provide input to FirstName. To isolate the issue, print the value of fname in your jsp
如果您使用 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);
If this prints your input correctly, you would need to see service implementation to check it reads and process the input correctly. If it is fname is null , then we can come back to the HTML/JSP to have second look.
如果这正确打印了您的输入,您将需要查看服务实现以检查它是否正确读取和处理输入。如果 fname 为 null ,那么我们可以回到 HTML/JSP 再看看。
Update: To see the request in details, you could use below code
更新:要详细查看请求,您可以使用以下代码
Enumeration reqParams= request.getParameterNames();
while (reqParams.hasMoreElements()) {
String key= (String) reqParams.nextElement();
String val = request.getParameter(element);
System.out.println(" Key ==> "+key+" , Value ==> "+val);
}
Try this and lets see if you get values for any of the input your enterd in your html form.
试试这个,看看你是否得到了你在 html 表单中输入的任何输入的值。
Satheesh
萨蒂什
回答by Bob
This isn't really my area of expertise, but instead of calling action="Wclient2.jsp" you could call action="pass.php" then create a php file to grab the variables and redirect to Wclient2.jsp passing your variables in a query string.
这不是我真正的专业领域,但不是调用 action="Wclient2.jsp" 你可以调用 action="pass.php" 然后创建一个 php 文件来获取变量并重定向到 Wclient2.jsp 传递你的变量一个查询字符串。
For example in the pass.php file:
例如在 pass.php 文件中:
$fname = $_POST['fname'];
$lname = $_POST['lname'];
header( 'Location: http://www.yoursite.com/path/Wclient2.jsp?fname=' .fname. '&lname=' .lname );
That might be a really weird (or newbie) way to accomplish it, but just of the top of my head I do know that it works. Hopefully someone else will have a more appropriate answer, but in case no one replies quickly enough, I hope this helps a little bit :)
这可能是一种非常奇怪(或新手)的方式来完成它,但只是在我的脑海中,我确实知道它有效。希望其他人会有更合适的答案,但如果没有人回复得足够快,我希望这会有所帮助:)