Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » Apache Tomcat »

Re: Applet does not send request to servlet

Teh Noranis Mohd Aris

2007-01-17

Replies:

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 <teh_noranis@(protected)>
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@(protected)
To unsubscribe, e-mail: users-unsubscribe@(protected)
For additional commands, e-mail: users-help@(protected)

©2008 junlu.com - Jax Systems, LLC, U.S.A.