Solved

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

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now