No result when sending data from a JSP form

Asked

Viewed 460 times

0

I’m trying to make a mini-login screen, get the form compare data and after that redirect to a page JSP.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Login
<form>
<input type="text" name="login">
<input type="password" name="password">
</form>
<form>
<input type="submit" name="submit">

<!-- Declaração de variaveis -->
<%!String logdefault = "admin";
String passdefault = "admin"; %>
<% String login = request.getParameter("login");
String password = request.getParameter("password"); %>
<!-- FIM declaração de variaveis -->

<%
if(login.equals("admin") && password.equals("admin")){
response.sendRedirect("index.jsp");
System.out.println("deu certo");
}else{
System.out.println("algo deu errado");
}
%>
</form>
</body>
</html>

I tried before to use the if compared to == but by clicking on submit nothing happened, trying to equals return that error to me

An Exception occurred Processing JSP page /Login.jsp at line 31

  • Ronnysilva, I believe the answer here may help your question.

2 answers

1


First you must direct in the form action the name of your Selectlet to send the input data, taking this data with the request.getParameter("") to validate and pass the error message or not to a request.setAttribute("");. and redirected to the desired page according to the RequestDispatcher rd=request.getRequestDispatcher(""); rd.forward(request, response);' E não oResponse.sendRedirect("");`

Look at the difference between one and the other

/questions/90410/qual-a-diferen%C3%A7a-entre-sendredirect-e-requestdispatcher-forward `

I’d be more or less like this

<form action="servlet_qualquer" method="post">
<input type="text" name="login">
<input type="password" name="password">
<input type="submit" name="submit">
 </form>

Your Rvlet any

 protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
   // processRequest(request, response);
   String certo="deu certo";
    String erro="deu errado";


    String login = request.getParameter("login");
      String senha = request.getParameter("password");

   if(login.equals("admin") && password.equals("admin")){

    request.setAttribute("certo", certo); 
   RequestDispatcher rd=request.getRequestDispatcher("/certo.jsp");
    rd.forward(request, response);


    }else{
   request.setAttribute("erro", erro); 
   RequestDispatcher rd=request.getRequestDispatcher("/pagina_erro.jsp");
    rd.forward(request, response);

}

}

Right page.jsp

    <c:if test="${!empty certo}">
                    <p  style="color:red;" align="center">${param.certo}</p><br/>
                </c:if> 

Wrong page.jsp

<c:if test="${!empty erro}">
                    <p  style="color:red;" align="center">${param.erro}</p><br/>
                </c:if> 

0

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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.