Sharing values between Servlets and Jsp

In this post I will show a simple code and explain how to pass information between Jsp and Servlet. This information sharing is highly essential for many programming scenarios encountered in real time.

We will pass information to and fro between servlets and jsp using sessions and servlet context information.
I presume you know how to invoke a servlet from a jsp page by making entries in web.xml in Tomcat Container.
If you are using Eclipse, the latter will take care of the mapping, though in some cases you would have to manually make the entry in web.xml (Deployment Descriptor).

Overview:
We got 4 files here:

  • Page1JSP.jsp
  • Page2Servlet.java
  • Page3JSP.jsp
  • Page4Servlet.java
  1. We will pass a value from Page1JSP to Page2Servlet.
  2. Then we will pass another value from Page2Servlet to Page3JSP.
  3. And finally, we pass a value from  Page3JSP to Page4Servlet.
  4. In addition, we will be accessing a value in Page4Servlet that has been sent from Page1JSP to Page2Servlet.

Lets see some action!

In Page1JSP.jsp,
This is the form we have in Page1JSP.jsp

Notice that we are passing the form data “tName” to a servlet named “Page2Servlet”.

Output of Page1JSP.jsp:

In Page2Servlet.java,
We obtain form data sent by Page1JSP.jsp  in the service() method of the servlet:

Next, we create a  session  and set an attribute named “Page2SessAttr”
Page2SessAttr contains the form value received from Page1Jsp. You can also set any another value into it.
Next, we obtain the Servlet Context and set an attribute in it, so that when we  pass a servlets context to a Jsp , the latter would be able to access the attribute set and “forwarded” by the servlet.

Page3Jsp will receive the entire context of the servlet, that includes even the attributes set by the latter.

In Page3JSP.jsp,
We will access the values sent by Page2Servlet both by the session and through the servlet context.

Notice that, towards the end of the code, we have also created another session attribute named “Page3SessAttr”. The session itself was created in Page2Servlet.

In Pag3Jsp, we have just accessed that session and NOT created a new session. This you can know from the following line in the above code:

HttpSession sess = request.getSession(false);

The parameter “false” indicates that this is an already created session.

Finally, In Page4Servlet.java
We will access the session attributes set by Page3JSP.jsp

In addition, “recvStr2” contains the value of “Page2SessAttr” set in Page2Servlet session.

The final output I see in my Eclipse console is:

I hope, this piece of material was clear enough to explain you how to share values between Servlets and Jsp. There may be other methods of sharing too, but this seems fairly simple to me.

If you are not clear with my explanations in tutorial just read follow the code snippets alone.

Share this post if you like it!
Cheers!

JavaBean in JSP : a simple tutorial

Hi !

Purpose of this tutorial : To give the reader a better understanding of using Java beans in JSP.

Advantage : This tutorial serves as an instant learning tutorial for those who would want to learn about using  Java bean in JSP.

Sources : The code used in this short tutorial has been extracted from http://www.jsptut.com

What is a Java Bean ?
Java bean are classes written in the Java programming language conforming to a particular convention. A Java bean has a set of properties that can be read or changed. It is also possible to discover the properties a bea has available, which well suits beans for modeling all kinds of real world things that can also be described as a set of properties.

Illustration:

What we will be doing?

We will create a simple html page(getName.html) that is used to get user’s name, email and age.  When we submit that data, it invokes a JSP page(SaveName.jsp) that inserts the user entered data into a bean. We provide a “click here to continue” link in that same page, which when clicked would invoke another JSP page (NextPage.jsp) that retrieves the data that was inserted into the bean and displays it!

The implementation is simple and straight forward!

Tools needed:

I have tested this code successfully in Tomcat 6.0 configured suitably with jdk1.5.0_14 and jre1.5.0_14.

code :

getName.html


SaveName.jsp


NextPage.jsp


Keep the above files in your webserver directory. My webserver classpath is C:\Tomcat 6.0\webapps\ROOT\ . Now we must develop a Java code to store the values obtained from the form. We then compile that Java code to obtain its “class” file. This class file is the real beauty of Java. Its contents look somewhat junk type when viewed using any editor. And that means that its not easy to hack your data from it(say, a password was sent from the form).

The contents of the Java code are very important. We define a Java class with fields “username”,”email” and “age” which must correspond exactly to the field names in getName.html. We define two types of methods in this Java code viz, a “setter” method and a “getter” method.

In simple,

* The setter method is a method that starts with the keyword “set” followed by the field name. In our example the setter methods would be setUsername,setEmail and setAge. Now compare this with the field names in getName.html. The only point you must note here is  the first letter, following “set”, is in uppercase. These setter methods insert the user entered data into the corresponding variable names into the bean.

At this point, by reasoning you should have understood that a bean is an instance of the Java class(correct me if i am wrong).

* The getter method is a method that returns the values stored in the javabean, to the entity that invoked it(NextPage.jsp in this case). The getter methods are named in the same way as setter methods except that the keyword “set” is replaced by “get”. So now we have getUsername,  getEmail, getAge

The java code is shown below:

UserData.java

Now compile the above Java code and place the code inside a folder named “user” because we have declared a java package by that name. This folder (along with the java class)should be placed in the following location:

<Application_Base\application_name>\WEB-INF\classes\

For me its :

C:\Tomcat 6.0\webapps\ROOT\WEB-INF\classes\

If the folder “classes” is not present then create one.

That is it invoke the getName.html file from the browser after starting Tomcat server and see the code in action. JSP rocks!

Output:

See the output here , here and here.

What I have not explained here?

I have not explained the meaning of the java bean tags in the jsp code given about. Their meanings and pupose are quite intuitive,if not just google to find more info on them.

How could i make use of this idea?

You can use the idea gathered from this tutorial for designing a user login page using java beans. Creativity has no bounds.

References: