Results 1 to 7 of 7
  1. #1
    R O C K Y's Avatar
    R O C K Y is offline Member
    Join Date
    Feb 2009
    Posts
    19
    Rep Power
    0

    Question how to call servlet from the very first jsp page

    i need to call servlet as soon as i visited my first jsp page.

    i need a similar functionality like onLoad() function which calls the local function when the page is loading.

    to my knowledge i have seen that sevlets generally worked on submit button but for that we need to press the submit button and provide action for that.

    This time i need this kind of functionality for servlet when the first jsp page like welcome.jsp is being visited.

    any idea how to implement this................:confused:

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    You can invoke a servlet (a JSP page is compiled into a servlet by the Web application server) just by typing in its URL in the address bar, or referring to it with a standard link.

    When you do this, no data is sent in the request, so the servlet must be prepared for this. On the other hand, how hard is it to write code that expects no input?

  3. #3
    R O C K Y's Avatar
    R O C K Y is offline Member
    Join Date
    Feb 2009
    Posts
    19
    Rep Power
    0

    Question want to update and display the hit counter on my very first jsp page

    Quote Originally Posted by Steve11235 View Post
    You can invoke a servlet (a JSP page is compiled into a servlet by the Web application server) just by typing in its URL in the address bar, or referring to it with a standard link.

    When you do this, no data is sent in the request, so the servlet must be prepared for this. On the other hand, how hard is it to write code that expects no input?
    Dear member

    1.I want to update and display the hit counter on my very first jsp page.

    2.since the servlet contains the logic for updating the hit counter field so i need to call servlet first as soon as i loaded my first jsp page("welcome.jsp")

    3.Now do you have any solution for the above problem.....?:(

  4. #4
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    what steve is suggesting that you have visitors linked to the servlet, not the actual jsp

  5. #5
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    Let me clarify. The JSP *is* a servlet, once the WAS gets done with it. Just put the hit counter logic in the JSP. Ideally, you should create a hit counter class and use it from within your JSP. Don't make this complicated.

  6. #6
    fsunjive is offline Member
    Join Date
    Oct 2009
    Posts
    1
    Rep Power
    0

    Thumbs up Calling servlet on jsp load

    Hi ,

    you better get some or 1 attribute from servlet to help this out.The way to call the servlet when jsp load is " jsp tag " <jsp:forward page="/Test">

    here is a sample,
    <html>

    <%

    if(request.getAttribute("data") == null){


    %>

    <jsp:forward page="/Test" />

    <%

    }else {

    String data = request.getAttribute("data").toString();

    }
    %>

    </html>

  7. #7
    AmitB is offline Member
    Join Date
    Jul 2012
    Posts
    1
    Rep Power
    0

    Default Re: how to call servlet from the very first jsp page

    i know this post is too old, but for google searches:

    i think u should use :
    in WEB.XML:

    <servlet-mapping> // declare a servlet
    <servlet-name>YourCounterServlet</servlet-name>
    <url-pattern>/index</url-pattern>
    <url-pattern>/index.jsp</url-pattern>
    <url-pattern>/home</url-pattern> //etc.
    </servlet-mapping>
    <welcome-file-list>
    <welcome-file>YourCounterServlet</welcome-file> // if anyone just types: www . yourwebsite . com, this servlet will be loaded, so do ur counting here.
    </welcome-file-list>



    in ur YourCounterServlet

    first increment the counter,
    set the value in a bean (scope-application)
    then use:
    response.sendRedirect("new page URL");

    use the bean to display the hit count

Similar Threads

  1. using a link to call servlet
    By sijokunnappilly in forum Java Servlet
    Replies: 3
    Last Post: 11-12-2008, 03:22 PM
  2. Servlet should return a jsp page not html
    By hannehomuth in forum Advanced Java
    Replies: 1
    Last Post: 08-12-2008, 04:14 PM
  3. how can we call Logout servlet by closing window
    By nagesh1811 in forum Java Servlet
    Replies: 2
    Last Post: 07-11-2008, 08:41 AM
  4. Servlet calling JSP page
    By gapper in forum Java Servlet
    Replies: 1
    Last Post: 01-15-2008, 05:00 PM
  5. How to call JSP inside Servlet
    By Swamipsn in forum Advanced Java
    Replies: 0
    Last Post: 07-31-2007, 01:50 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
  •