views:

25

answers:

2

I have a JSP form. My requirement is to get this form's data and create a java beans object on the server side.

Example: my form has fields like Name, SSN, EMAIL & Phone Number

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?

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!

+1  A: 

Take a look at the tutorial on Handling Form Data

Boris Pavlović
+1  A: 

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).

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>

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);

See also:

BalusC