You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Teh Noranis Mohd Aris <te...@yahoo.com> on 2007/01/17 08:42:25 UTC

Applet does not send request to servlet

Hi,
   
  I already solve the problem of loading my applet in the web browser. Thank you to all. Now, I'm having the problem to send request from the applet to the servlet. Attached is the AppletLogin.java (the servlet which I put in package myapp) and LoginApplet.java (the applet which I put in package myapp.applets). I use http://localhost:8080/myapp/AppletLogin to access the applet. The same username and password input should link to "Welcome to our site", "More cool stuff coming soon" but I got the "Invalid user - please try again" message. I tried to modify the codeBase variable in the init() method of the applet but could not link to the next page. I think that the applet cannot access the servlet in package myapp. The attached programs are only short programs in a book. I just want to organize the programs in my own directory. Does anyone have any other ideas why this is happening? Please help me! Thank you in advance.
   
  Yours Sincerely,
  TEH NORANIS  

 
---------------------------------
Need a quick answer? Get one in minutes from people who know. Ask your question on Yahoo! Answers.

Re: Applet does not send request to servlet

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Teh,

Teh Noranis Mohd Aris wrote:
> I've been working out the applet program since last
> night but still could not get it correct. I think that
> the codeBase variable path that was initialised in the
> applet program is not correct. I put the
> LoginApplet.class in
> C:\jakarta-tomcat-4.1.31\webapps\myapp\applets. It's
> just a matter of writing the codeBase variable the
> correct way.

If your class really is webide.applets.LoginApplet, then I think you
want to put your class file in
...\webapps\myapp\applets\webide\applets\LoginApplet.class

I'm not entirely sure how the Java plug-in loads classes, but the
docbase is probably like the classpath. Since the classpath is
"/myapp/applets", your class should be loadable there using its
fully-qualified class name (webide.applets.LoginApplet), which probably
means that it expects to be in a subdirectory.

Alternatively, you could put everything into a JAR file and use the JAR
file as the docbase (instead of a directory).

Hope that helps,
- -chris

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFrwpM9CaO5/Lv0PARAh0eAJ0atXt1kPWx6bS4t170N0fno7SMhACdEMEt
vPL/jM57XsOS0Eo6gR4MIkY=
=LQ9E
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Applet does not send request to servlet

Posted by Teh Noranis Mohd Aris <te...@yahoo.com>.
Hello,

I've been working out the applet program since last
night but still could not get it correct. I think that
the codeBase variable path that was initialised in the
applet program is not correct. I put the
LoginApplet.class in
C:\jakarta-tomcat-4.1.31\webapps\myapp\applets. It's
just a matter of writing the codeBase variable the
correct way. Can anyone please give me any
suggestions? Please Help Me! Hoping for a reply. Thank
you.

TEH NORANIS   
--- Teh Noranis Mohd Aris <te...@yahoo.com>
wrote:

