Results 1 to 4 of 4
  1. #1
    krishkill is offline Member
    Join Date
    May 2010
    Posts
    11
    Rep Power
    0

    Default Variables in a Servlet

    Hi,

    I'm using some variables in a servlet. the servlet response in a jsp page which after taking some more parameters gets redirected to another servlet. I want to use same variables I used in first servlet in the new one. How should i declare inorder to use this way. I know something like session attributes, but donno how to implement it. please help . please let me know the syntax.

    Thanks

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    the session is a property of the servlet request object.


    in the first servlet,

    Java Code:
    public void doGet(HttpServletRequest request,  HttpServletResponse response) throws ServletException, IOException {
    
      request.getSession().setAttribute("paramName",  "some value");
    
    }
    where the value is any object.

    in the second servlet
    Java Code:
    public void doGet(HttpServletRequest request,  HttpServletResponse response) throws ServletException, IOException {
    
      Object val = request.getSession().getAttribute("paramName");
    
    }
    where you need to know the class type, usually by casting

    String value = (String) request.geSession().getAttribute("paramName");

  3. #3
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    5

    Default

    wouldn't

    Java Code:
    request.getParameter("paramName")
    work also?

    for the second servlet also work?
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

  4. #4
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    Yes, request.getParameter() would work on the second page, if the first page did a redirection thru a link on the page where there are URL parameters, or if there was a <form> element that was submitted.

    If the first page is submitted back to itself and the server side does a HTTP 302 redirect, it might not have the URL parameters on the second page ?

Similar Threads

  1. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 05:48 PM
  2. how can i link html+servlet+xml to form servlet
    By balachandarr in forum Java Servlet
    Replies: 0
    Last Post: 04-15-2009, 04:06 PM
  3. Replies: 1
    Last Post: 12-02-2008, 12:23 AM
  4. Replies: 0
    Last Post: 08-30-2008, 11:52 AM
  5. Replies: 1
    Last Post: 08-07-2007, 03:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •