File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Login Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Login Servlet" Watch "Login Servlet" New topic
Author

Login Servlet

Mohini Dhanaskar
Ranch Hand

Joined: Mar 27, 2012
Posts: 55
Hello Sir,

It's my first project, want to create login servlet for employee.

i have created jsp but not getting the logic to create Servlet for the same.

The jsp code is as follows :


Habeeb Shaikh
Ranch Hand

Joined: Nov 23, 2008
Posts: 48
Hi mohini,
What you want to say exactly.Do you want to create a servlet ? how to use it? or anything else?
Mohini Dhanaskar
Ranch Hand

Joined: Mar 27, 2012
Posts: 55
i want to create a login servlet for the employee.
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
What, exactly, should this servlet do? No sense thinking about details (or even code) unless and until the requirements are clear.
Mohini Dhanaskar
Ranch Hand

Joined: Mar 27, 2012
Posts: 55
Sorry Sir for inconvenience,

There is a jsp called employee login in which the employee has to enter empid and password.

which has been check from database table EmpRegistration.

If the employee is registered and valid ,EmailFormat jsp will appear or else LoginError.jsp will appear.

hope this has given you some idea.
Habeeb Shaikh
Ranch Hand

Joined: Nov 23, 2008
Posts: 48
hi mohini, do not called me as sir,

1. Create a class LoginServlet.
2. Create 2 setter and getter method for empname and password.
3. In getter method you can write your any validation code like emp does not exit if it is not in database or any your simple arraylist.
4. Import LoginServlet into your .jsp class
5 Set value attribute of empname text field by using loginServlet.getEmpname()

I hope you understand it.let me know if any difficulty.
Mohini Dhanaskar
Ranch Hand

Joined: Mar 27, 2012
Posts: 55
thank you, i will try and let you know.
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Habeeb Shaikh wrote:
1. Create a class LoginServlet.
2. Create 2 setter and getter method for empname and password.
3. In getter method you can write your any validation code like emp does not exit if it is not in database or any your simple arraylist.
4. Import LoginServlet into your .jsp class
5 Set value attribute of empname text field by using loginServlet.getEmpname()

A servlet is a servlet, not a backing (or model) bean. It does not need getters and setters, and it should definitely not be used in a JSP page. The basic steps of the servlet code are: retrieve username and password from the request, use them to check for validity against the DB, and depending on that redirect to either of the JSP pages. Which of these steps are you having difficulties with?

Also note that you need to fix your HTML - while there are several input fields, there is no actual <form ...> ... </form> tag. Without that there'S no way to specify the URL of the servlet that the form should be submitted to, or that the HTTP method to be used is a POST.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1479
    
    6

Habeeb Shaikh wrote:hi mohini, do not called me as sir,

1. Create a class LoginServlet.
2. Create 2 setter and getter method for empname and password.
3. In getter method you can write your any validation code like emp does not exit if it is not in database or any your simple arraylist.
4. Import LoginServlet into your .jsp class
5 Set value attribute of empname text field by using loginServlet.getEmpname()

I hope you understand it.let me know if any difficulty.


Why should a servlet have a setter, getter methods. Servlet is a controller. What is the need of importing a servlet in jsp? You should never embed java code in jsp it's a very old practice and not at all recommended these days, use el/jstl/custom tags etc. Your process flow should be like this

1. Create a login page which has two textboxes to enter user id and password
2. The submit action of this page is mapped to a servlet
3. Servlet retrieves the value of those two boxes using getParameter method of request object.
4. You do the required validation in the servlet or using some helper classes.
5. Based on the validity of user credentials you redirect the user to the specific jsp/html page.


Swastik
Mohini Dhanaskar
Ranch Hand

Joined: Mar 27, 2012
Posts: 55
i have tried sir as you said,

but i am getting an error as follow :




and the servlet code is as folllow:


Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1479
    
    6

Mohini,

What code do you have in line#48 EmpLoginServlet?
Mohini Dhanaskar
Ranch Hand

Joined: Mar 27, 2012
Posts: 55
Nothing Sir.

Actually i didn't understand what should i write there,or should i use that if block or not.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1479
    
    6

It can't be nothing, look at the exception you are getting

java.lang.NullPointerException
EmpLoginServlet.processRequest(EmpLoginServlet.java:48)

and I don't think you need the following check



unless you have more than one submit button in the same page with different values.
Mohini Dhanaskar
Ranch Hand

Joined: Mar 27, 2012
Posts: 55
Ok, Sir


I will remove these lines from code.
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
It looks like you're storing passwords in cleartext in the DB; that's a huge No-No for security reasons!
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Mohini Dhanaskar , its better to separate Persistence logic from servlet. Why don't you go for MVC architecture?

kunal
Mohini Dhanaskar
Ranch Hand

Joined: Mar 27, 2012
Posts: 55
ok,

Sir,then what should i do to resolved this?
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1479
    
    6

Tim's and Kunal's suggestion is excellent. But what happened after removing that if check? Did it still not work?
Prashant Chindhade
Ranch Hand

Joined: Jan 19, 2011
Posts: 77
first try simple application like this.

Login.jsp file.


this is bean class which is used to hold the data. it is also called model class.
Employee.java


now write servlet. like this.
LoginServlet.java


try this.
Mohini Dhanaskar
Ranch Hand

Joined: Mar 27, 2012
Posts: 55
Swastik Sir, i removed that lines and run it ..it is displaying LoginError.jsp page.

But Sir,while running Employee Login page,there is no action after clicking signin button.

Not getting,where i am wrong.
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Time to do some debugging: Is the page being submitted, meaning: is the servlet being invoked? If so, what are the values of the parameters?

Be aware that we do not see your code, so we have no idea what might or might not be happening.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1479
    
    6

prashant,

You should never compare a String object like this



it should have been (use methods of String class)



Mohini,

What you are saying is little confusing. You said that it's displaying the login error page, that means it's being redirected from the servlet, right, and again you are saying nothing happens if you click on the submit button.
Prashant Chindhade
Ranch Hand

Joined: Jan 19, 2011
Posts: 77
Swastik Dey wrote:prashant,

You should never compare a String object like this



it should have been (use methods of String class)



Mohini,

What you are saying is little confusing. You said that it's displaying the login error page, that means it's being redirected from the servlet, right, and again you are saying nothing happens if you click on the submit button.


ya thanks for that. i will follow this now on.
Mohini Dhanaskar
Ranch Hand

Joined: Mar 27, 2012
Posts: 55
Swastik Sir,i mean to say that

if i run only that servlet alone it is displaying LoginError.jsp.

Bu when i run the EmployeeLogin jsp that time after clicking signin(submit) button there is
no respond.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1479
    
    6

Mohini,

By this what I understand is, when you are hitting the mapped url of the servlet in the browser you are getting the login error page, that may be possible because it may not be getting the query parameter values. As far as the second problem is concerned, probably we need to see the code of your login page, i.e form action, and the web.xml file to know whether the servlet mapping is correct or not.
Mohini Dhanaskar
Ranch Hand

Joined: Mar 27, 2012
Posts: 55
Yuppiii,, my servlet is running properly.

thank you Habeeb Sir, Kunal Sir, Prashant Sir..

and Special thanks to Tim Sir and Swastik Sir..

Thanks a lot.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1479
    
    6

You are most welcome Mohini.
Habeeb Shaikh
Ranch Hand

Joined: Nov 23, 2008
Posts: 48
Your are welcome mohini...
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16303
    
  21

Not to spoil the party, but the technical term for web applications with Login Servlets in them is "hacked", "pwned" or "exploited". I really wish that J2EE books wouldn't use Login Servlets as demonstrators. Java has a much more secure way of doing logins that doesn't require any user-written code at all.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
 
subject: Login Servlet