Solved

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

Posted on 2006-07-08
4
915 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

685 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