passing parameter from JSP to servlet

Discussion in 'Java' started by Krish, Nov 5, 2006.

  1. Krish

    Krish Guest

    how can i pass parameter from JSP to servlet
    /////in the JSP ////

    <%String filechoice="1";
    request.setAttribute("filechoice",filechoice); %>



    //////in the servlet///
    (String)request.getAttribute("filechoice"))

    But I am getting null exception error....I am new to JAVA....need help
    Krish, Nov 5, 2006
    #1
    1. Advertising

  2. Krish

    Daniel Pitts Guest

    Krish wrote:
    > how can i pass parameter from JSP to servlet
    > /////in the JSP ////
    >
    > <%String filechoice="1";
    > request.setAttribute("filechoice",filechoice); %>
    >
    >
    >
    > //////in the servlet///
    > (String)request.getAttribute("filechoice"))
    >
    > But I am getting null exception error....I am new to JAVA....need help


    I'm not really sure of your design, but a JSP is a Servlet itself, so
    unless they are processing the same request, then they don't really
    communicate with eachother. the request attributes are only set for
    one request (in otherword, one page load from the client).
    Daniel Pitts, Nov 5, 2006
    #2
    1. Advertising

  3. Krish wrote:
    > how can i pass parameter from JSP to servlet
    > /////in the JSP ////
    >
    > <%String filechoice="1";
    > request.setAttribute("filechoice",filechoice); %>
    >
    >
    >
    > //////in the servlet///
    > (String)request.getAttribute("filechoice"))
    >
    > But I am getting null exception error....I am new to JAVA....need help


    First: it is a very unusual setup. Passing parameters from
    servlet to JSP are way more common.

    Second: it will only work using the request object if the
    JSP page forward to the servlet.

    All other usages would require usage of session instead of request.

    Arne
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Nov 5, 2006
    #3
  4. Krish said,on 06/11/2006 12:20 AM:
    > how can i pass parameter from JSP to servlet
    > /////in the JSP ////
    >
    > <%String filechoice="1";
    > request.setAttribute("filechoice",filechoice); %>
    >
    >
    >
    > //////in the servlet///
    > (String)request.getAttribute("filechoice"))
    >
    > But I am getting null exception error....I am new to JAVA....need help
    >


    Instead of passing parameters, try using
    request.getsession().setAttribute().

    This would put the values in the session
    for further processing.

    if you still want the request to hold it, then you
    would have to do a forward(), with the same request
    object.

    Rgds,
    Shanmu.

    --
    Crime does not pay ... as well as politics.
    -- A. E. Newman
    Shanmuhanathan T, Nov 6, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. circuit_breaker
    Replies:
    2
    Views:
    1,996
    Jack Jia
    Apr 4, 2004
  2. Andrew Purser
    Replies:
    3
    Views:
    5,079
    Alex Kay
    Nov 4, 2004
  3. Saaji
    Replies:
    6
    Views:
    7,142
    Saaji
    Mar 14, 2006
  4. javadev
    Replies:
    5
    Views:
    12,904
    javadev
    Nov 16, 2006
  5. Replies:
    4
    Views:
    783
Loading...

Share This Page