Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    46
    Rep Power
    12

    convert php to jsp


    I have a function that I would like to convert from php to jsp. The php function is below, can anyone help?

    function find_promo_content($today) {

    $promos=array();
    $temp=array();

    $filecontents=file('/home/httpsd/ra-new/home/fp_config.txt');

    # load up $temp with all our positions
    for ($i=0; $i< count($filecontents); $i++) {
    $buffer=trim($filecontents[$i]);
    # a '#' at the beginning of the line is a comment line--ignore it
    # and blank lines
    if ( (substr($buffer,0,1) != "#") && strlen($buffer) ) {
    list($position, $start_date, $content) = split("\t",$buffer);

    if (! is_array($temp[$position])) {
    $temp[$position]=array();
    }
    array_push($temp[$position], array(
    'date'=>$start_date,
    'content'=>$content
    )
    );
    }
    }

    # now go through all our positions and only send back the one we're
    # supposed to be showing on the front page, according to $today

    # for every position/category
    for (reset($temp); $position=key($temp); next($temp) ) {
    # and for every date found in that category...
    for ($j=0; $j< count($temp[$position]); $j++) {
    $thisdate=$temp[$position][$j]['date'];

    # if the date in the config file is valid
    if ($thisdate <= $today ) {
    # if we haven't added an entry into our response yet, or if
    # this date is later than the one we had there, write it
    if ( (!is_array($promos[$position]) ) ||
    ($thisdate > $promos[$position]['date']) ) {
    $promos[$position]=array(
    'date'=>$thisdate,
    'content'=>$temp[$position][$j]['content']
    );
    }
    }
    }
    }

    return $promos;
    }
  2. #2
  3. Prom night: 1973
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Dec 2001
    Posts
    1,156
    Rep Power
    24
    I've never used JSP, but here's the Java code for your routine. This is a pretty direct conversion from your PHP code, and so this isn't necessarily the way it'd be done in Java. It's also untested... it compiles, and that's about it. Ever tried doing this sort of thing in a database?
    Code:
    import java.io.*;
    import java.util.*;
    
    
                   public HashMap findPromoContent(long today) throws IOException {
                   // Assuming today and start_date were timestamps
    
                    BufferedReader filecontents = new BufferedReader(new FileReader("/home/httpsd/ra-new/home/fp_config.txt"));
    
                    String[] arr = null;
                    HashMap temp = new HashMap();
                    ArrayList tempList = null;
                    Long date = null;
                    String buffer = null;
    
                    System.out.println("Reading file");
                    while ((buffer = filecontents.readLine()) != null) {
                        // trim contents
                        buffer = buffer.trim();
    
                        // skip blank lines
                        if (buffer.length() == 0) continue;
                        // skip comments
                        if (buffer.matches("^#.*")) continue;
    
                        arr = buffer.split("\\t");
                        date = new Long(arr[1].toString());
    
                        if (!temp.containsKey(arr[0])) temp.put(arr[0], new ArrayList());
                        tempList = (ArrayList) temp.get(arr[0]);
    
                        // Normally, I'd create this array another way, but this looks more like PHP
                        Object[] tempArr = { date, arr[1] };
                        tempList.add(tempArr);
                    }
    
                    System.out.println("Getting promotions");
                    // now go through all our positions and only send back the one we're
                    // supposed to be showing on the front page, according to $today
    
                    ArrayList category = null;
                    Object key = null;
                    Long thisDate = null;
                    Long storedDate = null;
    
                    Object[] thisInfo = null;
                    Object[] storedInfo = null;
    
                    HashMap promos = new HashMap();
                    Object position = null;
    
                    for (Iterator i = temp.keySet().iterator(); i.hasNext(); ) {
                        position = i.next();
                        category = (ArrayList) temp.get(position);
                        final int arraySize = category.size();
    
                        for (int a = 0; a < arraySize; a++) {
                            thisInfo = (Object[]) category.get(a);
                            thisDate = (Long) thisInfo[0];
    
                            if (thisDate.longValue() > today) continue;
    
                            if (!promos.containsKey(position)) {
                                promos.put(position, thisInfo);
                                continue;
                            }
    
                            storedInfo = (Object[]) promos.get(position);
                            storedDate = (Long) storedInfo[0];
    
                            if (thisDate.longValue() < storedDate.longValue()) continue;
    
                            promos.put(position, thisInfo);
                        }
                    }
    
                    return promos;
            }
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    46
    Rep Power
    12
    Thanks a bunch. I am totally new to the Java world. I have written some test jsp pages so far, but that's about it. What so I need to do with your code? Do I need to compile it on my end? Is this what would be in a .class or .java file?

    Thanks for your help!!!
  6. #4
  7. Prom night: 1973
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Dec 2001
    Posts
    1,156
    Rep Power
    24
    The code will have to become part of a class definition. When I was compiling the code, I had an class definition file called FileTest.java (in the file FileTest.java) which looked like this:
    Code:
    import java.util.*;
    // other imports
    
    public class FileTest {
    
        public FileTest() {
           // The constructor... nothing in here yet
        }
    
    
        public HashMap findPromoContent(long today) {
           // the code I prodivded
        }
    
    
         public static void main(String[] args) {
                 try {
                         FileTest f = new FileTest();
                         f.findPromoContent(System.currentTimeMillis());
                 } catch (IOException e) {
                         e.printStackTrace();
                 }
         }
    
    }
    This'll let you compile FileTest.java into FileTest.class. The main method isn't necessary, but it lets you run the class from the command line.

    JSP will use the FileTest.class file. However, I've no idea how JSP accesses classes... either get a hold of a tutorial, or ask another question in the forum.

    Once you know how to get JSP to "see" FileTest.class you can create a new FileTest object and then process its output.

    Code:
       FileTest f = new FileTest();
       HashMap promos = f.findPromoContent(System.currentTimeMillis());
       for (Iterator i = promos.keySet().iterator(); i.hasNext; ) {
          // do stuff
       }
    If you have any questions about the code, I'll try to explain it. Good luck!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    46
    Rep Power
    12
    Ok, another question. the text file that this is reading is a tab delimited file, where each lin has a name, date and code. On my jsp page, once I read in the class, how would I go through the file and only show content that mathces the name. Here is an example of what the text file looks like:

    middle-middle 2001-11-01 <a href="/stores/instore_specials/"><img src=home/images/promo_2001_11_01.jpg width=145 height=90 border=0></a>

    middle-middle 2001-12-01 <a href="/stores/instore_specials/"><img src=home/images/promo_2001_12_01.jpg width=148 height=98 border=0></a>

    So, example, I only want to show the content that has a name(position) of middle-middle. In php the code looks like this:

    <?eval("?> " .$promos['top-top']['content'])?>

    Thanks for all your help.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2003
    Posts
    778
    Rep Power
    11
    mm, bit harder to do with JSP than PHP not sure why you want to switch to JSP in the first place. However the best language for parsing HTML (IMHO) would be perl. There is a perl module at CPAN specifically for this purpose.

    If you want to go ahead with JSP you will be happy to know that java 1.4 comes with regular expression support. or perhaps you might want work with the StringTokenizer class. Even more primitive would be the split() method in string.

    --
    http://www.radinks.com/upload/
    java file upload applet.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    46
    Rep Power
    12
    Our company is in the process of moving our site from PHP to J2EE for business purposes. I am just trying to rewrite our navigation system in jsp right now, since I am totally new to all this.
  14. #8
  15. Prom night: 1973
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Dec 2001
    Posts
    1,156
    Rep Power
    24
    Code:
    <?eval("?> " .$promos['top-top']['content'])?>
    should eval be echo? If not, then I haven't a clue as to what you're dong. If so, then you're looking to do something like
    Code:
    Object[] arr = promos.get("top-top");
    System.out.println(arr[1].toString());
    which can be further shortened to
    Code:
    System.out.println(((Object[]) promos.get('top-top'))[1]);
    if you're comfortable with casting.

    JSP probably has a different way of outputting text, though, so you'll probably have to replace the System.out.println part.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    46
    Rep Power
    12
    Ok, I got your code for sitecommon in a .jsp page and I am including it in another jsp page (layout_top.jsp). In layout_top.jsp, I put:

    FileTest f = new FileTest();
    HashMap promos = f.findPromoContent(System.currentTimeMillis());
    for (Iterator i = promos.keySet().iterator(); i.hasNext; ) {
    // do stuff
    }

    and get the errors:

    D:\Tomcat4.1\work\Standalone\localhost\_\hipaa_email\index_jsp.java:199: cannot resolve symbol
    symbol : class FileTest
    location: class org.apache.jsp.index_jsp
    FileTest f = new FileTest();
    ^



    An error occurred at line: 29 in the jsp file: /hipaa_email/inc/layout_top.jsp

    Generated servlet error:
    D:\Tomcat4.1\work\Standalone\localhost\_\hipaa_email\index_jsp.java:199: cannot resolve symbol
    symbol : class FileTest
    location: class org.apache.jsp.index_jsp
    FileTest f = new FileTest();
    ^



    An error occurred at line: 29 in the jsp file: /hipaa_email/inc/layout_top.jsp

    Generated servlet error:
    D:\Tomcat4.1\work\Standalone\localhost\_\hipaa_email\index_jsp.java:201: cannot resolve symbol
    symbol : variable hasNext
    location: interface java.util.Iterator
    for (Iterator i = promos.keySet().iterator(); i.hasNext; ) {
    ^
    3 errors
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    46
    Rep Power
    12
    Ok, I got rid of the first 2 errors by putting the following in my sitecommon.jsp file:

    public sitecommon() {
    // The constructor... nothing in here yet
    }

    But I still get the 3rd error:

    D:\Tomcat4.1\work\Standalone\localhost\_\hipaa_email\index_jsp.java:205: cannot resolve symbol
    symbol : variable hasNext
    location: interface java.util.Iterator
    for (Iterator i = promos.keySet().iterator(); i.hasNext; ) {
    ^
    1 error
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    46
    Rep Power
    12
    I updated my code to this:

    sitecommon f = new sitecommon();
    HashMap promos = f.findPromoContent(System.currentTimeMillis());
    //for (Iterator i = promos.keySet().iterator(); i.hasNext; ) {
    // do stuff
    //}

    I commented out the for loop to see if it is at least reading the text file with the date. I get the following:

    HTTP Status 500 -

    --------------------------------------------------------------------------------

    type Exception report

    message

    description The server encountered an internal error () that prevented it from fulfilling this request.

    exception

    org.apache.jasper.JasperException: For input string: "2003-10-03"
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:254)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
    at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.j ava:643)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.j ava:643)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
    at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2417)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
    at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.j ava:643)
    at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
    at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.j ava:641)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
    at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.j ava:641)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
    at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.j ava:643)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
    at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:193)
    at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:309)
    at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:387)
    at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:673)
    at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:615)
    at org.apache.jk.common.SocketConnection.runIt(ChannelSocket.java:786)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:666)
    at java.lang.Thread.run(Thread.java:534)


    root cause

    java.lang.NumberFormatException: For input string: "2003-10-03"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Long.parseLong(Long.java:403)
    at java.lang.Long.(Long.java:630)
    at org.apache.jsp.index_jsp$sitecommon.findPromoContent(index_jsp.java:48)
    at org.apache.jsp.index_jsp._jspService(index_jsp.java:206)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:137)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:210)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
    at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.j ava:643)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.j ava:643)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
    at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2417)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
    at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.j ava:643)
    at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
    at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.j ava:641)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
    at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.j ava:641)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
    at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.j ava:643)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
    at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:193)
    at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:309)
    at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:387)
    at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:673)
    at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:615)
    at org.apache.jk.common.SocketConnection.runIt(ChannelSocket.java:786)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:666)
    at java.lang.Thread.run(Thread.java:534)
  22. #12
  23. Prom night: 1973
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Dec 2001
    Posts
    1,156
    Rep Power
    24
    for (Iterator i = promos.keySet().iterator(); i.hasNext; ) {
    hasNext is a function, not a variable. You need to add a couple of brackets to let Java know this:
    Code:
    for (Iterator i = promos.keySet().iterator(); i.hasNext(); ) {
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    46
    Rep Power
    12
    gotcha, what goes in the "do stuff"?

    sitecommon f = new sitecommon();
    HashMap promos = f.findPromoContent(System.currentTimeMillis());
    for (Iterator i = promos.keySet().iterator(); i.hasNext(); ) {
    //do stuff
    }

    do I take the for statement and put it wherever I want the code from my text file to be placed?
  26. #14
  27. Prom night: 1973
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Dec 2001
    Posts
    1,156
    Rep Power
    24
    gotcha, what goes in the "do stuff"?
    Err... whatever you want it to do. For each item in the HashMap, do <something>... print it out?
    Code:
    sitecommon f = new sitecommon();
    HashMap promos = f.findPromoContent(System.currentTimeMillis());
    for (Iterator i = promos.keySet().iterator(); i.hasNext(); ) {
       //get the element
       Object value = promos.get(i.next());
       // convert the element to an array of Object
       Object[] arr = (Object[]) value;
       // print the content of this element
       System.out.println(arr[1].toString());
       
    }
    The i variable contains a list of keys for the HashMap, so every time you call i.next you're grabbing the next item in the list... yes, it's crap. The next version of Java has a bit more of a PHP-like loop for this kind of thing.
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    46
    Rep Power
    12
    Ok, let me try to explain this again, I'm getting confused. I got the code to work for my sitecommon.jsp file, which reads in the text file, etc. That file looks like this:
    -----------------------------------------------------------------------------------
    middle-middle 2001-11-01 <a href="/stores/instore_specials/"><img src=home/images/promo_2001_11_01.jpg width=145 height=90 border=0></a>

    middle-middle 2001-12-01 <a href="/stores/instore_specials/"><img src=home/images/promo_2001_12_01.jpg width=148 height=98 border=0></a>
    -----------------------------------------------------------------------------------

    In my layout_top.jsp page, I am calling this sitecommon.jsp file like this:

    sitecommon f = new sitecommon();
    HashMap promos = f.findPromoContent(System.currentTimeMillis());
    for (Iterator i = promos.keySet().iterator(); i.hasNext(); ) {
    //get the element
    Object value = promos.get(i.next());
    // convert the element to an array of Object
    Object[] arr = (Object[]) value;
    // print the content of this element
    //out.println(arr[1].toString());
    }

    Now, further down my layout_top.jsp page, I want to display the 3rd column contents(the html) of my text file where today's date is equal to or greater than the date in column 2 of my text file and where colum1 equals where I want it display.

    Right now, I am doing this:

    <% out.println(((Object[]) promos.get("left-nav-ds"))[1]);%>

    But what it is displaying in that position is the date. If I replace the [1] with [2], I get the error:

    org.apache.jasper.JasperException: 2

    This is almost working, but I need to display column 3 of the text file.

    Thanks!!!
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo