从JSP(form)获取信息到Java Bean [英] Get information from JSP(form) to Java Bean
问题描述
我有一个JSP表单.我的要求是获取此表单的数据并在服务器端创建一个Java bean对象.
I have a JSP form. My requirement is to get this form's data and create a java beans object on the server side.
示例:我的表单具有姓名","SSN",电子邮件"和电话号码
Example: my form has fields like Name, SSN, EMAIL & Phone Number
public class Test {
long ssv= 1282199222991L;
long phone= 4082224444L;
String email = "abcdef@yahoo.com";
String name="abcdef"
}
据我所知,我一直在考虑使用Servlet创建Bean对象,该Servlet是在服务器端由JSP创建的.我的问题是我如何访问此服务器创建的" servlet以获取变量数据?
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?
PS:我是Web编程和Web应用程序的初学者.服务器端脚本.如果问题不清楚,请告诉我.任何信息对我来说都是非常有价值的.如果我以正确的方式思考,请告诉我.预告片谢谢!
推荐答案
JSP确实应该将表单提交给servlet. servlet实际上应该创建Bean并使用它通过必要的层传输提交的数据(使用DAO类保存在数据库中和/或在提交后在结果JSP中重新显示).
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外观的启动示例:
Here's a kickoff example of how the JSP should look like:
<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>
这是如何编写正在/register
的url-pattern
侦听的servlet的doPost()
方法.
And here's how you could write the doPost()
method of the servlet which is listening on an url-pattern
of /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);
另请参见:
- JSP和Servlet入门和中级教程
- Beginning and intermediate JSP/Servlet tutorials
See also:
这篇关于从JSP(form)获取信息到Java Bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!