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 <[EMAIL PROTECTED]> 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: [email protected]
>> To unsubscribe, e-mail: [EMAIL PROTECTED]
>> For additional commands, e-mail: [EMAIL PROTECTED]
>>
>>
>
>
---------------------------------------------------------------------
To start a new topic, e-mail: [email protected]
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------
Finding fabulous fares is fun.
Let Yahoo! FareChase search your favorite travel sites to find flight and hotel
bargains.