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.
  • ... Θεόδωρος Κυριμαλής
    • ... Doug Turner
    • ... Doug Turner
    • ... Θεόδωρος Κυριμαλής
    • ... shekar gowda
      • ... Hector Fabio Meza Martinez

Reply via email to