?
Solved

condition in JSP

Posted on 2011-04-30
19
Medium Priority
?
502 Views
Last Modified: 2012-05-11
Hi folks,

I have a form in jsp that hold the information of a book the user would like to reserve in the library upon clicking the submit button. The information would then go the database table reservations with the username and Booktitle. MY problem is to set a condtion to check first before approving the reservation , if the book is reserved by another user. There is no need to search the library because the reserved row has only "F" or "V" and i

The form looks like this:
 <%@ page contentType = "text/html" %>
 <%@ taglib prefix = "c" uri="http://java.sun.com/jsp/jstl/core" %>
 <%@ page import="java.util.*" %>
 
 <html>
<body>
<h4>Are you sure you would like to reserve the following book(s)? If yes, please click submit below to reserve the book<h4>


<form action="reserve.jsp" method="get">
<table>
<tr>
<td>Book Title:</td>
<td><input type="text" name="BookTitle" value="${param.BookTitle}"></td>
</tr>

<tr>
<td>Author:</td>
<td><input type="text" name="Author" value="${param.Author}"></td>
</tr>

<tr>
<td>ISBN:</td>
<td><input type="text" name="Isbn" value="${param.Isbn}"></td>
</tr>

<tr>
<td>Edition:</td>
<td><input type="text" name="Edition" value="${param.Edition}"></td>
</tr>

[b]<tr>
<td>Reserved:</td>
<td><input type="text" name="reserved" value="${param.Reserved}"></td>
</tr>[/b]

<tr>
<td><input type="submit"  name="Reserve" /></td>
</tr>
</table>
</body>
</html>

Open in new window


As you can see from the above code, i have the value of the reserved row from query in a previous page....

This is the way i tried to go with the matter:

<c:choose>
<c:when test="${request.getParameter("Reserved").equals("Y")}">
out.println("sorry, you cannot reserve the book...reserved by another user");
</c:when>
<c:otherwise>
<jsp:forward page="reserve_now.jsp" />
</c:otherwise>

the big problem remain , how to stop the user from proceding to the next page when clicking the submit button? Instead , if reserved="Y", they should only get the message...

thanks
0
Comment
Question by:ozzyfanta
  • 8
  • 5
  • 3
  • +2
19 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35499375
You should make a javascript popup
alert("Error message")
  window
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35499380

I guess like in this example:
http://www.roseindia.net/jsp/jsp-alert.shtml
0
 
LVL 92

Assisted Solution

by:objects
objects earned 400 total points
ID: 35499387
try this:

<c:choose>
<c:when test="${param.Reserved == "Y"}">
sorry, you cannot reserve the book...reserved by another user
</c:when>
<c:otherwise>
<jsp:forward page="reserve_now.jsp" />
</c:otherwise>
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 35499390
> the big problem remain , how to stop the user from proceding to the next page when clicking the submit
> button? Instead , if reserved="Y", they should only get the message...

a better option may be to not even allow them to submit if reserved


<c:choose>
<c:when test="${param.Reserved == "Y"}">
sorry, you cannot reserve the book...reserved by another user
</c:when>
<c:otherwise>
<input type="submit"  name="Reserve" />
</c:otherwise>
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35499400

I guess, something like that should work:
 <%@ page contentType = "text/html" %>
 <%@ taglib prefix = "c" uri="http://java.sun.com/jsp/jstl/core" %>
 <%@ page import="java.util.*" %>
 <html>
 <head>
 <script>
 function DoCustomValidation()
 {
   var frm = document.forms["myform"];
   if(frm.reserve.value = 'Y')
   {
     alert('Book is reserved');
     //return false;
   }
   else
   {
     location='reserve_now.jsp';
   }
}
</script>
</head>

 
<body>
<h4>Are you sure you would like to reserve the following book(s)? If yes, please click submit below to reserve the book<h4>


<form action="reserve.jsp" id="myform" method="get">
<table>
<tr>
<td>Book Title:</td>
<td><input type="text" name="BookTitle" value="${param.BookTitle}"></td>
</tr>

<tr>
<td>Author:</td>
<td><input type="text" name="Author" value="${param.Author}"></td>
</tr>

<tr>
<td>ISBN:</td>
<td><input type="text" name="Isbn" value="${param.Isbn}"></td>
</tr>

<tr>
<td>Edition:</td>
<td><input type="text" name="Edition" value="${param.Edition}"></td>
</tr>

[b]<tr>
<td>Reserved:</td>
<td><input type="text" id="reserve" name="reserved" value="${param.Reserved}"></td>
</tr>[/b]

<tr>
<td><input type="submit"  name="Reserve" onClick=validate()/></td>
</tr>
</table>
</body>
</html>

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35499402
I guess, something like that should work (correction)::

 <%@ page contentType = "text/html" %>
 <%@ taglib prefix = "c" uri="http://java.sun.com/jsp/jstl/core" %>
 <%@ page import="java.util.*" %>
 <html>
 <head>
 <script>
 function validate()
 {
   var frm = document.forms["myform"];
   if(frm.reserve.value = 'Y')
   {
     alert('Book is reserved');
     //return false;
   }
   else
   {
     location="reserve_now.jsp";
   }
}
</script>
</head>

 
<body>
<h4>Are you sure you would like to reserve the following book(s)? If yes, please click submit below to reserve the book<h4>


<form action="reserve.jsp" id="myform" method="get">
<table>
<tr>
<td>Book Title:</td>
<td><input type="text" name="BookTitle" value="${param.BookTitle}"></td>
</tr>

