Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Help on Sessions

Posted on 2005-03-29
Medium Priority
Last Modified: 2010-04-01
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?



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


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


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




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;


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


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

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

Question by:nickl1
LVL 19

Assisted Solution

Kuldeepchaturvedi earned 300 total points
ID: 13655541
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
LVL 29

Accepted Solution

bloodredsun earned 300 total points
ID: 13656612
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.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Herein one will find an aggregate of some of my experience building and deploying virtualization stacks both in standalone, clustered Hyper-V, clustered Hyper-V with a Scale-Out File Server (SOFS) backend, and Storage Spaces Direct (S2D).
Often, the users face difficulty in accessing Outlook 2016 PST files on Windows 10 computer. One of the reasons behind it is the improper functioning of MS Outlook when the user tries to open it. MS Outlook suddenly stops working, or it will not op…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month15 days, 1 hour left to enroll

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question