Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » Servlet Interest »

Re: communicate servlet from jsp

Partha Ranjan Das

2004-06-28


Hi Shiv,

In a JSP page you can access the 'getServletConfig().getServletContext()' as
'application' object. So you can do the replacement accordingly. But I do
not know whether I have answered your question. You can always replace 2nd
servlet with JSP with that java code as scriptlet, can't you?

Also, I think it is better to keep such functions in helper java beans
classes rather than servlets, if not absolutely required. Servlets are
supposed to perform a separate duty.


Regards,
Partha

    ----------
    From: shiv juluru [SMTP:shivjuluru@(protected)]
    Sent: Friday, June 25, 2004 9:33 PM
    To: SERVLET-INTEREST@(protected)
    Subject: Re: communicate servlet from jsp

    hi partha,
    my problem is not like this,calling get and post methods of servlets
from jsps are working.
    this is not my problem.my problem is calling other than get,post
methods.
    i am sending some sample code.
    Servlet1.java
    -------------------
    import java.io.*;
    import javax.servlet.http.*;
    import javax.servlet.*;
    public class Servlet1 extends HttpServlet{
      public void doGet(HttpServletRequest req,HttpServletResponse
res)throws ServletException,IOException{
      PrintWriter out=res.getWriter();
      out.println("Hello this is Servlet1.....");
      getServletConfig().getServletContext().setAttribute("add",this);
     }
      public int add(int a,int b){

      return a+b;
     }
    }

    Servlet2.java
    ------------------
    import java.io.*;
    import javax.servlet.http.*;
    import javax.servlet.*;
    public class Servlet2 extends HttpServlet{
      public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException{
      PrintWriter out=res.getWriter();
      out.println("Hello i am Servlet2...");
      Servlet1
s=(Servlet1)(getServletConfig().getServletContext().getAttribute("add"));
      int a=s.add(4,5);
      out.println("the result is:"+a);
     }
    }


    these 2 are working fine.2nd servlet displaying the result.

    insteadof 2nd servlet i want jsp.

    plz. help me for this.

    thanks in advance
    shiva.


    Partharanjan Das <partharanjan.d@(protected):

          Hi,

          There are two ways of calling a servelt method from a jsp:

          1. for any public method on the servlet, you can call
getServlet() from
          the JSP for any servlet within the same container specifying
the name of the
          servlet as specified in the web.xml. This will however
bypass the http call
          interposition that is done by the container and you are
responsible for any
          synchronisation or any call interposition requirement. Not
recommended.
          2. You can make a URLConnection to the servlet from your JSP
and call
          doGet() or doPost(). The logic should be there in these two
methods.

          Of course, you can use request dispatcher for mundane uses.

          Hope this helps.

          Regards,
          Partha

          ----------
          From: Sireesha Beeram [SMTP:sireesha_beeram@(protected)]
          Sent: Wednesday, June 16, 2004 4:29 PM
          To: SERVLET-INTEREST@(protected)
          Subje ct: Re: communicate servlet from jsp

          To send the call to the servlet page:

          1.In your jsp page, put
          -->"aa" servlet's doPost() method
          will be
          called. From here you can direct the call to different
places

          or
          2. You can include java beans in the jsp page using->

          and you can call methods on this included bean
          ${bean.property}

          You can also check:

http://www.javaworld.com/javaworld/jw-05-2003/jw-0523-calltag.html

          hope this is what you are asking for..





          >From: Shiva Kumar
          >Reply-To: "A mailing list for discussion about Sun
Microsystem's
          Java
          > Servlet API Technology."
          >To: SERVLET-INTEREST@(protected)
          >Subject: communicate servlet from jsp
          >Date: Wed, 16 Jun 2004 01:29:22 -0600
          >
          >hi all,
          >
          >my servlet has some methods,and it has deployed in
webserver .i
          want to
          >call that servlet methods from a j sp page.
          >plz help me how to call servlet methods from a jsp.
          >the path is like this.webapps is webserver root folder.
          >individual jsp files and servlets are working fine.
          >
          >webapps--->*.jsp
          >webapps-->WEB-INF-->web.xml (description of all servlets)
          >webapps-->WEB-INF-->classes-->*.class (servlet classes)
          >
          >
          >
          >thanks in advance
          >shiva kumar
          >


>___________________________________________________________________________
          >To unsubscribe, send email to listserv@(protected)
include in
          the body
          >of the message "signoff SERVLET-INTEREST".
          >
          >Archives:
          http://archives.java.sun.com/archives/servlet-interest.html
          >Resources:
          http://java.sun.com/products/servlet/external-resources.html
          >LISTSERV Help: http://www.lsoft.com/manuals/user/user.html


_________________________________________________________________
          Getting marri ed? Find great tips, tools and the latest
trends at MSN
          Life
          Events. http://lifeevents.msn.com/category.aspx?cid=married



___________________________________________________________________________
          To unsubscribe, send email to listserv@(protected)
include in
          the body
          of the message "signoff SERVLET-INTEREST".

          Archives:
          http://archives.java.sun.com/archives/servlet-interest.html
          Resources:
          http://java.sun.com/products/servlet/external-resources.html
          LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

*********************************************************************
          Disclaimer: The information in this e-mail and any
attachments is
          confidential / privileged. It is intended solely for the
addressee or
          addressees. If you are not the addressee indicated in this
message, you may
          not copy or deliver this message to anyone. In such case,
you should destroy
          this message and kindly notify the sender by reply email. P
lease advise
          immediately if you or your employer does not consent to
Internet email for
          messages of this kind.

*********************************************************************


___________________________________________________________________________
          To unsubscribe, send email to listserv@(protected)
include in the body
          of the message "signoff SERVLET-INTEREST".

          Archives:
http://archives.java.sun.com/archives/servlet-interest.html
          Resources:
http://java.sun.com/products/servlet/external-resources.html
          LISTSERV Help: http://www.lsoft.com/manuals/user/user.html


      _____

    Do you Yahoo!?
    Yahoo! Mail
<http://us.rd.yahoo.com/mail_us/taglines/50x/*http://promotions.yahoo.com/ne
w_mail/static/efficiency.html> - 50x more storage than other providers!
___________________________________________________________________________
To unsubscribe, send email to listserv@(protected)
of the message "signoff SERVLET-INTEREST".

    Archives:
http://archives.java.sun.com/archives/servlet-interest.html Resources:
http://java.sun.com/products/servlet/external-resources.html LISTSERV Help:
http://www.lsoft.com/manuals/user/user.html

*********************************************************************
Disclaimer: The information in this e-mail and any attachments is
confidential / privileged. It is intended solely for the addressee or
addressees. If you are not the addressee indicated in this message, you may
not copy or deliver this message to anyone. In such case, you should destroy
this message and kindly notify the sender by reply email. Please advise
immediately if you or your employer does not consent to Internet email for
messages of this kind.
*********************************************************************

___________________________________________________________________________
To unsubscribe, send email to listserv@(protected)
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
©2008 junlu.com - Jax Systems, LLC, U.S.A.