[jsp] Using if-else in JSP

I'm using the following code to print the name of the user on the browser:

<body>
  <form>
    <h1>Hello! I'm duke! What's you name?</h1>
    <input type="text" name="user"><br><br>
    <input type="submit" value="submit">&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="reset">
  </form>
  <%String user=request.getParameter("user"); %>
  <%if(user == null || user.length() == 0){
    out.print("I see! You don't have a name.. well.. Hello no name");   
   }
   else {%>
      <%@ include file="response.jsp" %>
   <% } %>  
</body>

response.jsp:

<body>
    <h1>Hello</h1>
    <%= request.getParameter("user") %>
 body>

Every time I execute it, the message

I see! You don't have a name.. well.. Hello no name

gets displayed even though I haven't entered anything in the textbox. However if I enter anything in it, then the response.jsp code is displayed, but I don't want the first message to be displayed on execution. How do I make that happen? Please suggest changes in my code.

P.S. I had read in some questions that instead of checking for equality with null, one must check it for not equals, so that it doesn't throw null pointer exception. when I tried the same, i.e. if(user != null && ..), I got NullPointerException.

This question is related to jsp if-statement scriptlet

The answer is


You may try this example:

_x000D_
_x000D_
<form>_x000D_
  <h1>Hello! I'm duke! What's you name?</h1>_x000D_
  <input type="text" name="user">_x000D_
  <br>_x000D_
  <br>_x000D_
  <input type="submit" value="submit">&nbsp;&nbsp;&nbsp;&nbsp;_x000D_
  <input type="reset">_x000D_
</form>_x000D_
<h1>Hello ${param.user}</h1> _x000D_
<!-- its Expression Language -->
_x000D_
_x000D_
_x000D_


Instead of if-else condition use if in both conditions. it will work that way but not sure why.


It's almost always advisable to not use scriptlets in your JSP. They're considered bad form. Instead, try using JSTL (JSP Standard Tag Library) combined with EL (Expression Language) to run the conditional logic you're trying to do. As an added benefit, JSTL also includes other important features like looping.

Instead of:

<%String user=request.getParameter("user"); %>
<%if(user == null || user.length() == 0){
    out.print("I see! You don't have a name.. well.. Hello no name");   
}
else {%>
    <%@ include file="response.jsp" %>
<% } %>

Use:

<c:choose>
    <c:when test="${empty user}">
        I see!  You don't have a name.. well.. Hello no name
    </c:when>
    <c:otherwise>
        <%@ include file="response.jsp" %>
    </c:otherwise>
</c:choose>

Also, unless you plan on using response.jsp somewhere else in your code, it might be easier to just include the html in your otherwise statement:

<c:otherwise>
    <h1>Hello</h1>
    ${user}
</c:otherwise>

Also of note. To use the core tag, you must import it as follows:

 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

You want to make it so the user will receive a message when the user submits a username. The easiest way to do this is to not print a message at all when the "user" param is null. You can do some validation to give an error message when the user submits null. This is a more standard approach to your problem. To accomplish this:

In scriptlet:

<% String user = request.getParameter("user");
   if( user != null && user.length() > 0 ) {
       <%@ include file="response.jsp" %>
   }
%>

In jstl:

<c:if test="${not empty user}">
    <%@ include file="response.jsp" %>
</c:if>

Examples related to jsp

Difference between request.getSession() and request.getSession(true) A child container failed during start java.util.concurrent.ExecutionException The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path Using if-else in JSP Passing parameters from jsp to Spring Controller method how to fix Cannot call sendRedirect() after the response has been committed? How to include js and CSS in JSP with spring MVC How to create an alert message in jsp page after submit process is complete getting error HTTP Status 405 - HTTP method GET is not supported by this URL but not used `get` ever? How to pass the values from one jsp page to another jsp without submit button?

Examples related to if-statement

How to use *ngIf else? SQL Server IF EXISTS THEN 1 ELSE 2 What is a good practice to check if an environmental variable exists or not? Using OR operator in a jquery if statement R multiple conditions in if statement Syntax for an If statement using a boolean How to have multiple conditions for one if statement in python Ifelse statement in R with multiple conditions If strings starts with in PowerShell Multiple conditions in an IF statement in Excel VBA

Examples related to scriptlet

Using if-else in JSP How do I pass JavaScript values to Scriptlet in JSP? I can pass a variable from a JSP scriptlet to JSTL but not from JSTL to a JSP scriptlet without an error How can I avoid Java code in JSP files, using JSP 2?