Solved

Help creating a Login servlet

Posted on 2003-11-23
4
629 Views
Last Modified: 2013-11-24
This is a chunk of my code from my LoginServlet.java:

      public void printLoginTable(PrintWriter out)

      {
            

            out.println("<h2>Login</h2>");
            out.println("<FORM METHOD=POST ACTION="/com/wrox/servlet/Authenticate1">");
            out.println("ID: <INPUT TYPE=TEXT NAME=uid SIZE=20><BR>");
            out.println("Password: <INPUT TYPE=PASSWORD NAME=pw SIZE=20><BR>");
            out.println("<P><INPUT TYPE=SUBMIT>");
            out.println("</FORM>");
              String userId = req.getParameter("uid");
              String password = req.getParameter("pw");
                bool validated = validate(userId,password);
                if(validated)
                 {
                           printLogged(out);
                 }
                 else
                 {
                          out.println("INVALID");
                          //then go back to some other page...
                 }
      }

basically its purpose is to create the HTML form to take in a user id and password, then the last two lines are supposed to take those two parameters and process those strings elsewhere. Am i doing this correctly?  I'm guessing not because i have to worry about the "onclick" action of an html form...but how do i make my req.getParameter wait for onclick? Or am i totally off?
0
Comment
Question by:jinduy
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 50 total points
ID: 9805687
Your main problem is a logical one. What you're doing is simultaneously creating a form to allow the user to log in and behaving as if a login has already taken place. If you are only just creating the form, how can

>>String userId = req.getParameter("uid");

be anything other than null, unless the login *has* taken place? And if it has, why print the login form?

What you should be doing is either using two different pages - one to print the form and another to receive it, or, if you're using the same page/servlet, you should be either printing the form or processing it, but not both.

You should really save the login in the session, so if you can't find it in the session, you know the user has to log in and you can print the login form.


0
 

Expert Comment

by:lapchern
ID: 9808658
oh yeah... userID will always be null =(


can you please elaborate more on saving the "login in the session..."

how do i save the login in a session? Do u mean saving the username of the login in the session? Thanks
0
 

Author Comment

by:jinduy
ID: 9808664
oops i replied with my lab partner's login just now...so yeah...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9810090
>>Do u mean saving the username of the login in the session?

Yes.

requestion.getSession().setAttribute("username", username);
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
hash value 2 38
bigHeights  challenge 13 56
Unable to start eclipse ? 17 87
Java / Linux and Regular Expressions 11 47
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now