-1

So I have a form in one JSP and one to pass the data to another JSP to check whether is it empty or not. Can I do this?

First.jsp

<form id="search-form" name="search">
                    <input type="text" name="txtSearch" id="search-field">
                    <button type="submit" onclick="<c:set var="search" value="${param.txtSearch}" scope="request"/>"><a href="Second.jsp">SEARCH</a></button>
                </form>

Second.Jsp

<c:set var="search" value="${requestScope['search']}"/>
<c:choose>
        <c:when test="${empty search}">
            <h1>Empty</h1>
        </c:when>
        <c:otherwise>
            <h1>${search}</h1>
</c:otherwise>
    </c:choose>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user3676506
  • 102
  • 10

1 Answers1

2

You're checking the request parameter in the wrong place. You should check it in the request associated with the target resource. The <c:set> inside onclick doesn't make any sense. JSP produces HTML code. The onclick executes only JS code. Rightclick page, do View Source and look at the place of the onclick attribute. Do you see? It's empty! Further, a HTML link wrapped in a HTML button is also nonsense. You're basically producing invalid HTML. In forms, you're supposed to specify the target resource in <form action>.

Here's a basic kickoff example:

<form action="Second.jsp">
    <input type="text" name="txtSearch" />
    <input type="submit" value="Search" />
</form>
<c:choose>
    <c:when test="${empty param.txtSearch}">
        ...
    </c:when>
    <c:otherwise>
        ...
    </c:otherwise>
</c:choose>

Regardless, I strongly recommend to step back and review if you're really using the right learning resources to learn HTTP, HTML and JSP. There were quite some serious fundamental mistakes in your first attempt. Start here: Java EE web development, where do I start and what skills do I need?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks, appreciate your help. I will review my knowledge on HTTP, HTML and JSP. – user3676506 Nov 08 '15 at 15:19
  • I have another question. In the First.jsp, if I only have an , is there a way to obtain the same result as above? – user3676506 Nov 08 '15 at 15:58
  • This section is for comments, not for new questions. There's a "Ask Question" button on right top for that. Regardless, just learn some basic HTML first. – BalusC Nov 08 '15 at 15:59