Solved

doGet() and doPost() and use of local variable.

Posted on 2006-07-08
4
914 Views
Last Modified: 2012-08-14
Hi,

I have a servlet controller that controls every action; however, doGet method seems to be called twice and the variable that declared is null when it is being called second time. For example, I have hidden field from html/jsp page to detect which page the action call is coming from. I have print out stmt like this:

requestPage: WelcomePage
requestPage: null

Here is my code:
+++++++++++++++

   public void init() throws ServletException{
     
        dsName = getServletConfig().getInitParameter("dataSourceName");  
        numLoginAttempts = getServletConfig().getInitParameter("numLoginAttempts");  
    }
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException, java.io.IOException {
     
   
       
        requestPage = (String)request.getParameter("RequestPage");
        System.out.println("requestPage: " + requestPage);
        RequestDispatcher rd = null;
       
        if(requestPage.equals(LOGIN_PAGE)){    
              if((String)session.getAttribute("fromRegistrationForm") != null) {
                    session.removeAttribute("fromRegistrationForm");
              }
            rd = request.getRequestDispatcher("/JSPLogin.jsp");      
              rd.forward(request, response);  
             
        }else if(requestPage.equals(NEW_REGISTRATION)) {
              // rd = request.getRequestDispatcher("/JSPRegistration.jsp");      
             //rd.forward(request, response);  
              response.sendRedirect("./JSPRegistration.jsp");  
            //System.out.println("here4");
            //response.sendRedirect("./JSPLogin.jsp");
        }else if(requestPage.equals(REGISTER_PAGE)){    
              
              session.setAttribute("fromRegistrationForm", "fromRegistrationForm");
              ...
              ...
                ...
              System.out.println("studentInfoBean.getUserId(): " + studentInfoBean.getUserId());
              
            rd = request.getRequestDispatcher("/JSPLogin.jsp");        
            rd.forward(request, response);  
           
           
           
     
        }else{}
 
    }
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException, java.io.IOException {
        doGet(request, response);
    }
   ++++++++++++++++

thanks in advance
0
Comment
Question by:dkim18
4 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 50 total points
ID: 17066547
check in your page when that hidden var is being set to null.
0
 
LVL 23

Assisted Solution

by:Ajay-Singh
Ajay-Singh earned 150 total points
ID: 17066780
Is this servlet single-threaded (i mean does it implemnent SingleThreadModel interface). If yes this behaviour is expected.
Otherwise the only possibility is that this local variable "requestPage" is modified concurrently.
0
 
LVL 10

Accepted Solution

by:
mukundha_expert earned 150 total points
ID: 17070622
This might be because,

you might be forwarding or redirecting the request again to the same servlet ( this servlet ) , that is why you get the request 2 times..

because the first time request comes from your form thats is why you have the value not null, the next time the request is coming from someother source might be your jsp's also ( to which you are forwarding  they might again resend it to the same servlet ),

so for the second request teh parameter 'RequestPage' wil not be there since its not coming from the form..


what you can do is,,

since the value of requestPage is welcomepage,

>>  if(requestPage.equals(LOGIN_PAGE)){    
>>  }else if(requestPage.equals(NEW_REGISTRATION)) {
>>  }else if(requestPage.equals(NEW_REGISTRATION)) {
>>  else
>> {}

i dont see any condition check for welcome page,

if teh request is from the welcome page what you are doing??

if you are forwarding to someother JSP check what that JSP is doing..
somewhere in the loop its getting diverted back to the same servlet..

use system.out.println () in places where you think the flow is leading and check what is happening


0
 

Author Comment

by:dkim18
ID: 17073672
i fixed the problem. when i declared requestPage inside of doGet(), i got the result as I expected. there might be better solution, but this is good enough. Thanks all
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question