Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Login Servlet

 
Mohini Dhanaskar
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to create a login servlet for the employee.
 
Tim Moores
Bartender
Posts: 2947
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you, i will try and let you know.
 
Tim Moores
Bartender
Posts: 2947
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Mohini Dhanaskar
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohini,

What code do you have in line#48 EmpLoginServlet?
 
Mohini Dhanaskar
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing Sir.

Actually i didn't understand what should i write there,or should i use that if block or not.
 
Swastik Dey
Rancher
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, Sir


I will remove these lines from code.
 
Tim Moores
Bartender
Posts: 2947
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohini Dhanaskar , its better to separate Persistence logic from servlet. Why don't you go for MVC architecture?
 
Mohini Dhanaskar
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok,

Sir,then what should i do to resolved this?
 
Swastik Dey
Rancher
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2947
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are most welcome Mohini.
 
Habeeb Shaikh
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your are welcome mohini...
 
Tim Holloway
Saloon Keeper
Posts: 18303
56
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic