Results 1 to 5 of 5
  1. #1
    KingdomX is offline Member
    Join Date
    Dec 2011
    Location
    Brazil
    Posts
    26
    Rep Power
    0

    Question PHP include on Java?

    I'm trying to include fragments from other pages much like i'd do this on PHP:

    PHP Code:
    // index.php?pid=1
    $pid = $_GET['pid'];
    
    switch($pid){
         case '1':
               include('register.php');
               break;
    }
    Now, i got on java as far as getting to the $pid var comparison, but i just can't seem to find a way to include the page dynamically:
    Java Code:
                    if (request.getParameter("pid") != null){
                        int id = Integer.parseInt(request.getParameter("pid"));
                        if( id == 1 ){
                           // INCLUDE FILE SYNTAX
                        }
                    }
    Anyway on doing this?

    Thank in advance!

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: PHP include on Java?

    There is no analogue of PHP's include in Java. (There is a Java "include", but it does something different.)

    What you will have to do is write the code within the if statement. Of course the code can call other code which resides in a different Java class (and hence a different file if you want to think of it that way). Something like:

    Java Code:
    if (request.getParameter("pid") != null){
        int id = Integer.parseInt(request.getParameter("pid"));
        if( id == 1 ){
           Registrar reg = new Registrar(id);
           reg.process();
        }
    }
    The code is rather whimsical, but I wanted to point out a couple of things. First the Registrar class is quite independent of the class where this code occurs and does not pick up any of its context as a result of being used. Anything it needs must be passed explicitly like id was. (Contrast this with PHP where the included code would inherit the variable scope.)

    Secondly what is called are the methods of some class. With PHP you have a choice, but in Java all functionality is packaged within classes.

    -----

    Only a saint could resist coding in language X with the idioms and patterns they are used to applying in language Y. But especially with respect to the patterns and overall construction of your program you would be well advised to learn Java by learning Java.

  3. #3
    KingdomX is offline Member
    Join Date
    Dec 2011
    Location
    Brazil
    Posts
    26
    Rep Power
    0

    Default Re: PHP include on Java?

    Hmmmmmm, so my question is: how do people reuse static pages like footer and menu while developing on Java? Can you only achieve this using frameworks?

    Thanks for clarifying that btw, i had already tried those imports from Java but i couldn't get it to work...

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: PHP include on Java?

    i couldn't get it to work..
    If you get errors, please copy and paste the full text here with your questions.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: PHP include on Java?

    how do people reuse static pages like footer and menu while developing on Java?
    Yes, a framework of some kind will be involved. JavaServerPages, part of JavaEE have an include tag that does what you would expect.

Similar Threads

  1. Include (and run) uncompiled Java
    By Ruben.cc in forum New To Java
    Replies: 6
    Last Post: 05-17-2011, 07:11 PM
  2. Replies: 0
    Last Post: 03-02-2010, 08:28 AM
  3. include-prelude/include-coda
    By devin in forum Java Servlet
    Replies: 0
    Last Post: 02-11-2009, 07:33 PM
  4. Replies: 5
    Last Post: 07-29-2008, 02:13 PM
  5. jsp:include and include file
    By Heather in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-14-2007, 04:19 PM

Posting Permissions

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