Get information from JSP(form) to Java Bean

只愿长相守 提交于 2021-02-07 10:17:29

问题


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!


回答1:


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:

  • Beginning and intermediate JSP/Servlet tutorials



回答2:


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.




回答3:


Take a look at the tutorial on Handling Form Data




回答4:


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



来源:https://stackoverflow.com/questions/3563387/get-information-from-jspform-to-java-bean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!