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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

An introduction to the wonderful sport of Scam Baiting.  Learn how to help fight scammers by beating them at their own game. This great pass time helps the world, while providing an endless source of entertainment. Enjoy!
The Summer 2017 Scholarship Winners have been announced!
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

762 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