Java Mailing List Archive

http://www.junlu.com/

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

Re: communicate servlet from jsp

Michael Weller

2004-06-26

Replies:

hi!
in a JSP:
<%= ((Servlet1)application.getAttribute("add")).add(1,2) %>
should do the trick...
but you don't have to use "attributes" to pass a servlet to be able to
call methods from that servlet...
2 ways:
1. make 'int add' static and call it like 'Servlet1.add(i1,i2)' in your
2nd servlet/jsp (sidenote: methods like your 'add' should always be
static, in my eyes, because 'add' doesn't rely on Servlet1's state...)
2. extract all methods you want to use in more than one servlet/jsp in a
utility class (e.g. MyUtils) and use that one (like any other class!!!)
in your servlets/jsp. Always remember: A servlet is just yet another
ordinary java class, it just implements Servlet... And jsps are just
another way to write servlets.

HTH!

-mw

shiv juluru wrote:

> 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)
>   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)
>   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)
>   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/new_mail/static/efficiency.html>
> - 50x more storage than other providers!
> ___________________________________________________________________________
> To unsubscribe, send email to listserv@(protected)
> 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
>

___________________________________________________________________________
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.