DoGet() and service() method differences in servlets

can you give the differences between doGet() and service() methods in servlets. where to use doGet() and where to use service()
thanks

service() is called whenever the Servlet gets a HTTP request. Per default it calls the correct do<METHOD>() method.
So doGet() is only called when you get a GET request via HTTP. For POST (for example) doPost() will be called instead.

Similar Messages

  • Write a servlet without doGet() and doPost() methods

    Hi,
    Can we write a servlet without doGet() and doPost() methods ?

    public class MyCoolServlet extends HttpServlet
    public void init(ServletConfig servletConfig) throws ServletException
    public void service(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException
    // your code here
    }Just an example on how to do it.
    A servlet is loaded by server (before any request). It will run the init function. Then pick up an request, spawn a thread and put this servlet code into the thread, give it session, request and response objects, then call serivce.
    Remember that init is called before, outside the thread, while service is inside the thread and the last to be called. Nice place to put your code.
    If you need to have some sort of init function to be called first on every request PR USER, then make your own function, and call it first in service method.
    Stuff that are put in init() function might only get runned once in the entire server lifetime. Until restart.

  • Jave servlets- doget() and dopost() methods.

    Iam trying to learn servlets but got confused on these doget() and dopost() methods usage. I just want to know generally what does these methods do in general (like the doget() sets the header..but what about dopost()?). I saw some example code where doget() is called within a dopost()method so Iam not clear about their purposes.
    I'd appreciate any help possible. Thank you.

    The doPost() and doGet() (also doHead() etc.) methods are all designed to handle specific HTTP request types. eg: doGet() handles HTTP GET requests (requests caused by common HTML links or typing a URL in your browser's address bar) while POST handles HTTP POST requests (commonly generated by HTML form submissions).
    When you see an example of a serlvet's doGet() being called within it's doPost(), it is because, at least for part of the processing, the servlet will be treating the two request types the same way.

  • Service method in javax.servlet.http.HttpServlet

    Hi All,
    I have a doubt in the service method of javax.servlet.http.HttpServlet
    There are two service methods in the javax.servlet.http.HttpServlet.
    one is with protected and other is with public access specifier.
    In the javadoc it is said that public service method dispatches client requests to the protected service method.
    Why this type of design is required.
    Instead of having a two service methods, Is it not possible to have a single service method and attain the functionality intended.
    Are there any advantages by having two service methods.
    I think there may be some idea/functionality behind these for having two service methods , which I am not able to understand.
    Would be greatful , if someone can throw light on this .
    Thanks,
    Dhanasekaran.

    This is just a theory, but here goes:
    The public service() method takes arguments of ServletRequest and ServletResponse, presumably because that's the signature that the non-HTTP specific servlet engine requires.
    The protected service() method of HttpServlet takes arguments of HttpServletRequest and HttpServletResponse and includes HTTP specific functions.
    My guess is that the public service() method either casts the arguments to the Http objects or does work to create the Http objects before calling the protected method.

  • Is it possible to use doGet() and doPost method in one jsp

    i am having a jsp form which will perform upload as well as download.There is a seperate servlet for download and upload.For downloading i use doGet method and doPost for the upload.how can i change the method dynamically for the above cases.

    Hi,
    in your jsp, if you call:
    request.getMethod();
    it should the string "GET" or "POST" depending on the HTTP method used, so you could do something like
    <%
    String httpMethod = request.getMethod();
    if ( "GET".equals(httpMethod) ) {
    // code to handle get requests
    else if ( "POST".equals(httpMethod) ) {
    // code to handle post requests
    %>
    I probably would of done it in a servlet(override doGet and doPost), or 2 seperate jsps
    Hope this helps
    Dominic

  • Difference Between doGet() and doPost

    Hi Everyone !!!
    I'm very new to servlets.I dont know what is the difference between doGet() and doPost() Method.
    Can anyone explain me .
    Can any give me details and differences between these two
    thanks in advance

    As you mention Why developer will use GET, if POST have all the advantages. Please look into, it may clear you doubts
    GET
    the GET type request is normally used for simple HTML page requests. The types of events that generate this type of request are clicking on a hyperlink, changing the address directly by typing in the address textbox on a browser or application that has HTML functionality, and submitting an HTML form where the method header is set to get as in method=get. Also, a GET request is triggered when selecting a favorite from the Favorites list and using JavaScript to change location.href. Usually the browser is configured to send a GET request even if no method is set explicitly by the HTML.
    The benefits of the GET method are
    1. It retrieves information such as a simple HTML page or the results of a database query.
    2. It supports query strings (name-value pairs appended to URL). Servers usually limit query strings to about      
         1024 characters.
    3. It allows bookmarks.
    POST
    This occurs when a browser or application submits an HTML form with the method attribute set to post as in method=post.
    The benefits of the POST method are
    1. It sends information to the server such as form fields, large text bodies, and key-value pairs.
    2. It hides form data because it isn't passed as a query string, but in the message body.
    3. It sends unlimited length data as part of its HTTP request body.
    4. It disallows bookmarks.
    Conclusion, always prefer to use GET, except mentioned in the following reason:
    1. If data is sensitive
    2. It is greater than 1024 characters
    3. If your application don't need bookmarks
    As because GET is more faster than POST.
    Both are used to send the request to the server, if you analyze than you may make the differnces like
    In the case of GET means send the request to get the simple HTML page from server whereas, in the case of POST means send the request to post the data to server.
    By Ausaf Ahmad
    Message was edited by:
    Genius_Brainware

  • Service method  of servlet  is called repetitively as response time is more

    Problem: when we try to retrieve data for a particular input, then the retrieve
    query takes longer time which is as expected i.e more than five minutes as our data ware house is large and the where clause is very complex. After 5 mins what happens is that the service
    method in the servlet call is made again and so the retrieve process as if it was
    triggered again,where as no one has triggered it. This happens every 5 mins and the overall
    process hangs forever.pl help if any answer
    thanks in adv.
    M.S.Chandrasekhar

    Hi ,
    Hello ,
    Try this code ,
    Class classObject = Class.forName("SecondServlet");
    SecondServlet servlet = (SecondServlet)classObject.newInstance();
    servlet.init(getServletConfig());
    System.out.println("Before calling the service() from TestServlet");
    servlet.service(req,res);
    servlet.destroy();
    Regards ,
    Venkat

  • Service method of servlet is called repeatedly by 9ias

    Problem: when we try to retrieve data for a particular input, then the retrieve
    query takes longer time which is as expected i.e more than five minutes as our data ware house is large and the where clause is very complex. After 5 mins what happens is that the service
    method in the servlet call is made again and so the retrieve process as if it was
    triggered again,where as no one has triggered it. This happens every 5 mins and the overall
    process hangs forever.pl help if any answer
    thanks in adv.
    M.S.Chandrasekhar

    Hi MS,
    My guess (and it can only be a guess, I'm afraid, based on the [lack
    of] information you have supplied) is that you are hitting some sort
    of timeout value, which is determined by a setting in one of your
    configuration files.
    So as Prasad has already mentioned, more information would help us
    to help you.
    Good Luck,
    Avi.

  • Disadvantages of Overriding service() method in servlets?

    Hi,
    Could you please tell me what are the possible disadvantages of overriding service() method in the servlets?

    If there is some business logic in the service() itself? without the control being transferred to doGet() nor doPost()?
    then what might be the disadvantages of overriding service()But there isn't any business logic in the service() method itself, unless you override it. You are going around in circles here. This is making no sense at all.

  • Request dispatcher and jsp pages called from servlets

              Given the following webapp directory structure:
              root/
              jsp/
              ..all jsp files
              web-inf/
              classes/
              /servlets
                   ..all servlet files
              /com
              ..all other java class files
              I have the jsp directory coded as: jspdir = "/jsp/" (and have tried variations
              on this theme)
              I try to open a new jsp page with the following code in a servlet in the above
              servlet directory:
                   RequestDispatcher rd;
                   rd = getServletContext().getRequestDispatcher(jspdir + myjsp.jsp);
                   rd.forward(req, res);
              rd continues to return null.
              The jsp pages are not registered in the web.xml file for this webapp, if that
              makes any difference.
              Does anyone have any suggestions?     
              [problem.txt]
              

    I create a small sample and put these files into /jsp in my war context
              it works fine, please send me the example code you are using and the
              environment description of the deployed application
              I attached my samples, that work
              Filip
              ~
              Namaste - I bow to the divine in you
              ~
              Filip Hanik
              Software Architect
              [email protected]
              www.filip.net
              "Bill Blackmon" <[email protected]> wrote in message
              news:[email protected]...
              >
              > Yes, I am able to access the jsp pages with the url listed.
              >
              > "Filip Hanik" <[email protected]> wrote:
              > >The weblogic servlet engine, is by default mapping anything that ends
              with
              > >*.jsp to be a JSP.
              > >so there shouldn't be a problem. You didn't answer my first question,
              > >are you able to access your JSP through the browser?
              > >http://localhost:7001/root/jsp/myjsp.jsp
              > >
              > >Filip
              > >
              > >--
              > >~
              > >Namaste - I bow to the divine in you
              > >~
              > >Filip Hanik
              > >Software Architect
              > >[email protected]
              > >www.filip.net
              > >"Bill Blackmon" <[email protected]> wrote in message
              > >news:[email protected]...
              > >>
              > >> The code you are reading in the message here was pasted in and
              > >> looks bad, so maybe that's what you are referring to? The dispatcher
              code
              > >is called
              > >> in the service() method of the servlet.
              > >> I have a suspicion that the jsp directory needs to be defined someplace
              > >-
              > >weblogic.properties
              > >> or web.xml?, though I don't see this in any of the examples that come
              > >with
              > >the product.
              > >>
              > >>
              > >>
              > >> "Simon Ng" <[email protected]> wrote:
              > >> >
              > >> >Hello Bill,
              > >> > Did you put a <BR>
              > >> >return;
              > >> ><BR>
              > >> >statement right after the dispatcher.forward() statement? Well, I am
              > >assuming
              > >> >your
              > >> >code is in the doPost() or doGet() methods.
              > >> >
              > >> >Simon
              > >> >
              > >> >
              > >> >"Filip Hanik" <[email protected]> wrote:
              > >> >>if you do http://localhost:7001/root/jsp/myjsp.jsp
              > >> >>
              > >> >>do you get a result?
              > >> >>
              > >> >>make sure you have the right character case on the filename.
              > >> >>
              > >> >>Filip
              > >> >>
              > >> >>--
              > >> >>~
              > >> >>Namaste - I bow to the divine in you
              > >> >>~
              > >> >>Filip Hanik
              > >> >>Software Architect
              > >> >>[email protected]
              > >> >>www.filip.net
              > >> >>"Bill Blackmon" <[email protected]> wrote in message
              > >> >>news:[email protected]...
              > >> >>>
              > >> >>> Given the following webapp directory structure:
              > >> >>> root/
              > >> >>> jsp/
              > >> >>> ..all jsp files
              > >> >>> web-inf/
              > >> >>> classes/
              > >> >>> /servlets
              > >> >>> ..all servlet files
              > >> >>> /com
              > >> >>> ..all other java class files
              > >> >>>
              > >> >>>
              > >> >>> I have the jsp directory coded as: jspdir = "/jsp/" (and have tried
              > >> >>variations
              > >> >>> on this theme)
              > >> >>> I try to open a new jsp page with the following code in a servlet
              > >in
              > >> >the
              > >> >>above
              > >> >>> servlet directory:
              > >> >>> RequestDispatcher rd;
              > >> >>> rd = getServletContext().getRequestDispatcher(jspdir + myjsp.jsp);
              > >> >>> rd.forward(req, res);
              > >> >>>
              > >> >>> rd continues to return null.
              > >> >>>
              > >> >>> The jsp pages are not registered in the web.xml file for this
              webapp,
              > >> >>if
              > >> >>that
              > >> >>> makes any difference.
              > >> >>>
              > >> >>> Does anyone have any suggestions?
              > >> >>>
              > >> >>
              > >> >>
              > >> >
              > >>
              > >
              > >
              >
              [filipInclude.jsp]
              [filipForward.jsp]
              [filip.jsp]
              

  • How does SingleThreadModel handles doget and dopost?

    Hi,
    I have a requirement to synchronize the DB transactions but my servlet has doGet and doPost methods instead of service method.
    public class EMEATPRates extends HttpServlet implements SingleThreadModel{
    doGet(){
    synchronized(){
    ---Gets the data from DB and displayes on a page
    doPost(){
    synchronized(){
    ----saves the changes to the DB
    I need to get a lock when an user requests for page(doGet()) and release the lock only after he saves it(doPost).
    Does SingleThreadModel handles this?

    HTTP GET requests submit any form parameters within the URL of the request itself. HTTP POST requests submit any form parameters within the HTTP message body. Let's say you have two forms for a logon screen:
    userid, password
    HTTP GET: http://localhost:8080/LoginServlet?userid=foo&password=bar
    [http headers]
    [http body (empty)]
    HTTP POST: http://localhost:8080/LoginServlet
    [http headers]
    userid=foo&password=bar
    Clearly, for something like a logon screen, you would want to POST (no need to see the user's password in a URL if someone walks by). Otherwise, the implementation is up to you. I personally like cleaner-looking URL's, so I usually POST.
    - Saish

  • Problem calling servlet from doget method of another servlet

    hi,
    Iam trying to post an html form written in the doGet() method
    of a servlet to pass this information to another servlet's doPost() method. Iam giving the following URL:
    "<FORM ACTION=http://localhost:8080/examples/servlet/UpdateProcessServlet" +
    "METHOD=POST>"
    But its not happening,the error says that "the page cannot be found" The servlet is not getting called at all. would someboy please help me in this regard.
    Thanks

    #1 Iam calling servlet 2 from here
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    session=request.getSession(false);
    out.println
    (ServletUtilities.DOCTYPE +
    "<!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.0 Transitional//EN>"+
    "<HTML>" +
    "<BODY>" +
    "<P CLASS=LARGER>" +
    "<FORM ACTION=http://localhost:8080/examples/servlet/UpdateProcessServlet METHOD=POST>"
    "<INPUT TYPE=SUBMIT NAME=submitButton Value=submit>" +
    "</BODY> " +
    "</HTML>" );
    #2 This should get called and print me "Iam in doPost method
    public void doPost(HttpServletRequest request,HttpServletResponse response)
    throws ServletException,IOException
    url = "jdbc:odbc:Resume";
    System.out.println("Iam in doPost method");
    response.setContentType("text/html");
    out = response.getWriter();
    out.println("This is the last servlet for this project:");
    bool=false;
    check=false;
    Thank...:)

  • What is the difference b/w ALV Function Module and ALV Methods?

    Hello Friends,
          Can anybody help me in finding out the difference between ALV Function Modules and ALV methods?
    Thanks & Regards
    Sathish Kumar

    Hi Sathish,
    Plz go through this info. It is very useful.
    hi,
    chk these excellent links.
    http://www.geocities.com/mpioud/Abap_programs.html
    http://www.sapdevelopment.co.uk/reporting/reportinghome.htm
    Simple ALV report
    http://www.sapgenie.com/abap/controls/alvgrid.htm
    http://wiki.ittoolbox.com/index.php/Code:Ultimate_ALV_table_toolbox
    http://www.sapgenie.com/abap/controls/alvgrid.htm
    http://wiki.ittoolbox.com/index.php/Code:Ultimate_ALV_table_toolbox
    ALV
    1. Please give me general info on ALV.
    http://www.sapfans.com/forums/viewtopic.php?t=58286
    http://www.sapfans.com/forums/viewtopic.php?t=76490
    http://www.sapfans.com/forums/viewtopic.php?t=20591
    http://www.sapfans.com/forums/viewtopic.php?t=66305 - this one discusses which way should you use - ABAP Objects calls or simple function modules.
    2. How do I program double click in ALV?
    http://www.sapfans.com/forums/viewtopic.php?t=11601
    http://www.sapfans.com/forums/viewtopic.php?t=23010
    3. How do I add subtotals (I have problem to add them)...
    http://www.sapfans.com/forums/viewtopic.php?t=20386
    http://www.sapfans.com/forums/viewtopic.php?t=85191
    http://www.sapfans.com/forums/viewtopic.php?t=88401
    http://www.sapfans.com/forums/viewtopic.php?t=17335
    4. How to add list heading like top-of-page in ABAP lists?
    http://www.sapfans.com/forums/viewtopic.php?t=58775
    http://www.sapfans.com/forums/viewtopic.php?t=60550
    http://www.sapfans.com/forums/viewtopic.php?t=16629
    5. How to print page number / total number of pages X/XX in ALV?
    http://www.sapfans.com/forums/viewtopic.php?t=29597 (no direct solution)
    6. ALV printing problems. The favourite is: The first page shows the number of records selected but I don't need this.
    http://www.sapfans.com/forums/viewtopic.php?t=64320
    http://www.sapfans.com/forums/viewtopic.php?t=44477
    7. How can I set the cell color in ALV?
    http://www.sapfans.com/forums/viewtopic.php?t=52107
    8. How do I print a logo/graphics in ALV?
    http://www.sapfans.com/forums/viewtopic.php?t=81149
    http://www.sapfans.com/forums/viewtopic.php?t=35498
    http://www.sapfans.com/forums/viewtopic.php?t=5013
    9. How do I create and use input-enabled fields in ALV?
    http://www.sapfans.com/forums/viewtopic.php?t=84933
    http://www.sapfans.com/forums/viewtopic.php?t=69878
    10. How can I use ALV for reports that are going to be run in background?
    http://www.sapfans.com/forums/viewtopic.php?t=83243
    http://www.sapfans.com/forums/viewtopic.php?t=19224
    11. How can I display an icon in ALV? (Common requirement is traffic light icon).
    http://www.sapfans.com/forums/viewtopic.php?t=79424
    http://www.sapfans.com/forums/viewtopic.php?t=24512
    12. How can I display a checkbox in ALV?
    http://www.sapfans.com/forums/viewtopic.php?t=88376
    http://www.sapfans.com/forums/viewtopic.php?t=40968
    http://www.sapfans.com/forums/viewtopic.php?t=6919
    Go thru these programs they may help u to try on some hands on
    ALV Demo program
    BCALV_DEMO_HTML
    BCALV_FULLSCREEN_DEMO ALV Demo: Fullscreen Mode
    BCALV_FULLSCREEN_DEMO_CLASSIC ALV demo: Fullscreen mode
    BCALV_GRID_DEMO Simple ALV Control Call Demo Program
    BCALV_TREE_DEMO Demo for ALV tree control
    BCALV_TREE_SIMPLE_DEMO
    BC_ALV_DEMO_HTML_D0100
    OOPs:
    Check this for basic concepts of OOPS
    https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/library/webas/abap/abap%20code%20samples/abap%20objects/abap%20code%20sample%20to%20learn%20basic%20concept%20of%20object-oriented%20programming.doc
    https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/library/webas/abap/abap%20code%20samples/alv%20grid/abap%20code%20sample%20to%20display%20data%20in%20alv%20grid%20using%20object%20oriented%20programming.doc
    Tabstrip
    https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/library/webas/abap/abap%20code%20samples/alv%20grid/abap%20code%20sample%20for%20tab%20strip%20in%20alv.pdf
    Editable ALV
    https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/library/webas/abap/abap%20code%20samples/alv%20grid/abap%20code%20sample%20to%20edit%20alv%20grid.doc
    Tree
    http://www.sapdevelopment.co.uk/reporting/alv/alvtree/alvtree_usrint.htm
    General Tutorial for OOPS
    https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/library/webas/abap/an%20easy%20reference%20for%20alv%20grid%20control.pdf
    Rewords some points if it is helpful.
    Rgds,
    P.Naganjana Reddy

  • Can we have more than one service method in a single servlet

    can we have more than one service method in a single servlet?explain with example

    No, you can only have one service method. However, you can have it process two different requests as follows.
    Lets say you have two JSP pages. Put a hidden text value in one called jsp1 and a hiddent text value in the other called jsp2 (example: <input type="hidden" name="jspPage" value="jsp1"> and <input type="hidden" name="jspPage" value="jsp2">.
    Then in your service method, read in request.getParameter("jspPage") to determine if its jsp1 or jsp2 and call up the appropriate logic to process them separately.

  • URGENT: Getting app module service method result and calling web service??

    We have a .jar from another vendor that allows us to interact with an FOP rendering process. This .jar has methods that allow us to pass several parameters which results in the creation of the PDF which is stored in the database.
    We need to create the XML, which we've done, via view objects and a method in our application module. The developers at this point, have placed the code to call the service method into the model code that generates the XML. The actual .jar file for this is in the viewcontroller area.
    Is this a "reasonable" approach? ( that is, importing view object jar in model)
    The web service needs to have several parameters sent to it, most of which are configured in the web.xml. They are now being passed via a data action to the model's service method.
    Shouldn't the data action be written more like the following - note: some questions in here as well...
    public void onShowPdf( DataActionContext ctx )
    // note, you can get here from 5 different places in the application and we need to be able to retrieve the bindings from these separate places for proper generation... how??
    AppModule am = (AppModule) getAm();
    String strXML = am.getXML(...);
    // get web.xml values from session context...
    PDFArchiver stub = new PDFArchiver();
    stub.generate( send prms here );
    // the next page just needs to have 3 of the parameters exposed from this page to yield the results of the generated pdf (retrieve from db)
    // what would be the best way to get the parameters over there?
    } // onShowPDF
    after this data action, it simply forwards to the next page, which has a frame to display the pdf with an [ok] button at the bottom. The pdf is rendered via a Servlet.
    one last question...
    Right now, the service method was dropped on the data action, and it is automatically invoked ( we've bound the 8 parameters to it by over-riding initializeMethodParameters). If we were to switch it so the method RETURNED the XML, how would we get the results back while INSIDE A DATA ACTION? I could see if we called it by invoking a service method from the app module instance, but how do you do it in the lifecycle of the data action??
    Whew, that's all the questions for now.
    Any help, again, would be GREATLY appreciated.

    Most of the items in this previous post have been addressed. The one problem that remains is that we use a servlet to display the PDF. The servlet reads 3 parameters from the URL to send to a service method that reads and streams the PDF to the browser.
    HOWEVER, the user can view source and then paste parameters into the URL and view whatever he wants. So:
    1. How can we prevent the servlet parameters from being seen inside the source? OR, how can we simply prevent the user from invoking the view pdf servlet with other parameters?
    2. We've temporarily, written a data action (displayPDF) which has no service method. In the findForward data action, we grab the bindings that the servlet needs from other pages (4 different binding containers), then invoke the service method and write the data to the response object. This seems to work. So, our page has an iframe with the src="displayPDF.do".
    Again, should this be done another way? Seems strange to have a data action that "acts like a servlet"...
    Thanks for feedback.

Maybe you are looking for

  • Importing iTunes from Old Computer to a New One

    I want to import my old iTunes (files, name changes, data etc) to a new computer. However, iTunes is having toruble locating the files (obviously, since now the music files are in a different folder in the new computer...there was no way for me to re

  • Problem in Customer Interaction Center

    Hi All, I have an issue related to CIC win client. While using the Customer Interaction Centre, enter Partner number and performs a search, the sold to party and bill to party customers brings up the incorrect details. Where should I need to check re

  • A possible dumb question - capture HDV

    should i be able to hear the audio on the computer while doing capture from an HDV camcorder? Message was edited by: Forrest Jerome1

  • Raid-0 Stripe & Cluster Size

    I just ordered 2 10k RPM Raptor S-ATA Drives from newegg, they should arrive shortly. I plan to configure my system with them as Raid-0 for increased performance, I just read the "Raid Setup Guide 865/875 LSR/FIS2R Rev 1.01" by Vango and it seems tha

  • Trouble importing LiveType into FCE

    I am trying LiveType for the first time with my video. I have created the file in LiveType and then tried to import it into FCE. When I view the background in the viewer it looks great. When I drag it to my movie and try Insert I get an error that sa