In Java, test string equality this way: if (name.equals(client)) or if
(!name.equals(client))...

Using == tests the identity of the two strings (they are the same object)
not the equality (two objects contain the same data).

HTH.

----------
From: &THgr;&egr;&oacgr;&dgr;&ohgr;&rgr;&ogr;&sfgr; &Kgr;&ugr;&rgr;&igr;&mgr;&agr;&lgr;&eeacgr;&sfgr; <[EMAIL PROTECTED]>
To: [EMAIL PROTECTED]
Subject: Problem in writing a Login servlet
Date: Saturday, April 24, 1999 12:46 PM

Hi. I am trying to write a very simple Login servlet, just for testing
purposes, but I have problems.
I 've constructed a sample form with username & password fields.
Whatever values I input in the username & password fields, even the
"correct" ones, such as "client" & "pass", the control of the program goes
to the following part of the code
....
else if(name != client || pasw != pass) {
        msg = "Your login name or password is inncorrect. Try again";
        .....

It doesn't seem to go through this part of the code
.......
    if(req.getParameter("ok") != null)
    {
      if(name == client & pasw == pass) {
.........}
......

Could you help me?

The problem is, I suppose, in the following part of the code:

.......
    if(req.getParameter("ok") != null)
    {
      if(name == client & pasw == pass) {
.........}
......

Here is the whole source code:

public class Login extends HttpServlet {

  String client = "client";
  String pass = "pass";

  public void init(ServletConfig config) throws ServletException
    {super.init(config);}

  protected void doGet(HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException
  {
    res.setContentType("text/html");
    res.setHeader("pragma", "no-cache");
    PrintWriter out = res.getWriter();
    out.println("<HTML><HEAD><TITLE>Form</TITLE></HEAD>");
    out.println("</UL><HR><FORM METHOD=POST>");
    out.println("Username: <INPUT TYPE=TEXT NAME=username><BR>");
    out.println("Password: <INPUT TYPE=PASSWORD NAME=password><BR>");
    out.println("<BR>");
    out.println("<INPUT TYPE=SUBMIT NAME=ok VALUE=OK>");
    out.println("<INPUT TYPE=SUBMIT NAME=cancel VALUE=CANCEL>");
    out.println("</FORM><HR></BODY></HTML>");
    out.close();
  }


  protected void doPost(HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException
  {
    String name = req.getParameter("username");
    String pasw = req.getParameter("password");
    String msg = "";

    if(req.getParameter("ok") != null)
    {
      if(name == client & pasw == pass) {
        msg = "Welcome!";
        res.setContentType("text/html");
        res.setHeader("pragma", "no-cache");
        PrintWriter out = res.getWriter();
        out.println("<HTML><HEAD><TITLE>Form</TITLE></HEAD><BODY>");
        out.println(msg);
        out.println("</BODY></HTML>");
        out.close();
      }
      else if(name != client || pasw != pass) {
        msg = "Your login name or password is inncorrect. Try again";
        res.setContentType("text/html");
        res.setHeader("pragma", "no-cache");
        PrintWriter out = res.getWriter();
        out.println("<HTML><HEAD><TITLE>Form</TITLE></HEAD><BODY>");
        out.println(msg);
        out.close();
      }
    }
    else if(req.getParameter("cancel") != null) {
      msg = "Visit us again soon!";
      res.setContentType("text/html");
      res.setHeader("pragma", "no-cache");
      PrintWriter out = res.getWriter();
      out.println("<HTML><HEAD><TITLE>Form</TITLE></HEAD><BODY>");
      out.println(msg);
      out.println("<HR><A HREF=Welcome.html>Go Home</A><HR>");
      out.println("</BODY></HTML>");
      out.close();
    }
}

}

 Thanks for your time.

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in 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
  • ... Θεόδωρος Κυριμαλής
    • ... Doug Turner
    • ... Doug Turner
    • ... Θεόδωρος Κυριμαλής
    • ... shekar gowda
      • ... Hector Fabio Meza Martinez

Reply via email to