CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2017

    Java JSP - using if()-else() inside scriptlets

    Good day, all.

    I have the following setup:

    - A JSP that outputs an HTML form to browser, then when one of the buttons is clicked after user enters an amount in the
    text field,
    - The data is sent to a servlet that performs certain calculations and sends result data back to the JSP.
    - The JSP then shows the result data and keeps running so user can make more iterations using the program.

    I am trying to code the JSP such that when it runs, it first checks for existence of 2 session attributes, "balance"
    and "formattedBal". If these attributes do not exist (meaning this is the very first time running the JSP after the
    user just hit its URL address), the JSP execution must create and assign these session attributes. If these attributes
    do exist (meaning this was not the initial launch of the JSP but a consequent one, with servlet having reverted the user
    to the JSP and sent result data, hence non-null session attributes to it), the JSP must simply execute the HTML output,
    showing the values of said session attributes in appropriate coded places.

    The following is the code of the JSP:

    	<hr>		<!--Horizontal line.-->
    	<title>Online Bank ATM Simulator</title>		<!--Title to show on browser title bar.-->
    	<h1 align = "center">Bank ATM Simulation</h1>	<!--Page heading, centered on page.-->
    	function checkAttributes()
    	// Set balance and formatted balance as session attributes.
    	if(request.getSession().getAttribute("balance") = null && request.getSession().getAttribute("formattedBal") = null)
    		request.getSession().setAttribute("balance", 0);
    		request.getSession().setAttribute("formattedBal", "$0");
    	<body onLoad = "checkAttributes()", "amount.focus()">				<!--Set focus to the text-field.-->
    		<form method = "POST" action = "../servlet/JSPBank">	<!--Form method and submission address.-->
    			<center>		<!--Tag to center the following output on page.-->
    			<input type = "text" name = "amount" id = "amount" size = "20"><br><br>	<!--Amount text field.-->
    			<%=(String)request.getSession().getAttribute("formattedBal")%> + "<br><br>	<!--Current formatted balance shown.-->
    			<button name = "balButton" value = "Balance">Balance</button>	<!--"Balance" button.-->
    			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;	<!--Spacers.-->
    			<button name = "depButton" value = "Deposit">Deposit</button>	<!--"Deposit" button.-->
    			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;	<!--Spacers.-->
    			<button name = "withdrButton" value = "Withdraw">Withdraw</button>	<!--"Withdraw" button.-->
    			</center>		<!--Tag to end centering of output on page.-->
    		</form>		<!--End of form.-->
    	<hr>		<!--Horizontal line.-->
    The problem is, I'm getting an error that points to line 11 - the <% tag. Whether I use a function or not, the error
    remains and reads exactly as follows:

    HTTP Status 500 -

    type Exception report
    description The server encountered an internal error () that prevented it from fulfilling this request.
    org.apache.jasper.JasperException: Unable to compile class for JSP

    An error occured at line: 11 in the jsp file: /bank.jsp
    Generated servlet error:
    Syntax error on token "=", != expected


    I have been researching this and related errors, but to not much avail.

    What makes no sense is the error states that I am using an = sign, but it expects an != combination, while as you see
    line pointed to there is never an = sign in the first place.

    I'm new to Java and it is possible this is a simple mistake somewhere, could you please help to identify it?

    I need to use scriptlets in the JSP whenever I need to utilize "standard" Java commands.

    Also, if the described and coded way to check for existence of these session attributes and their conditional
    assigning is not correct, please advise how I would better achieve this.

    Thank you very much!

  2. #2
    Join Date
    Aug 2017

    Re: Java JSP - using if()-else() inside scriptlets

    The if / else version is syntactically different from the ternary operator. It doesn't "return" anything.

    In order to make something like that work you'd need to do this

    if (state) {
    } else {
    If statements need something to do. They can't just have a string as their only statements. The ternary operator chooses and returns the selected value.

    Scriptlet blocks with the <%= %> syntax have to be a single expression that produces a value to output. Basically they have to evaluate to something. Even if the if statement were syntactically valid, it still wouldn't return a value.

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012

    Re: Java JSP - using if()-else() inside scriptlets

    if(request.getSession().getAttribute("balance") = null && request.getSession().getAttribute("formattedBal") = null)
    = is an assignment. For a condition test use ==.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.2)

  4. #4
    Join Date
    May 2009
    Lincs, UK

    Re: Java JSP - using if()-else() inside scriptlets

    The if does not have a string as its only statement. It has two statements, both setting session attributes (balance and formattedBal). The error complains about the use of = (assignment) instead of == (comparison).

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

By using this site, you agree to the Privacy Policy