Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Passing a variable from a jsp page to Java code and back to jsp page.

  1. #1
    Junior Member
    Join Date
    May 2023
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Passing a variable from a jsp page to Java code and back to jsp page.

    I am new to Java programming but I have some experience in programming.

    <display:column titleKey="testOrder.voucher" width="110">
    ${testOrder.voucherNumber}
    </display:column>


    ${testOrder.voucherNumber} prints a string made of a combination of letters and numbers (ABC1234).

    I want a way to split the string ABC1234 and only print the number 1234 in the input box.
    Also, I want the user to only be able to write numeric values in the input box.

    Thanks for the help.

  2. #2
    Member Helium c2's Avatar
    Join Date
    Nov 2023
    Location
    Kekaha, Kaua'i
    Posts
    111
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Passing a variable from a jsp page to Java code and back to jsp page.

    Just declare it. String x = " ";
    String y = " ";
    String z = " ";

    Then write it out. So the results being z. You would re-type the method or formula in to split ABC and 1234 to give the results as...this would then be in jsp. And they're using probably html to mark up a page. I know C++ is similar to Java. But the same approach. This would be the conversion part. Write it in the program in Java.

  3. #3
    Member
    Join Date
    Jan 2024
    Posts
    75
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Passing a variable from a jsp page to Java code and back to jsp page.

    To pass a variable from a JSP page to Java code and back to the JSP page, you can follow these steps:

    1. In your JSP page, you already have `${testOrder.voucherNumber}` which prints a string like "ABC1234".
    2. To split the string and extract only the numeric part (e.g., "1234"), you can use Java code within the JSP page.
    3. You can create a JavaScript function to ensure that the user can only input numeric values into the input box.

    Here's a sample solution:

    ```jsp
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@ taglib uri="http://displaytag.sf.net" prefix="display" %>

    <!DOCTYPE html>
    <html>
    <head>
    <title>Split Voucher Number</title>
    <script>
    function allowNumbersOnly(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
    evt.preventDefault();
    }
    }
    </script>
    </head>
    <body>
    <display:column titleKey="testOrder.voucher" width="110">
    <c:set var="voucherNumber" value="${testOrder.voucherNumber}" />
    <c:set var="numericPart" value="${voucherNumber.replaceAll('[^\\d]', '')}" />
    <input type="text" id="numericInput" value="${numericPart}" onkeypress="allowNumbersOnly(event)" />
    </display:column>
    </body>
    </html>
    ```

    Explanation:
    - The JSP page uses the JSTL library to perform operations.
    - `${voucherNumber.replaceAll('[^\\d]', '')}` replaces all non-numeric characters in `${testOrder.voucherNumber}` with an empty string, effectively extracting only the numeric part.
    - The JavaScript function `allowNumbersOnly(evt)` ensures that only numeric values can be entered into the input box.

    This solution should fulfill your requirements of splitting the string and allowing only numeric values in the input box. If you need further assistance with your JSP assignment, feel free to explore additional resources available online like ProgrammingHomeworkHelp.com.

Similar Threads

  1. opening a new page and running a script on it, from the old page.
    By Hudsense in forum Other Programming Languages
    Replies: 1
    Last Post: September 25th, 2014, 04:44 AM
  2. opening a new page and running a script on it, from the old page.
    By Hudsense in forum What's Wrong With My Code?
    Replies: 0
    Last Post: September 25th, 2014, 03:51 AM
  3. Replies: 6
    Last Post: August 4th, 2014, 05:04 AM
  4. help taking code from java class put it in JSP page.
    By anavagomez in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 26th, 2012, 10:57 AM