Calling 3 different servlets from a single servlet

hi all,
i am in lot of soup...i am new to servlets,etc....so help me pls.
i need to call 3 different servlets from one servlet. all 3 servlets that i want to call perform different functions and execute queries on the database.
what i am trying to do is:
there is an html page that call a servlet called authenticate.java.(whose code is at the bottom of this page).
the servlet authenticate.java should authenticate a user according to the password and username supplied in the html page.
MAINLY:-
i need to display 3 buttons to the authenticted user. those 3 buttons each should call 3 different servlets, which do different things.
now in authenticte.java i set certain information in the session using session.putValue();
further, whatever servlet is called, i need to use that information i set into the session...using session.getValue().
PLS TELL ME HOW TO DO THAT...ALSO C IF THE BELOW CODE IS OK.
ALSO TELL ME HOW TO END A SESSION. ALSO DO SERVLETS SUPPORT SESSIONS?
I AM USING jdk1.3 and jsdk2.0.
* Authenticate.java
// this places all user variables in session
//to access use session.getAttribute
//dispatcher stuff
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import java.sql.*;
import java.io.*;
import java.util.*;
* @author Gudiya
* @version
public class Authenticate extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
HttpSession session = request.getSession(true);
Connection con=null;
String sessionid;
String deptcode;
String desig;
String add1;
String add2;
String contname;
PrintWriter out = response.getWriter();
try
{System.out.println(1);
response.setContentType("text/html");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:SACFA","","");
System.out.println("connection established");
catch(ClassNotFoundException e){
System.out.println("database driver not found");
System.out.println(e.toString());
catch(Exception e){System.out.println(e.toString());
String username=request.getParameter("username");
String password=request.getParameter("password");
try{
System.out.println(2);
PreparedStatement stmt=con.prepareStatement("SELECT * FROM AUTHENTICATION,mb_add where AUTHENTICATION.NAME=? AND AUTHENTICATION.PASSWORD=? AND mb_add.DEPT_NAME=?");
stmt.setString(1,username);
stmt.setString(2,password);
stmt.setString(3,username);
ResultSet rs=stmt.executeQuery();
System.out.println(3);
boolean rowfound=false;
rowfound=rs.next();
if (rowfound==true)
sessionid=session.getId();
System.out.println(sessionid);
System.out.println(4);
session.putValue("user",username);
rs.beforeFirst();
while (rs.next())
System.out.println(5);
deptcode=rs.getString("DEPT_CODE");
desig=rs.getString("DESIGNATION");
add1=rs.getString("ADDRESS1");
add2=rs.getString("ADDRESS2");
contname=rs.getString("NAME");
session.putValue("dept_code",deptcode);
session.putValue("designation",desig);
session.putValue("address1",add1);
session.putValue("address2",add2);
session.putValue("cont_name",contname);
System.out.println(6);
}          out.println("<html>");
          out.println("<head>");
          out.println("<title>Successful Login Screen</title>");
          out.println("</head>");
          out.println("<body bgcolor='ORANGE'>");
out.println("WELCOME SACFA MEMBERS FROM "+ username);
//print decorative html statements here
          out.println(" SELECT ANY ONE ACTION:-");
//out.println("<form Action="+"\"servlet/noc\""+" method =post>");//call for your noc servlet
//print decorative html statements here
out.println("<INPUT TYPE=submit VALUE='GENERATE NOC' NAME='NOC'>");
out.println("</form>");
//print decorative html statements here
//out.println("<form Action="+"\"servlet/comments\""+" method =post>");//call for your comment servlet
out.println("<INPUT TYPE=submit VALUE='ENTER/ MODIFY COMMENTS' NAME='COMMENTS' >");
out.println("</form>");
//print decorative html statements here
// out.println("<form Action="+"\"servlet/bye\""+" method = post>");
out.println("<INPUT TYPE=submit VALUE=EXIT NAME=EXIT >"); //place javascript fucction for exiting window
out.println("</form>");
          out.println("</body>");
          out.println("</html>");
con.close();
} catch( SQLException e){ }
out.close();}
THANK YOU,
REGARDS,
ASHNA

Hi Ashna,
You have use three different forms for three buttons. But I think all are commented one.It will also work if you uncomment it.
Else you can use only one Form without giving action in form tag. Use normal buttons instead of submit type buttons & call different JavaScript functions on onClick event for each button.
Try this out.
Ajay

Similar Messages

  • Calling different pages in a single sap script based on conditions?

    Hi All,
             Can anyone please give me an example of how to call different pages in a single sap script based on condition. Eg., i need to call 5 differnet pages from a single sap script based on 5 company codes.
    Please help
    Regards
    Priya

    This approach to make call from SAPscript. Its concept is similar to make call to a subroutine in another program. I would presume you understand how to use USING and CHANGING parameter. =)
    SAPscript -
    /: Perform get_date in program z_at_date
    /:    using &p_year&
    /:    changing &new_date&
    /: endperform.
    program z_at_date -
    form get_date TABLES rec_in  STRUCTURE itcsy
                                    rec_out STRUCTURE itcsy..
    DATA:
       v_year type char10.
    sap script and subroutine uses itcsy structure to transmit parameters
    first parameter is incoming while second parameter is out going
    their function is like an internal table with header line
    all data types between SAPscript and subroutine are string.
    so, you might need additional conversion.
    read incoming parameter with exact name from SAPscript
      READ TABLE rec_in WITH KEY name = 'P_YEAR'.
      IF sy-subrc EQ 0.
        v_year = rec_in-value.
      ENDIF.
    to return value, use the exact name on the second structure
        CONCATENATE v_year v_year INTO v_year.
        READ TABLE rec_out WITH KEY name = 'NEW_DATE'.
        IF sy-subrc EQ 0.
          rec_out-value = v_year.
          MODIFY rec_out TRANSPORTING value WHERE name = 'NEW_DATE'.
        ENDIF.
    endform.
    Hope this helps =)

  • Invoking multiple servlets from a single request

    Hi. I'm wondering if it's possible to execute multiple servlets within a single request and context. I have a servlet defined in web.xml which says:
    <servlet>
    <servlet-name>firstServlet</servlet-name>
    <servlet-class>mypackage.First</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>firstServlet</servlet-name>
    <url-pattern>/*</url-pattern>
    </servlet-mapping>
    (clearly this one gets invoked upon every request in the context)
    But I also have another servlet which gets invoked when another condition applies:
    <servlet>
    <servlet-name>secondServlet</servlet-name>
    <servlet-class>mypackage.Second</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>secondServlet</servlet-name>
    <url-pattern>*.ftl</url-pattern>
    </servlet-mapping>
    So if the url was http://localhost:8080/mycontext/page.ftl, I want both servlets to get executed. Is there anyway I can do this? I've tried the above code and it doesn't work. How would I do this (or can I even do it)?
    Thanks,
    Dylan

    depending on what your servlets do, you might also think of using filters. say you have a first servlet that compresses the respone you would be better off to implement this as a filter. than you might have a second serlvet that does request logging stuff. this might also be better than in a filter, so you chain first and second filter. the business logic is put in servlet three i.e.

  • How to call different templates from EBS 11i?

    Hi,
    I have three templates having same layout but different page width and heigth.
    How can i call these three templates conditionally from main template in EBS.
    Need urgent help
    Thanks,
    Mahesh

    Register them as subtemplates in Main template using syntax given below
    <?import:xdo://SDS.SDSCOMINVHDBG.en.US?>
    (<?import:xdo://<Application short name>.<Template name as registered>.<language>.<Territory>?>)
    and call them in main template based on condition as relevant.
    Thanks
    Kamalakar.G

  • Can I stream to two totally different sources from a single instance of FMLE?

    I am a DJ and stream live video to a site called Mixtube.dj, and recently I have joined another site called mxiify.com that also allows for video streaming, when I do my shows I usually simocast them on a few different sites. WIth audio only it isn't a big deal, I use a program called simplecast that allows for multiple streams at once, and until recently I only had the one site where I needed video, but now that I have two places that allow video, I was hoping I could take advantage of the video features on both sites. From what I have read it seems possible, however I cannot find instructions on how to do it exactly how to do this. I've seen hints where it seems like I need to add a % inbetween the URLs and streams, but I still haven't found concrete instructions. Has anyone done this, or remember where they saw the instructions for setting it up?

    i have the same type of issue.  I have an iphone and my wife an ipod touch.  we use seperate accounts however we would both like to stream to the same pc.
    The only way i've found to do it so far is to log out of your cloud in the control panel and log in to hers.  This will disable your photo streaming until you log back out of hers and into yours.  you have to remember to check the photo streaming box.
    I wish someone would tell us how to leave 2 clouds open at the same time.  that would save a lt of time logging in and out.
    Hope this helps somewhat.  it's not convenient but at least you'll be able to get your pictures onto your pc.  you will not however be able to stream from one device to the next.  you will have to transfer the photos into the upload file to get them from say your iphone onto her ipad

  • Payment terms for different products from a single vendor

    Hi Pundits,
    Our client has a requirement to assign different payment terms for different products. Each product he buys from his vendor has it's own payment terms. He wants us to customize in such a way that when he/user enters the invoice, not only the vendor number is shown along with the product name but also the payment terms for that particular product are calculated.
    I know that only one payment term can be assigned in the vendor master data. Is there anyway to get around this?
    Creating the same vendor multiple times and assign payment terms looks a possible option but that looks quite hectic cause he has close to 1000 payment terms.

    Taurian, thank you for that suggestion!
    I can maintain as many existing payment terms as the client has with his vendors and let him choose the payment terms at the time of invoice entry.
    The only problem is that he might have to change it according to the current market rates for that product. So maintaining any payment term is of little use.
    Edited by: Dummy_Variable on Aug 16, 2011 8:26 AM

  • How can i call a servlet from a servlet ?

    Hello,
    Can i call a Servlet from within a Servlet ?
    and is it "right thing" to Do ?
    Thanks

    JMO, but I wouldn't do it like this.
    Don't have a remote object like a servlet doing database queries for your app. That will mean TWO network hops for every query if the database resides on another machine. The network is the biggest bottleneck you've got.
    Write a JavaBean that does the database stuff for you and just have the first servlet instantiate it when it needs it. A Bean has a better chance of being reused, too. Write a TableGateway or DAO for your object.
    Or write an EJB to do it.
    The only time I've done a servlet-to-servlet connection like that was for one servlet running in a DMZ that would authenticate a user and then forward the request to another servlet running inside the second firewall.
    I wouldn't encapsulate database query logic in a servlet like that. JMO - MOD

  • Executing a Perl Script from within a Servlet

    I'm trying to call a script from within a servlet.This is the code i'm using:
    Process process = Runtime.getRuntime().exec(new String[]{"sh","-c",script,nick,pass});
    InputStream input = process.getInputStream();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int i;
    while ((i=input.read())!=-1) {
    baos.write(i);
    System.out.println(baos.toString());
    I can`t make it work.This is the output i'm getting.
    java.io.IOException: CreateProcess: sh -c /home/script.sh user pass error=2
    at java.lang.Win32Process.create(Native Method)
    at java.lang.Win32Process.(Win32Process.java:66)
    at java.lang.Runtime.execInternal(Native Method)
    at java.lang.Runtime.exec(Runtime.java:551)
    at java.lang.Runtime.exec(Runtime.java:477)
    at java.lang.Runtime.exec(Runtime.java:443)
    at controllers.EmailAcountCreate.doPost(EmailAcountCreate.java:34)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
    Thanks in advance.

    I am facing the same problem. Could you please post the answer if you have one now. Please treat this as very urgent.

  • Load different xml structure in single flash file

    Hi to all,
    How can i call different xml structures in single file..
    actually i am looking for 2 different menu galleries in single
    file.. first menu contains.. some 4 to 5 buttons.. done with this..
    in other flash file..having some of 4-5 buttons.. and these
    buttons.. to show in main flash file.. can i load 2 different swf
    files in single main file and with different xml structure please
    help me on this.. thanks in advance.
    Best Regards
    Satish Kumar Rajula

    yes, you can use one xml instance with an if-statement, but
    it's easier and cleaner to use different xml instances and
    different parsing code for each xml file loaded that has different
    structure.

  • Update 2 different databases from Timesten cache

    Is it possible to udpate 2 different databases from a single Timesten configured cache?.
    Edited by: user6867140 on Feb 12, 2009 1:06 PM

    Two different oracle instances.
    Let me give an example
    Table A is present in Oracle database 1
    Table B is present in Oracle database 2
    Table A is cached in TimesTen
    Any change in Table A in timesten need to be updated in Table A of Oracle database 1 and Table B of Oracle database 2
    Table A and Table B has the same data structure.

  • Calling servlet from a backing bean

    hi guys i need to call a servlet from a backing bean in JSF. how do i do that ?
    The scenario is i have to call servlet on a different machine from my backing bean . I also need to pass an object to the servlet .
    This object contains data which must be manipulated and inserted in the database by the calling servlet.
    I also want that after insertion in the db the control must be passed to the backing bean .
    Is this all possible???
    Please help

    You may want to investigate the built in java.net.URL class. For advanced needs, Apache has a Java HTTP client library.

  • Calling servlet from java page flow

    Hi,
    I need to call servelt class from the <netui:anchor ..> tag of my jpf.
    i have mapped this servlet class in the web.xml.
    please tell us how to invoke servlet from the netui:anchor tag from the jpf action
    method.
    thanks
    shashi

    I am calling a Servlet from JAVA Class as I am using IDE: Eclipse. The Problem is below
    URL url = new URL(ServletPath);
    URLConnection connet = url.openConnection();Why should the Servlet running in different Runtime print on your Eclipse Runtime. Please be more clear about the question.

  • Calling Servlet from Java Class in eclipse

    Hi ,
    I am calling a Servlet from JAVA Class as I am using IDE: Eclipse. The Problem is below
    URL url = new URL(ServletPath);
    URLConnection connet = url.openConnection();
    I am using the above code in JAVA to Connect to Servlet I have given Print statement in Servlet such that whenever it calls it prints on Console but when I try to run the code the Statement is not printed in the Console of eclipse I think URL connection is not able to establish any connection ..... if any has solution for this please reply
    Thanks & Regards,
    Arvind

    I am calling a Servlet from JAVA Class as I am using IDE: Eclipse. The Problem is below
    URL url = new URL(ServletPath);
    URLConnection connet = url.openConnection();Why should the Servlet running in different Runtime print on your Eclipse Runtime. Please be more clear about the question.

  • Can I call a "function" from a servlet?

    A few of my servlets need to perform about the same computations (have large sections of identical code).
    It would save some disk space to be able to call a procedure from different servlets.
    Can this be done? How?
    Thank you all.

    A few of my servlets need to perform about the same
    computations (have large sections of identical code).
    It would save some disk space to be able to call a
    procedure from different servlets.
    Can this be done? How?This wouldn't be the way to do it.
    Since Java is an object oriented programming language, you should use it's concepts to implement a 'basic' class and than extend it. This means: Create a class that contains the basic procedures (you will never create an instance of this class) and then create all the serlvet classes by extending the 'mother' class. Like this you have the common code once in the 'mother' class and the specific code in the specific servlets.

  • Calling Java application from servlet

    Hi !
    I'm trying to run a Java application from within a servlet with Tomcat 4. I'm using the Runtime.getRuntime ().exec () method. So the application is run in a different JVM as a subprocess of the servlet. I use ObjectInputStream and ObjectOutputStream and a serializable object to enable communication between the servlet and the application.
    I tested the application and the serializable object with another Java application that works as the caller and it works fine. However, replacing the caller application with the servlet I get a StreamCorruptedException. The structure of the caller application and the servlet is the same.
    My questions are:
    - Is there something I should configure in Tomcat to create a subprocess ?
    - What is the cause of the StreamCorruptedException ? How do I get it with the servlet and not with the application ?
    - Should I use an environment with the call to Runtime.getRuntime ().exec () ? How do I use it ?
    - Is the called application forced to run in my servlet's context ?
    - Is there a better way to do this ?
    Thanks to all

    Here's my code:
    1. The serializable object:
    // Object Obj
    import java.io.*;
    public class Obj implements Serializable
    public int n;
    public Obj ()
    n = 0;
    public Obj (int n)
    this.n = n;
    public String toString ()
    return getClass ().getName () + " -> (n = " + n + ")";
    2. The application Sub (subprogram)
    // Application Sub
    import java.io.*;
    public class Sub
    private static File f;
    private static FileWriter fw;
    public static void main (String [] args)
    throws IOException, InterruptedException, ClassNotFoundException
    ObjectInputStream ois;
    ObjectOutputStream oos;
    Obj obj;
    ois = new ObjectInputStream (System.in);
    obj = (Obj) ois.readObject ();
    f = new File ("Sub.txt");
    fw = new FileWriter (f);
    fw.write (obj.toString ());
    fw.close ();
    oos = new ObjectOutputStream (System.out);
    oos.writeObject (obj);
    ois.close ();
    oos.close ();
    3. The application AMain (caller application)
    // Application AMain
    import java.io.*;
    class AMain
    private static File f;
    private static FileWriter fw;
    public static void main (String [] args)
    throws IOException, ClassNotFoundException
    Runtime r;
    Process p;
    ObjectInputStream ois;
    ObjectOutputStream oos;
    Obj obj, obj2;
    r = Runtime.getRuntime ();
    p = r.exec ("java Sub");
    oos = new ObjectOutputStream (p.getOutputStream ());
    obj = new Obj (5);
    oos.writeObject (obj);
    oos.flush ();
    B.comunica (obj);
    System.out.println ("AMain sends to Sub: " + obj.toString ());
    try
    p.waitFor ();
    catch (InterruptedException e)
    System.out.println ("Subprogram was interrupted");
    System.out.println (e.toString ());
    ois = new ObjectInputStream (p.getInputStream ());
    System.out.print ("Sub sends to AMain: ");
    obj2 = (Obj) ois.readObject ();
    System.out.println (" " + obj2.toString ());
    oos.close ();
    ois.close ();
    p.destroy ();
    4. The servlet SMain (the calling servlet)
    // Servlet SMain
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.sql.*;
    import java.io.*;
    import java.util.*;
    public class SMain extends HttpServlet
    public void doPost (HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
    Runtime r;
    Process p;
    ObjectInputStream ois;
    ObjectOutputStream oos;
    Obj obj, obj2;
    int state, i;
    res.setContentType ("text/html");
    ServletOutputStream out = res.getOutputStream ();
    out.println ("<html>");
    out.println ("<head><title>Sub</title></head>");
    out.println ("<body>");
    out.println ("Invoking subprogram...");
    out.println ("<br>");
    try
    r = Runtime.getRuntime();
    p = r.exec ("java -cp .;c:\\Programs\\Apache~1.0\\webapps\\SMain\\WEB-INF\\classes Sub");
    out.println ("...invoked<br>");
    oos = new ObjectOutputStream (p.getOutputStream ());
    obj = new Obj (5);
    oos.writeObject (obj);
    oos.flush ();
    out.println ("<br>SMain sends to Sub: " + obj.toString () + "<br>");
    try
    p.waitFor ();
    catch (InterruptedException e)
    out.println ("<br>Subprogram was interrupted<br>");
    out.println ("<br>" + e.toString () + "<br>");
    state = p.exitValue ();
    out.println ("<br>Subprogram state: " + state + "<br>");
    ois = new ObjectInputStream (p.getInputStream ());
    out.print ("<br>Sub sends to SMain: ");
    obj2 = (Obj) ois.readObject ();
    p.destroy ();
    catch (SecurityException e)
    out.println ("<br>SecurityException<br>");
    out.println ("<br>" + e.toString () + "<br>");
    catch (IOException e)
    out.println ("<br>IOException<br>");
    out.println ("<br>" + e.toString () + "<br>");
    catch (Exception e)
    out.println ("<br>Exception<br>");
    out.println ("<br>" + e.toString () + "<br>");
    out.println ("</body>");
    out.println ("</html>");
    So, as you can see, both application AMain and servlet SMain invoke application Sub and pass it the serializable object Obj. Oddly enough, application AMain works fine whereas servlet SMain throws a StreamCorruptedException exception.
    johnpoole said:
    �It's hard to guess what would cause the exception without seeing code, but the interaction between the processes would differ from that between two applications, because the servlet process is started with a different class loader. I'm not sure which one the jvm started by the call would use.�
    How can I enforce that a System classloader be used in the call to Runtime.getRuntime ().exec () ? (I mean by System classloader a classloader equals to the one applications are launched from console).
    johnpoole said
    �Is there a reason why you aren't starting the second process manually and then connecting to it on a port?�
    The idea is providing a Web interface for an application running in the server. The servlet is used to restrict access to this application but once access is granted (passing the servlet) the application should not be constrained.

Maybe you are looking for

  • New update how to play music on apple tv

    I just updated my iPhone 4 and now can't figure out how to play music or videos on my apple tv

  • New Mac Pro 8-core / D700 not much faster than an iMac... in PPro CC.

    So.... my very preliminary testing with our new Mac Pro using the plugin I use most (filmconvert -FC) anyway, shows that Premiere CC needs more optimization for the dual GPUs. In fact, I'd say the CPU utilization is not up to snuff either. I know FC

  • Drag & drop songs into iTunes?

    I used to be able to drag and drop songs into my iTunes, but I cannot any longer.  I'm using a PC and running the latest version of iTunes.  Can anyone help me figure out how? Is there some setting change?  Thanks!

  • Message when beat mapping

    I have used this in LP7 and it worked pretty cool. Now in LP8 I'm following the same procedure and get a "this would cause negative clocks" --- where may I have gone wrong?

  • ATO Model -Auto Scheduling

    If you closely see ,The Auto scheduling for the ATO Models are not supported by oracle . Once you configure the item using configurator and click on finish ,When it returns to OM ,I expected the Schedule ship dates populated ,But this did not happen