Solved

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

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

776 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