Solved

Help creating a Login servlet

Posted on 2003-11-23
4
631 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB Script to add site to Java Exception List 4 79
varialbe initialization 11 33
type mismatch (Object[] to double[] 4 24
Java: anonymous class 4 22
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

803 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