Solved

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

Posted on 2006-07-08
4
916 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

751 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