Passing variables from JSP to servlet

17,283

The standard way of passing/submitting data to the server in the pure Servlets/JSP world (as in your case from the JSP to the servlet) is by using HTML form, i.e. the same way as when using other technologies (ASP.NET, PHP etc). And it doesn't matter whether it is a pure HTML page or JSP page. The recommended/most used method of submitting data from the form to the server is POST.

You also can pass data in the query string that is contained in the request URL after the path (this also happens when instead of POST you use GET method in the form). But that is for the simple cases, like constructing URLs for the pagination etc (you can see the example of constructing URLs with the additional queries here: Composing URL in JSP)
Example of passing parameters in the URL:
http://example.com/foo?param1=bar&page=100

For the difference between submitting data using GET and POST methods read here:

So you can configure some servlet to process data sent/submitted from a JSP or HTML etc. It is highly recommended to submit data using POST method and respectively to process the submitted data using the doPost() method in your servlet. You will then get the parameters passed by the client in the request by using one of the following ServletRequest methods:

Here is a nice tutorial with examples: Handling the Client Request: Form Data

The above tutorial is from the following course:
Building Web Apps in Java: Beginning & Intermediate Servlet & JSP Tutorials


Another way of exchanging data using Java EE is by storing data as attributes in different scopes. (Following is the excerpt from one of my answers on SO)

There are 4 scopes in Java EE 5 (see The Java EE 5 Tutorial: Using Scope Objects). In Java EE 6 and in Java EE 7 there are 5 scopes (see The Java EE 6 Tutorial: Using Scopes and The Java EE 7 Tutorial: Using Scopes). The most used are:

  • Request scope
  • Session scope
  • Application scope (Web Context)

You can store some data in all the above scopes by setting the appropriate attribute.

Here is a quote from the Java EE API docs related to ServletRequest.setAttribute(String, Object) method in regard to request scope:

void setAttribute(java.lang.String name,
                  java.lang.Object o)

Stores an attribute in this request. Attributes are reset between requests. This method is most often used in conjunction with RequestDispatcher.
...

So with every new request the previous attributes you have set in request will be lost. After you have set an attribute in a request, you must forward the request to the desired page. If you redirect, this will be a totally NEW request, thus the attributes previously set will be lost. (If you still want use redirection read this: Servlet Redirection to same page with error message)

Those attributes that are set in a HttpSession (in the session scope) will live as long as the session lives and, of course, will be available to only the user to which session belongs.

As for the context attributes they are meant to be available to the whole web application (application scope) and for ALL users, plus they live as long as the web application lives.

Also maybe this article will be useful for you as well: How Java EE 6 Scopes Affect User Interactions


Also pay attention to the following issue. You wrote (quote):

I know , We can pass variable from servlet to jsp by using request.setAttribute(key , value) But when I used it to set variable in jsp and again get it by using session.getAttribute(key ) then result is null.

As the users @neel and @Sanchit have noticed, you are setting an attribute in the request object, but trying to get it back from the session. No wonder you are getting null in this case.


Hope this will help you.

Share:
17,283
MrYo
Author by

MrYo

Hello World!

Updated on June 14, 2022

Comments

  • MrYo
    MrYo almost 2 years

    All the time, when I searched on Google, I got dozen of answers which are posted in Stackoverflow about passing variables to servlet from JSP. But I am wondering, I don't get answer of: How to pass a variable from JSP to a servlet class? Is it possible?

    Actually I am doing a simple PhoneBook application. Here I have to send contact id to a servlet for editing and deleting. How can I pass this value?

    I know, we can pass variable from servlet to JSP by using request.setAttribute(key, value) But when I used it to set variable in JSP and again get it by using session.getAttribute(key ) then result is null.

    God help me.

    • Vaibhav Desai
      Vaibhav Desai about 11 years
      Why will you post here if you want God to help you :-p
    • Sanchit
      Sanchit about 11 years
      I wonder why putting stuff into request and getting stuff from something NOT named request isn't working.
    • MrYo
      MrYo about 11 years
      @VaibhavDesai Everyone is god who helps someone. :)
    • Sotirios Delimanolis
      Sotirios Delimanolis about 11 years
      I think you've misunderstood the lifecycle of an HTTP request in the context of a servlet and jsp. The request arrives at the container, which calls up a servlet. The servlet does some magic and forward the request to the jsp, basically rendering html. The servlet has no more business anymore. You need to make a new http request either through a form, a link, a page refresh, etc.
    • Roman C
      Roman C about 11 years
      You can communicate with servlet via making request to it, even if you do it from jsp.
    • MrYo
      MrYo about 11 years
      @SotiriosDelimanolis We can't use jsp page to pass some data to servlet other then session variable? It's purpose is only for render? then How we can communicate jsp and servlet with out using form?
    • MrYo
      MrYo about 11 years
      I can pass form element but How I can send other variable which are outside the form element?
    • MrYo
      MrYo about 11 years
      @RomanC I did it, But It always returns NULL only :(
    • Elye M.
      Elye M. about 11 years
      provide some code, so it will be ez'er to help.
    • Roman C
      Roman C about 11 years
      @SanjayaPandey servlet returns nothing but process the request and response.
    • Roman C
      Roman C about 11 years
      @SanjayaPandey Sorry, it returns only the last modification time of the request and it can't be NULL.
    • didierc
      didierc about 11 years
      In your question, where is the code you have problem with? You need to show the code, or a smaller version reproducing the issue you are facing. Please help us helping you.
    • Avinesh
      Avinesh about 11 years
      you are setting variable in request but getting it from session object that's why you are getting null. set and get attribute using same object either request or session.
    • Amir Pashazadeh
      Amir Pashazadeh about 11 years
      You must pass what you need as request parameters, and use request.getParameter() to get them.
  • MrYo
    MrYo about 11 years
    I had solved that problem by using query string after that question post here :) That is best for me in this case and your answer is similar to that concept, thanks for answer :)