Get information from JSP(form) to 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.

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"


}

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!

Answers


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:

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

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

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.


Take a look at the tutorial on Handling Form Data


if you need to be far to servlets, you can pass object to other JSPs from one page to another.

let's say your class is com.company.beans.User

on first page GetInput.jsp you have the following code

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

in the same page you can have a form that pass the form input to another jsp page

<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

<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


Need Your Help

What alternative to HTTP_HOST can I use?

php apache security host

I have to display current server host (in PHP, but it is not very important). I don't know on which host I am.

Check if file exits using Shell Script -Follow multiple code paths

macos shell installation sh

My Main application takes time to launch due to a non correctable bug in the framework i have used.I have created a launcher program that simply displays a loading animation and does the background...