-3

I have a servlet class for login process, if the username or password is incorrect I want to set two attributes to the request, one attribute is boolean authentication_error, the second is string authentication_error_message.

I want to make a condition statement to print the authentication_error_message if the authentication_error is true.

req.setAttribute("authentication_error", false);

if(!query.list().isEmpty()){
    List<?> list = query.list();
    Admin student = (Admin)list.get(0);
    req.setAttribute("username", student.getUsername());
}
else{

    req.setAttribute("authentication_error_message", "username or password is incorrect!");
    req.setAttribute("authentication_error", true);
}

req.getRequestDispatcher("/views/login.jsp").forward(req, res);

How I can write this statement in jsp?

N.B: preferr to not use jstl.

Mohammad
  • 3,449
  • 6
  • 48
  • 75

2 Answers2

2

Scriptlets are not recommended, JSTL is the way to go for these scenarios. That being said, a different way could be scriptlets.

Scriptlet syntax example:

<%
 String userName="";

if(statement){
 userName=request.getParameter("userName");
  }
%>

See source here Java code inside JSP page.

How to avoid using scriptlets How to avoid using scriptlets in my JSP page?

Community
  • 1
  • 1
Perdomoff
  • 938
  • 2
  • 7
  • 26
0

You can use scriplet if you don't prefer to use JSTL.

Below code snippet is demonstration on how we can use condition statements in JSP.

<% if("true".equals      (request.getParameter ("authentication_error"))) {
%>
  authentication_error_message
<% }
else {
%>      
   Other Message
<%  } %>
Naveen Singh
  • 184
  • 1
  • 11