-1

I have a Servlet class where I am doing NTLM authentication and if successful, I am forwarding to a JSP page.

JSP page has a user form where few registration details are entered and page is submitted. In another servlet called ServiceController I am getting the all the form details using

request.getParameter("");

Problem I am facing is if I am redirecting to JSP page from Servlet, request.getParameter(""); are all null, but if I am drectly accessing JSP page, I am successfully able to get all form values using request.getParameter("");

Why I am not able to get form values in Servlet if I am redirecting from Login Servlet class to JSP page?

Servlet Code where I am redirecting

public void doGet(HttpServletRequest request, 
                  HttpServletResponse response) throws ServletException, IOException {

    response.setContentType(CONTENT_TYPE);        
    String auth = request.getHeader("Authorization");
    ...

   if (findUsername(uname) == true) { 
   response.sendRedirect("myForm.jsp");
   return;
           } else {
        response.sendRedirect("accessdenied.jsp");
        return;

Any help is highly appreciable

Jacob
  • 14,463
  • 65
  • 207
  • 320
  • 5
    Are you intending to forward the request? Redirection is a completely new request that will drop the parameters unless they're manually forwarded. – pickypg Apr 08 '13 at 06:55
  • @pickypg Ideally I would like to set a session value in Login servlet and redirect to JSP page and retrieve session value in JSP or my other ServiceController Servlet classes. And of course in JSP page I should be able to pass all form values to servicecontroller class. – Jacob Apr 08 '13 at 06:58

4 Answers4

3

response.sendredirect() method will invoke a completly new request, so you can not access any values from your previous request. you can use requestdispatcher or you can set the values in session and read them in your jsp page

Vasu
  • 149
  • 7
  • Vasu I have tried as `RequestDispatcher requestDispatcher; requestDispatcher = request.getRequestDispatcher("/myForm.jsp"); requestDispatcher.forward(request, response);`, however still when I submit my JSP page, all form values are null in my ServiceController class. – Jacob Apr 08 '13 at 07:12
  • Before forwarding the request set whatever values you want to receive in your jsp to request object like request.setParameter("",""); – Vasu Apr 08 '13 at 07:14
  • Vasu, I am receiving values in a separate Servlet. Flow is Login Servlet to JSP page, form is submitted and it values are passed to another Servlet class called ServiceController. – Jacob Apr 08 '13 at 07:16
3

There are two separate concepts going on here, and I think you are mixing them up.

Once you have started handling a request, you have a few options:

  • Stop responding (whether intentional or not, although the container will do some things for you)
  • Redirect the request using HttpServletResponse.sendRedirect, which will respond with HTTP 302 to send the browser to the identified page. You could even send them away from your server with a redirect.
  • Forward the request using RequestDispatcher.forward (the RequestDispatcher can be accessed from the ServletRequest.getRequestDispatcher), which will redirect to a different servlet/JSP to handle the request. This is completely invisible to the user, and the requested URL will not change for them unlike redirect. A forwarded request must go to something on the server, and the user has no way of knowing the request was forwarded unless they are explicitly told. Forwarded requests will maintain their parameters, including Attributes, which are a clean way of passing parameters between servlets.

In your case, you mentioned the desire to set the Session upon a successful login, and then retrieve. Those concepts are completely independent of each other.

You can set the session inside of your this.validUser check, and then forward/redirect. In the else, you may choose to still redirect to really call out failure.

pickypg
  • 22,034
  • 5
  • 72
  • 84
  • I have tried as RequestDispatcher requestDispatcher; requestDispatcher = request.getRequestDispatcher("/myForm.jsp"); requestDispatcher.forward(request, response);, however still when I submit my JSP page, all form values are null in my ServiceController class. – Jacob Apr 08 '13 at 07:12
  • You can chain the calls to simplify them: `request.getRequestDispatcher('tenderList').forward(request, response)` (to improve readability). Are you sure that the parameters are non-`null` at this servlet? – pickypg Apr 08 '13 at 07:16
  • To make it clear, I am receiving values in a separate Servlet. Flow is Login Servlet to JSP page, form is submitted and it values are passed to another Servlet class called ServiceController. – Jacob Apr 08 '13 at 07:17
  • For further clarification: Login Servlet passes parameters to JSP page. Can _that_ JSP page see the parameters, or is it a later step trying to see the parameters? – pickypg Apr 08 '13 at 07:21
  • Login Servlet is successfully passing parameters to JSP. Problem is in JSP page I have a form and page is submitted to another Servlet class called ServiceController. In ServiceController, I am get null values for all form values of JSP page. – Jacob Apr 08 '13 at 07:24
  • Once the server responds or redirects, then those parameters will be gone unless they are replaced. They need to be stored, or added to the form as hidden parameters (don't store anything secure there, as users can see the values). Alternatively, you could add them to the `Session` as you noted, and then the submitted-to JSP page can check the `Session` for those parameters. – pickypg Apr 08 '13 at 07:27
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/27755/discussion-between-polppan-and-pickypg) – Jacob Apr 08 '13 at 07:29
2

Redirection is different from forwarding a request/response.In case of redirection transfer of control task is delegated to browser by container ,then browser generate a new request to the specified url and old request and response objects are lost.

Dangling Piyush
  • 3,658
  • 8
  • 37
  • 52
0

I have solved the issue I had with all forms elements being null, it was rather an issue with NTLM authentication code I had in my Login servlet.

I am using JCIFS (samba.org) solution for getting username who are logged into domain. Altogether removed my crap code which I was using for getting username who are logged into domain and followed this

Jacob
  • 14,463
  • 65
  • 207
  • 320