Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

JSP scripting question

Options
  • 01-03-2004 4:08pm
    #1
    Registered Users Posts: 4,222 ✭✭✭


    On a JSP page i have i display the results of and sql query from a MySql db. The problem is that one of the table rows contains a string that may contain regex elements, that are use by a non application for string matching.

    the problem is that i dont want to display these regex elements on the web page.

    For example, the regex string stored in the db could be "test\s+string"
    The string i want to display on the web page is "test string"
    So in essence what i i'm trying to do is replace "\s+" with " ".

    I know how to do this fine in ASP using vbscript and jscript but i'm confused as to how to achieve the same effect in a JSP page.

    heres an example of the code i'm using at the moment to display the results (with the code in red i need to change):
    <sql:query var="regexString">
    SELECT id, matchstring FROM myDB
    </sql:query>
    
    <table border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
      <td>ID</td>
      <td>String</td>
    <tr>
    
    <c:forEach var="row" items="${regexString.rows}"
    <tr>
      <td width="129" align="center" >
        <c:out value="${row.ID}"/>
      </td>
      <td width="129" align="center" >
        [COLOR=red] <c:out value="${row.matchstring }"/>[/COLOR] 
      </td>
    </tr>
    </c:forEach>
    

    any ideas


Comments

  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    You can get matchstring into a Java String by doing the following:
    <c:set var="matchString" value="${row.matchstring}" scope="page"/>
    
    <%
    String matchStr = (String)pageContext.getAttribute("matchString");
    
    // manipulate matchStr the same way you do it in jscript
    
    pageContext.setAttribute("fixedMatchStr", matchStr);
    %>
    
    <c:out value="${fixedMatchStr}"/>
    

    Ideally you would write your own tag to do this so as to take all that ugly scriptlet code out of the page. Fortunately writing your own tags these days is really easy.

    Hope this helps


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    wild guesses.

    & n b s p ;

    \0x20


  • Registered Users Posts: 4,222 ✭✭✭Scruff


    Cheers Enygma! Thats exactly what i was looking for.
    You're a starbar!
    :D

    Actually the code line
    String matchStr = (String)pageContext.getAttribute("matchString");
    

    made me realise how little about accessing and manipluating jsp elements i know. Could you recommend a good source of info on it?

    Thanks again!


  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    Jeez I dunno, if you search for "JSTL Core Tags" on Google you'll find a good tutorial about the JSTL tags but it probably won't give you too much info on manipulating the values.
    You might find some useful tags on the Jakarta site http://jakarta.apache.org/taglibs

    I think I originally just guessed that that's how it worked because we did it the same way on a similar tag library a few years ago.
    Just read up on the JSP spec I guess and get to know the pageContext, application and session objects. They're the main ones really.

    Try writing a few of your own tags too to see how it's done.


Advertisement