> Hi,
>    
>   I'm sorry, I didn't realize about the attachment
> capability for this mailing list. However here are
> the programs that I attached previously. Hope
> someone can help me out. Thank you.
>    
>   1. THE SERVLET PROGRAM:
>    
>   package myapp;
>   import javax.servlet.*;
> import javax.servlet.http.*;
> import java.io.ByteArrayOutputStream;
> import java.io.DataInputStream;
> import java.io.DataOutputStream;
> import java.io.IOException;
> import java.io.PrintWriter;
> import java.util.Hashtable;
>   public class AppletLogin extends HttpServlet
> {
>  public static String USER_KEY =
> "ServletLogin.user";
>  
>  static Hashtable crossRef = new Hashtable();
>  
>  public void doGet(HttpServletRequest req,
> HttpServletResponse resp)
>    throws ServletException, java.io.IOException
>    {
>     resp.setContentType("text/html");
>     
>     PrintWriter out = resp.getWriter();
>     
>     resp.setHeader("Expires", "Tues, 01 Jan 1980
> 00:00:00 GMT");
>     
>     HttpSession session = req.getSession();
>     String user = (String)
> session.getAttribute(USER_KEY);
>     
>     if (user == null)
>     {
>         user = (String)
> crossRef.get(session.getId());
>         
>         if (user != null)
>         {
>          session.setAttribute(USER_KEY, user);
>          crossRef.remove(session.getId());
>         }
>     
>      
>     }
>    
>    if (user == null)
>    {
>     login(out, req, resp);
>     return;
>    }
>    
>    out.println("<html>");
>    out.println("<head>");
>    out.println("<title>Welcome</title>");
>    out.println("</head>");
>    out.println("<body>");
>    out.println("<center><h2>Welcome to our
> site!</h2>");
>    out.println("<br>");
>    out.println("More cool stuff coming soon...");
>    out.println("</center>");
>    
>    out.println("</body>");
>    out.println("</html>");
>    out.flush();     
>  }
>  
>  public void doPost(HttpServletRequest req,
> HttpServletResponse resp)
>    throws ServletException, java.io.IOException
>  {
>   DataInputStream in = new
> DataInputStream(req.getInputStream());
>   
>   resp.setContentType("application/octet-stream");
>   
>   ByteArrayOutputStream byteOut = new
> ByteArrayOutputStream();
>   
>   DataOutputStream out = new
> DataOutputStream(byteOut);
>   
>   String sessionId = in.readUTF();
>   
>   String user = in.readUTF();
>   String password = in.readUTF();
>     
>   if (!validUser(user, password))
>   {
>    out.writeBoolean(false);
>   }
>   else
>   {
>    crossRef.put(sessionId, user);
>    out.writeBoolean(true);
>    
>    String nextPage = req.getScheme() + "://" +
>    
>    req.getServerName() + ":" +
>    req.getServerPort() +
>    req.getRequestURI();
>    
>    out.writeUTF(nextPage);
>   }
>   
>   out.flush();
>   
>   byte[] buf = byteOut.toByteArray();
>   
>   resp.setContentLength(buf.length);
>   
>   ServletOutputStream servletOut =
> resp.getOutputStream();
>   
>   servletOut.write(buf);
>   servletOut.close();
>  }
>   
>   protected void login (PrintWriter out, 
>                         HttpServletRequest req, 
>                         HttpServletResponse resp)
>     throws java.io.IOException
>   {
>    HttpSession session = req.getSession();
>    
>    out.println("<html>");
>    out.println("<head>");
>    out.println("<title>Login</title>");
>    out.println("<center><h2>Welcome! Please
> login</h2>");
>    out.println("<applet width=300 height=120");
>    out.println("name=\"LoginApplet\"");
>    out.println("codebase=\"" + req.getContextPath()
> + "/applets/\"");
>   
> out.println("code=\"webide.applets.LoginApplet\">");
>    out.println("<param name=\"servlet\" value=\"" +
>                req.getRequestURI() + "\">");
>       out.println("<param name=\"id\" value=\"" +
>                   session.getId() + "\">");
>       out.println("</applet>");
>       out.println("</center></body></html>");       
>  
>   }
>   
>   protected boolean validUser(String username,
> String password)
>   {
>    boolean valid = false;
>    
>    if ((username != null) && (username.length() >
> 0))
>    {
>     valid = username.equals(password);
>    }
>    
>    return valid;
>    
>   } 
>      
> }
>    
>   2. THE APPLET PROGRAM:
>    
>   package myapp.applets;
>   import java.applet.*;
> import java.awt.*;
> import java.io.*;
> import java.net.*;
>   public class LoginApplet extends Applet 
> {
>  TextField username;
>  TextField password;
>  Label message;
>  Button login;
>  String codeBase;
>  String servlet;
>  String nextDoc;
>  String sessionId;
>  
>  public void init() 
>  {
>   codeBase = "" + getCodeBase();
>   servlet = getParameter("servlet");
>   
>   if (servlet != null)
>   {
>    if (servlet.startsWith("/") &&
>        codeBase.endsWith("/"))
>        {
>         codeBase =
> codeBase.substring(0,codeBase.length() - 1);
> 
=== message truncated ===



 
____________________________________________________________________________________
Finding fabulous fares is fun.  
Let Yahoo! FareChase search your favorite travel sites to find flight and hotel bargains.
http://farechase.yahoo.com/promo-generic-14795097

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Applet does not send request to servlet

Posted by Teh Noranis Mohd Aris <te...@yahoo.com>.
Hi,
   
  I'm sorry, I didn't realize about the attachment capability for this mailing list. However here are the programs that I attached previously. Hope someone can help me out. Thank you.
   
  1. THE SERVLET PROGRAM:
   
  package myapp;
  import javax.servlet.*;
import javax.servlet.http.*;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Hashtable;
  public class AppletLogin extends HttpServlet
{
 public static String USER_KEY = "ServletLogin.user";
 
 static Hashtable crossRef = new Hashtable();
 
 public void doGet(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, java.io.IOException
   {
    resp.setContentType("text/html");
    
    PrintWriter out = resp.getWriter();
    
    resp.setHeader("Expires", "Tues, 01 Jan 1980 00:00:00 GMT");
    
    HttpSession session = req.getSession();
    String user = (String) session.getAttribute(USER_KEY);
    
    if (user == null)
    {
        user = (String) crossRef.get(session.getId());
        
        if (user != null)
        {
         session.setAttribute(USER_KEY, user);
         crossRef.remove(session.getId());
        }
    
     
    }
   
   if (user == null)
   {
    login(out, req, resp);
    return;
   }
   
   out.println("<html>");
   out.println("<head>");
   out.println("<title>Welcome</title>");
   out.println("</head>");
   out.println("<body>");
   out.println("<center><h2>Welcome to our site!</h2>");
   out.println("<br>");
   out.println("More cool stuff coming soon...");
   out.println("</center>");
   
   out.println("</body>");
   out.println("</html>");
   out.flush();     
 }
 
 public void doPost(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, java.io.IOException
 {
  DataInputStream in = new DataInputStream(req.getInputStream());
  
  resp.setContentType("application/octet-stream");
  
  ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
  
  DataOutputStream out = new DataOutputStream(byteOut);
  
  String sessionId = in.readUTF();
  
  String user = in.readUTF();
  String password = in.readUTF();
    
  if (!validUser(user, password))
  {
   out.writeBoolean(false);
  }
  else
  {
   crossRef.put(sessionId, user);
   out.writeBoolean(true);
   
   String nextPage = req.getScheme() + "://" +
   
   req.getServerName() + ":" +
   req.getServerPort() +
   req.getRequestURI();
   
   out.writeUTF(nextPage);
  }
  
  out.flush();
  
  byte[] buf = byteOut.toByteArray();
  
  resp.setContentLength(buf.length);
  
  ServletOutputStream servletOut = resp.getOutputStream();
  
  servletOut.write(buf);
  servletOut.close();
 }
  
  protected void login (PrintWriter out, 
                        HttpServletRequest req, 
                        HttpServletResponse resp)
    throws java.io.IOException
  {
   HttpSession session = req.getSession();
   
   out.println("<html>");
   out.println("<head>");
   out.println("<title>Login</title>");
   out.println("<center><h2>Welcome! Please login</h2>");
   out.println("<applet width=300 height=120");
   out.println("name=\"LoginApplet\"");
   out.println("codebase=\"" + req.getContextPath() + "/applets/\"");
   out.println("code=\"webide.applets.LoginApplet\">");
   out.println("<param name=\"servlet\" value=\"" +
               req.getRequestURI() + "\">");
      out.println("<param name=\"id\" value=\"" +
                  session.getId() + "\">");
      out.println("</applet>");
      out.println("</center></body></html>");         
  }
  
  protected boolean validUser(String username, String password)
  {
   boolean valid = false;
   
   if ((username != null) && (username.length() > 0))
   {
    valid = username.equals(password);
   }
   
   return valid;
   
  } 
     
}
   
  2. THE APPLET PROGRAM:
   
  package myapp.applets;
  import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
  public class LoginApplet extends Applet 
{
 TextField username;
 TextField password;
 Label message;
 Button login;
 String codeBase;
 String servlet;
 String nextDoc;
 String sessionId;
 
 public void init() 
 {
  codeBase = "" + getCodeBase();
  servlet = getParameter("servlet");
  
  if (servlet != null)
  {
   if (servlet.startsWith("/") &&
       codeBase.endsWith("/"))
       {
        codeBase = codeBase.substring(0,codeBase.length() - 1);
       }
  }
  
 sessionId = getParameter("id");
 
 setBackground(Color.white);
 
 setLayout(new BorderLayout(0, 5));
 
 message = new Label();
 add("North", message);
 
 Panel p = new Panel();
 p.setLayout(new GridLayout(2, 2, 30, 20));
 
 p.add(new Label("Enter user name:"));
 username = new TextField(10);
 p.add(username);
 
 p.add(new Label("Enter password:"));
 password = new TextField(10);
 password.setEchoCharacter('*');
 p.add(password);
 
 add("Center", p);
 
 login = new Button("Login");
 add("South", login);
}
 
 public boolean handleEvent(Event event)
 {
  if ((event != null) && (event.id == event.ACTION_EVENT))
  {
   if (event.target == login)
   {
    message.setText("");
    
    String user = username.getText();
    String pw = password.getText();
    
    boolean valid = false;
    try
    {
     valid = validate(user, pw);
    }
    catch (Exception ex)
    {
     ex.printStackTrace();
    }
    
    if (!valid)
    {
     message.setText("Invalid user - please try again");
    }
    else
    {
     try
     {
      URL url = new URL(nextDoc);
      System.out.println("showing " + url);
      getAppletContext().showDocument(url);
     }
     catch (Exception ex)
     {
      message.setText("Invalid document: " + nextDoc);
      ex.printStackTrace();
     }
    }
   }
  }
  return false;  
 }
 protected boolean validate(String user, String pw) throws Exception
 {
  boolean valid = false;
  
  java.net.URL url = new java.net.URL(codeBase + servlet);
  
  java.net.URLConnection con = url.openConnection();
  
  con.setUseCaches(false);
  con.setDoOutput(true);
  con.setDoInput(true);
  
  ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
  
  DataOutputStream out = new DataOutputStream(byteOut);
  
  out.writeUTF(sessionId);
  
  out.writeUTF(user);
  out.writeUTF(pw);
  
  out.flush();
  
  byte buf[] = byteOut.toByteArray();
  
  con.setRequestProperty("Content-type", "application/octet-stream");
  
  con.setRequestProperty("Content-length", "" + buf.length);
  
  DataOutputStream dataOut = new DataOutputStream(con.getOutputStream());
  dataOut.write(buf);
  
  dataOut.flush();
  dataOut.close();
  
  DataInputStream in = new DataInputStream(con.getInputStream());
  
  valid = in.readBoolean();
  System.out.println("valid user=" + valid);
  
  if (valid)
  {
   nextDoc = in.readUTF();
   System.out.println("next document=" + nextDoc);
  }
  
  in.close();
  return valid;
 } 
}

   
  TEH NORANIS
  
Zack Grafton <za...@gmail.com> wrote:
  I would have to agree with the attachment capability of this mailing list.

Zack

Andre Prasetya wrote:
> i think attachments are disabled...
>
> On 1/17/07, Teh Noranis Mohd Aris wrote:
>>
>> Hi,
>>
>> I already solve the problem of loading my applet in the web browser. 
>> Thank
>> you to all. Now, I'm having the problem to send request from the 
>> applet to
>> the servlet. Attached is the AppletLogin.java (the servlet which I 
>> put in
>> package myapp) and LoginApplet.java (the applet which I put in package
>> myapp.applets). I use http://localhost:8080/myapp/AppletLogin to access
>> the applet. The same username and password input should link to 
>> "Welcome to
>> our site", "More cool stuff coming soon" but I got the "Invalid user -
>> please try again" message. I tried to modify the codeBase variable in 
>> the
>> init() method of the applet but could not link to the next page. I think
>> that the applet cannot access the servlet in package myapp. The attached
>> programs are only short programs in a book. I just want to organize the
>> programs in my own directory. Does anyone have any other ideas why 
>> this is
>> happening? Please help me! Thank you in advance.
>>
>> Yours Sincerely,
>> TEH NORANIS
>>
>> ------------------------------
>> Need a quick answer? Get one in minutes from people who know. Ask your
>> question on Yahoo! 
>> Answers. 
>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> To start a new topic, e-mail: users@tomcat.apache.org
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>>
>
>


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org



 
---------------------------------
Finding fabulous fares is fun.
Let Yahoo! FareChase search your favorite travel sites to find flight and hotel bargains.

Re: Applet does not send request to servlet

Posted by Zack Grafton <za...@gmail.com>.
I would have to agree with the attachment capability of this mailing list.

Zack

Andre Prasetya wrote:
> i think attachments are disabled...
>
> On 1/17/07, Teh Noranis Mohd Aris <te...@yahoo.com> wrote:
>>
>> Hi,
>>
>> I already solve the problem of loading my applet in the web browser. 
>> Thank
>> you to all. Now, I'm having the problem to send request from the 
>> applet to
>> the servlet. Attached is the AppletLogin.java (the servlet which I 
>> put in
>> package myapp) and LoginApplet.java (the applet which I put in package
>> myapp.applets). I use http://localhost:8080/myapp/AppletLogin to access
>> the applet. The same username and password input should link to 
>> "Welcome to
>> our site", "More cool stuff coming soon" but I got the "Invalid user -
>> please try again" message. I tried to modify the codeBase variable in 
>> the
>> init() method of the applet but could not link to the next page. I think
>> that the applet cannot access the servlet in package myapp. The attached
>> programs are only short programs in a book. I just want to organize the
>> programs in my own directory. Does anyone have any other ideas why 
>> this is
>> happening? Please help me! Thank you in advance.
>>
>> Yours Sincerely,
>> TEH NORANIS
>>
>> ------------------------------
>> Need a quick answer? Get one in minutes from people who know. Ask your
>> question on Yahoo! 
>> Answers<http://answers.yahoo.com/;_ylc=X3oDMTFvbGNhMGE3BF9TAzM5NjU0NTEwOARfcwMzOTY1NDUxMDMEc2VjA21haWxfdGFnbGluZQRzbGsDbWFpbF90YWcx>. 
>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> To start a new topic, e-mail: users@tomcat.apache.org
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>>
>
>


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Applet does not send request to servlet

Posted by Andre Prasetya <an...@gmail.com>.
i think attachments are disabled...

On 1/17/07, Teh Noranis Mohd Aris <te...@yahoo.com> wrote:
>
> Hi,
>
> I already solve the problem of loading my applet in the web browser. Thank
> you to all. Now, I'm having the problem to send request from the applet to
> the servlet. Attached is the AppletLogin.java (the servlet which I put in
> package myapp) and LoginApplet.java (the applet which I put in package
> myapp.applets). I use http://localhost:8080/myapp/AppletLogin to access
> the applet. The same username and password input should link to "Welcome to
> our site", "More cool stuff coming soon" but I got the "Invalid user -
> please try again" message. I tried to modify the codeBase variable in the
> init() method of the applet but could not link to the next page. I think
> that the applet cannot access the servlet in package myapp. The attached
> programs are only short programs in a book. I just want to organize the
> programs in my own directory. Does anyone have any other ideas why this is
> happening? Please help me! Thank you in advance.
>
> Yours Sincerely,
> TEH NORANIS
>
> ------------------------------
> Need a quick answer? Get one in minutes from people who know. Ask your
> question on Yahoo! Answers<http://answers.yahoo.com/;_ylc=X3oDMTFvbGNhMGE3BF9TAzM5NjU0NTEwOARfcwMzOTY1NDUxMDMEc2VjA21haWxfdGFnbGluZQRzbGsDbWFpbF90YWcx>.
>
>
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>


-- 
-Andre-

People see things the way they are and say "why ?" I see things that never
were and say "Why not ?"