Help on Sessions

I have several forms that I am using to collect information as a part of a shopping cart (billing, shipping, payment, etc.). I want to be able to submit each form and temporarily store the information. Then I want to be able to display it several pages later as a confirmation page, to verify it is all correct.

From what I can tell there appears to be two ways of potentially handling this. My question is why would I use one of the following approaches over the other?

FIRST APPROACH

[page1.jsp]

<form method="post" action="page2.jsp" name="myForm">
<input type="text" name="firstname" size="30">
<input type="text" name="lastname" size="30">
<input type="submit" value="Submit">
</form>


[page2.jsp]

<% HttpSession user = request.getSession(true);
user.setAttribute("fname", request.getParameter("firstname"));
user.setAttribute("lname", request.getParameter("lastname"));
%>

[page3.jsp]

<% HttpSession user = request.getSession(true); %>
<%= user.getAttribute("fname") %>
<%= user.getAttribute("lname") %>

----------------------------------------------------------------

SECOND APPROACH

[MyBean.java]

package myPackage;

public class MyBean {
  private String fname;
  private String lname;
  public MyBean() {
    name = "";
  }

  public String getFname() {
    return fname;
  }

 
  public void setFname(String x) {
    fname = x;
  }

  public String getLname() {
    return lname;
  }

 
  public void setLname(String y) {
    lname = y;
  }
}



[page1.jsp]

<form method="post" action="page2.jsp" name="myForm">
<input type="text" name="firstname" size="30">
<input type="text" name="lastname" size="30">
<input type="submit" value="Submit">
</form>




[page2.jsp]

<jsp:useBean class="myPackage.MyBean " id="firstBean" scope="session" />
String firstname = request.getParameter("firstname");
String lastname = request.getParameter("lastname");
<jsp:setProperty name="firstBean" property="fname" value="firstname" />
<jsp:setProperty name="firstBean" property="lname" value="lastname" />



[page3.jsp]
<jsp:useBean class="myPackage.MyBean " id="firstBean" scope="session" />
<jsp:getProperty name="firstBean" property="fname" />
<jsp:getProperty name="firstBean" property="lname" />


nickl1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KuldeepchaturvediCommented:
only thing is that later one gives you more security over the data ( data can not be modified without accessing bean method) and is more structured in approach......

Second one is a recommonded one
0
bloodredsunCommented:
Definitely use the second approach. The last thing you want to do in a shopping process is expose your data any more thatn you have to. Ideally you would also change the jsp you are sending you data to to a servlet. That way you can only implement the doPost method. This prevents the user from spoofing the values by adding them in the manner of a GET request (which would be handled by the doPost() method).

Once you have received the data in your servlet and entered into a javabean, then use a RequestDispatcher to forward the request onto the next jsp.

If you look at current best practise of MVC (Model View Controller) such as Struts, you will see that this is how the flow of control is handled. A javabean representing the form is passed to a servlet (actually a lightweight version termed an action) which, having performed business logic, then passes the request onto the presentation layer, typically a JSP.

Remember, all session data apart from the session id number is held on the server. This makes it very secure and difficult to spoof.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.