从 JSP(form) 获取信息到 Java Bean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3563387/
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
Get information from JSP(form) to Java Bean
提问by Reddy
I have a JSP form. My requirement is to get this form's data and create a java beans object on the server side.
我有一个 JSP 表单。我的要求是获取此表单的数据并在服务器端创建一个 java bean 对象。
Example:my form has fields like Name, SSN, EMAIL & Phone Number
示例:我的表单包含姓名、SSN、电子邮件和电话号码等字段
public class Test {
long ssv= 1282199222991L;
long phone= 4082224444L;
String email = "[email protected]";
String name="abcdef"
}
From the knowledge i have , i was thinking to create bean object using servlet, which is created out of JSP, at the server side. My question is how i access this "server created" servlet for getting the variables data?
根据我的知识,我想在服务器端使用由 JSP 创建的 servlet 创建 bean 对象。我的问题是我如何访问这个“服务器创建的”servlet 来获取变量数据?
PS: I am beginner in web programing & server side scripting. Please let me know if the question is not clear. Any information would very valuable for me. Please do let me know if i am thinking in the right way. Trailer Thanks!
PS:我是网络编程和服务器端脚本的初学者。如果问题不清楚,请告诉我。任何信息对我来说都是非常有价值的。请让我知道我的想法是否正确。预告片 谢谢!
采纳答案by BalusC
The JSP should indeed submit the form to the servlet. The servlet should indeed create the bean and use it to transfer the submitted data through the necessary layers (save in database using DAO class and/or redisplay in result JSP after submit).
JSP 确实应该将表单提交给 servlet。servlet 确实应该创建 bean 并使用它通过必要的层传输提交的数据(使用 DAO 类保存在数据库中和/或提交后在结果 JSP 中重新显示)。
Here's a kickoff example of how the JSP should look like:
下面是 JSP 应该是什么样子的启动示例:
<form action="register" method="post">
<p><input name="name">
<p><input name="email">
<p><input name="phone">
<p><input name="ssv">
<p><input type="submit">
</form>
And here's how you could write the doPost()
method of the servlet which is listening on an url-pattern
of /register
.
而这里的你怎么能写在doPost()
这上收听该servlet的方法url-pattern
的/register
。
String name = request.getParameter("name");
String email = request.getParameter("email");
String phone = request.getParameter("phone");
String ssv = request.getParameter("ssv");
// Do the necessary validations here and then ..
User user = new User(name, email, phone, Long.valueOf(ssv));
// Now you have an User javabean with the necessary information.
// Do with it whatever you want. E.g. saving in database.
userDAO.save(user);
See also:
也可以看看:
回答by Boris Pavlovi?
Take a look at the tutorial on Handling Form Data
看一看处理表单数据的教程
回答by Ezeilo Uchenna
Well its been a long time since this question was asked. However, I believe this is the required answer.
自从问这个问题以来已经很长时间了。但是,我相信这是必需的答案。
Start with a HTML form to collect the data as below (I used only username and email)
从 HTML 表单开始收集如下数据(我只使用了用户名和电子邮件)
<HTML>
<BODY>
<FORM METHOD=POST ACTION="process.jsp">
Name <INPUT TYPE=TEXT NAME=Name SIZE=20><BR>
Email <INPUT TYPE=TEXT NAME=email SIZE=20><BR>
<P><INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>
To collec this data in a bean create a class e.g. MyData as shown
要在 bean 中收集此数据,请创建一个类,例如 MyData,如图所示
package datapackage;
public class MyData{
String name;
String email;
public void setName( String value )
{
name = value;
}
public void setEmail( String value )
{
email = value;
}
public String getName() { return name; }
public String getEmail() { return email; }
}
Make sure it is compiled and available to your application's environment
确保它已编译并可用于您的应用程序环境
Then create a JSP page process.jsp to receive this data
然后创建一个JSP页面process.jsp来接收这个数据
<jsp:useBean id="data" class="datapackage.MyData" scope="session"/>
<jsp:setProperty name="data" property="*"/>
<HTML>
<BODY>
You can output the data here too with e.g. <%= data.getName() %> but that was not your question
</BODY>
</HTML>
At this point you have an object in session (can be in page, application and request scopes too if you set the scope parameter on the jsp page to any of them). Any servlet that is called while the user still has a valid session after filling the form can access the object from session like this;
此时,您在会话中有一个对象(如果您将 jsp 页面上的 scope 参数设置为其中任何一个,也可以在页面、应用程序和请求范围内)。在用户填写表单后仍有有效会话时调用的任何 servlet 都可以像这样从会话访问对象;
package datapackage;
import java.io.IOException;
import javax.servlet.http.*;
public class SalesServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
HttpSession session = request.getSession();
MyData d = (MyData)session.getAttribute("data"); //data is the variable name set in the JSP page
}
}
This, I believe, is the general flow to your show you how you can access a Java Bean's data from within a Servlet.
我相信,这是向您展示如何从 Servlet 中访问 Java Bean 数据的一般流程。
回答by Yassine Abdul-Rahman
if you need to be far to servlets, you can pass object to other JSPs from one page to another.
如果您需要远离 servlet,您可以将对象从一个页面传递给其他 JSP。
let's say your class is com.company.beans.User
假设你的班级是 com.company.beans.User
on first page GetInput.jsp you have the following code
在第一页 GetInput.jsp 你有以下代码
<jsp:useBean id="user" class=com.company.beans.User />
now the object user can be reused in all JSPs pages such the container create an instance of the class ..
现在对象用户可以在所有 JSP 页面中重用,例如容器创建类的实例..
in the same page you can have a form that pass the form input to another jsp page
在同一页面中,您可以拥有一个将表单输入传递给另一个 jsp 页面的表单
<form action="second.jsp">
<input type="text" name="uname"/>
<input type="password" name="pass"/>
<input type="submit" value="submit"/>
</form>
on the second page you can access the object named user
在第二页上,您可以访问名为 user
<jsp:useBean id="user" class="com.deeb.beans.User" />
<%
user.setUsername(request.getParameter("uname"));
user.setPassword(request.getParameter("pass"));
%>
also on the another page you can get the values
同样在另一个页面上,您可以获得值