<tr>
<td>Author:</td>
<td><input type="text" name="Author" value="${param.Author}"></td>
</tr>

<tr>
<td>ISBN:</td>
<td><input type="text" name="Isbn" value="${param.Isbn}"></td>
</tr>

<tr>
<td>Edition:</td>
<td><input type="text" name="Edition" value="${param.Edition}"></td>
</tr>

[b]<tr>
<td>Reserved:</td>
<td><input type="text" id="reserve" name="reserved" value="${param.Reserved}"></td>
</tr>[/b]

<tr>
<td><input type="submit"  name="Reserve" onClick=validate()/></td>
</tr>
</table>
</body>
</html>

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 35499424
you could also remove the problem entirely by removing the 'reserve' link if the book is already reserved
that way they can only get to this form for an unreserved book
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35499425
One more correction:

 <%@ page contentType = "text/html" %>
 <%@ taglib prefix = "c" uri="http://java.sun.com/jsp/jstl/core" %>
 <%@ page import="java.util.*" %>
 <html>
 <head>
 <script>
 function validate()
 {
   var frm = document.forms["myform"];
   if(frm.reserve.value = 'Y')
   {
     alert('Book is reserved');
     //return false;
   }
   else
   {
     location="reserve_now.jsp?BookTitle=" + frm.title.value + "&Author=" + frm.author.value + "&Isbn=" + frm.isbn.value + 
"&Edition=" + frm.edition.value;
   }
}
</script>
</head>

 
<body>
<h4>Are you sure you would like to reserve the following book(s)? If yes, please click submit below to reserve the book<h4>


<form action="reserve.jsp" id="myform" method="get">
<table>
<tr>
<td>Book Title:</td>
<td><input type="text" name="BookTitle"  id="title" value="${param.BookTitle}"></td>
</tr>

<tr>
<td>Author:</td>
<td><input type="text" name="Author" id="author" value="${param.Author}"></td>
</tr>

<tr>
<td>ISBN:</td>
<td><input type="text" name="Isbn" id="isbn" value="${param.Isbn}"></td>
</tr>

<tr>
<td>Edition:</td>
<td><input type="text" name="Edition" id="edition" value="${param.Edition}"></td>
</tr>

[b]<tr>
<td>Reserved:</td>
<td><input type="text" id="reserve" name="reserved" value="${param.Reserved}"></td>
</tr>[/b]

<tr>
<td><input type="submit"  name="Reserve" onClick=validate()/></td>
</tr>
</table>
</body>
</html>

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 35499427
remember that javascript validation is easily bypassed, and won't work if javascript is disabled.
best to always use server side validation
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35499446
javacript validation is absolutely standard technique and those who disable
javascript will not see the siginificant parts of 80% of the web pages or maybe more.
Just try to disable it  and you'll see how it all looks.
Just click on ViewSource on this page on EE - it just all full of javascript
0
 
LVL 92

Expert Comment

by:objects
ID: 35499450
javascript is not a replacement for server side validation.
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 35499484

<c:when test="${param.Reserved eq 'Y'}">
0
 

Author Comment

by:ozzyfanta
ID: 35500178
objects:
I am getting  the following error:


exception

org.apache.jasper.JasperException: /find.jsp(30,34) equal symbol expected
      org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
      org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
      org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:88)
      org.apache.jasper.compiler.Parser.parseAttribute(Parser.java:193)
      org.apache.jasper.compiler.Parser.parseAttributes(Parser.java:148)
      org.apache.jasper.compiler.Parser.parseCustomTag(Parser.java:1208)
      org.apache.jasper.compiler.Parser.parseElements(Parser.java:1422)
      org.apache.jasper.compiler.Parser.parseBody(Parser.java:1634)
      org.apache.jasper.compiler.Parser.parseOptionalBody(Parser.java:978)
      org.apache.jasper.compiler.Parser.parseCustomTag(Parser.java:1246)
      org.apache.jasper.compiler.Parser.parseElements(Parser.java:1422)
      org.apache.jasper.compiler.Parser.parse(Parser.java:130)
      org.apache.jasper.compiler.ParserController.doParse(ParserController.java:255)
      org.apache.jasper.compiler.ParserController.parse(ParserController.java:103)
      org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:185)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:347)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:327)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:314)
      org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:592)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:326)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.32 logs.
0
 

Author Comment

by:ozzyfanta
ID: 35500190
Idem for dravidnsr
0
 
LVL 47

Accepted Solution

by:
for_yan earned 1200 total points
ID: 35500191

Instead of this:
<c:when test="${param.Reserved == "Y"}">
it should be
<c:when test="${param.Reserved == 'Y'}">
(single quote within the comparison)


0
 
LVL 47

Expert Comment

by:for_yan
ID: 35500196
try this:
<c:when test="${param.Reserved=='Y'}">
0
 
LVL 21

Assisted Solution

by:Amitkumar P
Amitkumar P earned 400 total points
ID: 35500209
Agreed with dravidnsr,

Following will work
1. <c:when test="${param.Reserved eq 'Y'}"> (by dravidnsr)
2. <c:when test="${param.Reserved == 'Y'}">

Also refer the following:
1. http://www.ibm.com/developerworks/java/library/j-jstl0211/index.html
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 35501099
is that eq is not working ???????????
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 35501102
is eq is not working ==  only working ?????
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month13 days, 16 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question