Results 1 to 7 of 7
  1. #1
    anthrax is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default how do i make all servlet implement this same condition?

    i want all my servlet to implement this conditional statement but i dont want to be re-writing the code on every servlets i make. how do i do it?

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub

    if(request.getParameter("a")==request.getParameter ("b")){
    //actions here will depend on servlet funtions

    }
    else{
    //actions here will depend on servlet funtions
    }

    }

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Different servlets will have to take different action, but you don't want to write separate code? I think I must be missing something. Can you give us a clearer example?

    -Gary-

  3. #3
    anthrax is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    i want all my servlet to check the session before executing their function.

    if the session is valid: servlet will do its function
    else: redirection is done

    the conditional IF statement is common to all my servlets, im looking for a way other than rewriting the IF statement

  4. #4
    moro is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

  5. #5
    anthrax is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    tnx :D this is what i need

  6. #6
    KumbhaniMehul is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Smile Your Answer

    I am giving solution of your question

    Make One Servlet
    Write the following Method in that servlet
    now on every servlt post method call this method like
    Servlet_Name.Method_Name(request);



    public static void Method_Name(HttpservletRequest req,HttpservletRequest res)
    {
    if(request.getParameter("a")==request.getParameter ("b")){
    //actions here will depend on servlet funtions

    }
    else{
    //actions here will depend on servlet funtions
    }
    ...
    }

  7. #7
    anthrax is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    thats also a good idea, tnx :D

    but there will be a problem if i have this

    public static void Method_Name(HttpservletRequest req,HttpservletRequest res)
    {
    if(request.getParameter("a")==request.getParameter ("b")){

    RequestDispatcher rd = request.getRequestDispatcher("/good.jsp");
    rd.forward(request, response);

    }
    else{
    RequestDispatcher rd = request.getRequestDispatcher("/error.jsp");
    rd.forward(request, response);
    }

    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub

    Servlet_Name.Method_Name(request);

    ...

    //do more method, an error will occur since request has been dispatched to other servlet/jsp

    }

Similar Threads

  1. Best Practices of handling more number of if..elseif.. Condition
    By gavaskarrathnam in forum Advanced Java
    Replies: 16
    Last Post: 01-28-2011, 10:25 AM
  2. IF-Condition in a String
    By lenaz in forum Advanced Java
    Replies: 1
    Last Post: 07-18-2009, 12:07 PM
  3. JFrame (closing under a condition)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-12-2008, 11:17 AM
  4. problem with using string in if condition
    By sireesha in forum New To Java
    Replies: 2
    Last Post: 11-20-2007, 10:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •