From the statement of your question your purpose is to implement security. So keep in mind that in Java EE, containers are responsible for providing application security.
A container basically provides two types of security: declarative and programmatic. And it is good practice, whenever possible, to avoid reinventing the wheel at the risk of not taking advantage of the resources that language provides.
And consider the following:
The login form must contain fields to enter a username and password. These fields must be named j_username and j_password, respectively. The authentication form must post these values in the logical name j_security_check.
All of these names starting with j_ are standardized by the Java Servlet specification - we just need to follow the convention to allow automatic mechanisms to work.[Michal Cmil et al - 2014, 310 p.]
I believe in your case, change your form as follows:
<form method="post" action="j_security_check" >
<input type="text" name="j_username" >
<input type="password" name="j_password">
<input type="submit" name="submit">
<!-- Declaração de variaveis -->
<%!String logdefault = "admin";
String passdefault = "admin"; %>
<%
String login = null;
String password = null;
/**
* Procure evitar misturar lógica na camada de apresentação.
* Isso fere o designer Pattern MVC - Lógica fica no Controller.
*/
if(request.getParameter("login") != null
&& request.getParameter("password") != null){
login = request.getParameter("login");
password = request.getParameter("password");
if(login.equals("admin") && password.equals("admin")){
response.sendRedirect("index.jsp");
System.out.println("deu certo");
}else{
System.out.println("algo deu errado");
}
}
%>
</form>
Observing: I understood that this is code experimentation, however, it is common sense to avoid implementing logic in the presentation layer. This hurts designer Pattern MVC.
If it is necessary to implement some logic in the presentation layer, the correct one would be to use the Expression Language - EL.
The link between a JSF page and a Java support bean is made through EL. You can use EL instructions to print the value of variables, access the attributes of objects on a page, or invoke a support bean method.[Gonçalves, Antonio - 2013, 321 p.]
The basic syntax for an EL statement is #{expr}
The #{expr} statements will be analyzed and evaluated by JSF runtime. EL expressions can use most usual Java operators.
- Arithmetic: +, -, *, / (div), % (mod),
- Relational: == (eq),! = (ne), <(lt),> (gt), <= (le),> = (ge),
- Logical: && (e), || (or),! (no) and
- Other: (), empty, [] ,.
I hope I’ve helped.
Note: more details on security: here.
Reference:
[Juneau, Josh - 2013], Apress, 2013, Java EE 7 Recipes: A problem-Solution Approach - Proven Solutions for Java Enterprise Edition 7 Developement
[Gonçalves, Antonio - 2013], Apress, 2013, Beginning Java EE 7 (Expert Voice in Java)
[JSR 196 - JASPIC], JSR 196 - JASPIC: Javatm Authentication Service Provider Interface for Containers
[Anjana Mankale - 2013], Copyright 2013 Packt Publishing, Spring Security 3.x Cookbook: Over 60 Recipes to help you successfully Safghanuard your web Applications with Spring Security.
[Michal Cmil et al - 2014], Copyright 2014 Packt Publishing, Java EE 7 Development with Wildfly: Leverage the power of the Wildfly application server from Jboss to develop Modern Java EE 7 Applications.
Ronnysilva, I believe the answer here may help your question.
– pss1suporte