Results 1 to 14 of 14
  1. #1
    Shaolin is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default Require some insight!

    Hey Guys,

    I am a PHP programmer and want to gain some experience in JSP. I have a an ok idea of the methodology of how JSP/Servlets works but I need to put my knowledge into practise. The best way I learn is through examples. I have afew questions and hope you guys don't mind helping me out! :)

    My first question is how would I create a link to a DB?
    My second is what is the basic structure of a class, and how would I include that class in a JSP and create an instant of it? And if I had a method within that class called "setName" how would I call it?

    I am using tomcat server.

    Thanks

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default precepts

    Quote Originally Posted by Shaolin View Post
    ...The best way I learn is through examples. I have afew questions and hope you guys don't mind helping me.
    There are plenty of samples code for every Java practice on the Sun webserver. In general these are the best place to start.
    Quote Originally Posted by Shaolin View Post
    My first question is how would I create a link to a DB?
    Java has a well established codebase in networking and database matters. In general, start with a google for java database connectivity. I looked into some code on a threading matter and found a built in switch that reconstructed the string to accomodate five or six very popular database protocols. In the simple, we have a five or six step code sequence that relies on strings. Often but not always these strings are shippable as get requests in a parameter string. IOW - compliance with rfc's for query strings would be one thing to note as part of preliminary studies.
    Quote Originally Posted by Shaolin View Post
    My second is what is the basic structure of a class, and how would I include that class in a JSP and create an instant of it? And if I had a method within that class called "setName" how would I call it?
    In Java, everything and I mean everything goes in a class. To create an instance of a class, we just do
    Java Code:
    B b = new b();//
    , which of course relies on a def for b somewhere. The minimal class would be:
    Java Code:
    class B{}
    which seems trivial, but just stuff everything inside the curly braces and do get-going work.
    Quote Originally Posted by Shaolin View Post
    I am using tomcat server.
    Free standing or in an IDE?
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    Shaolin is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    Thanks for th reply. The server is free standing. I think what I find confusing is how do Java programmers (when coding websites) structure their code. I know you use servlets, but are they just another name for a class ?

    in PHP, when I want to create a class I simply do the following:

    MyClass.php:
    PHP Code:
    <?php
    public class MyClass {
    	private $name;
    
    	MyClass() {
    		$name="";
    	}
    
    	setname($setName){
    		$this->$name = $setName;
    	}
    }
    ?>
    When I want to use that class in the PHP file I do the following:

    addname.php:
    PHP Code:
    <?php
    	require('MyClass.php');
    	
    	$a = new MyClass();
    	
    	$a->setname("john");
    ?>
    Which adds John to the $a instance of MyClass. How would I do the same thing using JSP ? I am talking strictly of coding in object oriented form.

  4. #4
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Post style

    Quote Originally Posted by Shaolin View Post
    Thanks for the reply. The server is free standing. I think what I find confusing is how do Java programmers (when coding websites) structure their code. I know you use servlets, but are they just another name for a class ?
    Not to be difficult, everything in Java is a class. If it is not a class, it is some other something or other. The premise is like asking fish about water or grumps about stinky feet.

    What is, is what is. Just like the Hollywood version of a computer in 2001 cannot, even under the promise of a two century mega-buster, concieve of anything other than evil. Evil, like beef, is what's for dinner under their world vision for everyone.

    To them, that's what is and that's the way it is. ( at least in Holly Lost-Hope )

    In Java, a class to Java like water is to Fish.

    In Java, when I want to create a class I simply do the following:
    Java Code:
    // Simple Sample, subject to review and comment by masters.
    // Dollar sign may be valid var label, I forgot.
    public class MyClass 
    {
        private int someInt;//
        private  String name = "";//
        // Constructors may be public private or protected.
        // A constructor without an access spec is defaut access.
        // IOW file scope class
         public MyClass(int i){
               someInt = i;      }
           // assigns incoming string to string variable
           puiblic setname(String Name){
                 this.name = Name;
           }
    }
    When I want to use that class in the Java file I do the following:
    Java Code:
    MyClass myClass = new MyClass();// Round braces are empty parameter list
    Quote Originally Posted by Shaolin View Post
    ...inisight....
    Can you remove the tabs from your source editor?

    Try this: Javamex: Solve it in Java

    This writer has an excellent grasp on what Java is and what Java does.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  5. #5
    Shaolin is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    Thanks for the reply. So would the code above would work as a servlet ? I have heard that "normal" java classes don't work when creating websites, instead you have to use servlets. I know how normal Java classes work, thats no problem but I just don't know how to structure java code for web applications.

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

    Default

    Servlets are written pretty much like normal java programs. They run in a servlet container that provides them with a view on the world via passed objects. The classes extend HTTPServlet and must override various methods that are called to handle GETs and POSTs.
    JSP looks a lot like the PHP code you posted above. It's a mix of java, its own language and HTML.
    Here's the hello world servlet that came with Tomcat:
    Java Code:
    import java.io.*;
    import java.text.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    /**
     * The simplest possible servlet.
     *
     * @author James Duncan Davidson
     */
    
    public class HelloWorldExample extends HttpServlet {
    
    
        public void doGet(HttpServletRequest request,
                          HttpServletResponse response)
            throws IOException, ServletException
        {
            ResourceBundle rb =
                ResourceBundle.getBundle("LocalStrings",request.getLocale());
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
    
            out.println("<html>");
            out.println("<head>");
    
    	    String title = rb.getString("helloworld.title");
    
    	    out.println("<title>" + title + "</title>");
            out.println("</head>");
            out.println("<body bgcolor=\"white\">");
            out.println("<body>");
    
    	// note that all links are created to be relative. this
    	// ensures that we can move the web application that this
    	// servlet belongs to to a different place in the url
    	// tree and not have any harmful side effects.
    
            // XXX
            // making these absolute till we work out the
            // addition of a PathInfo issue
    
    	    out.println("<a href=\"/examples/servlets/helloworld.html\">");
            out.println("<img src=\"/examples/images/code.gif\" height=24 " +
                        "width=24 align=right border=0 alt=\"view code\"></a>");
            out.println("<a href=\"/examples/servlets/index.html\">");
            out.println("<img src=\"/examples/images/return.gif\" height=24 " +
                        "width=24 align=right border=0 alt=\"return\"></a>");
            out.println("<h1>" + title + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }
    }
    Here's a simple test jsp program:
    Java Code:
    <%! int i=0; %>
    <html>
    <body>
    <% while (i<10)
    {
      i++;
    %>
    <%= i %>
    times running.
    <% } %>
    
    end test
    </body>
    </html>
    Last edited by Norm; 07-15-2008 at 03:33 AM.

  7. #7
    Shaolin is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    Cool - I presume the doGet will handle get requests. How do I create custom functions/classes and then call them from the html ? Would it be something like this:

    Java Code:
    import java.io.*;
    import java.text.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    public class HelloWorldExample extends HttpServlet {
    
    public void doGet(HttpServletRequest request,
    HttpServletResponse response)
    throws IOException, ServletException
    {
       private String name = "Jack";
    
       public String returnName() {
          return this.name;
       }
    }
    }
    XML Code:
    <html>
    <head></head>
    <body>
    <% 
    HelloWorldExample t1 = new HelloWorldExample(); 
    out.println(t1.returnName)
    %>
    
    println.out
    </body>
    </html>
    Last edited by Shaolin; 07-15-2008 at 04:12 AM.

  8. #8
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Exclamation remarkable progress

    Quote Originally Posted by Shaolin View Post
    Cool - I presume the doGet will handle get requests. How do I create custom functions/classes and then call them from the html ? Would it be something like this:
    You are really close already. What we do here for base concepting is that there are five or six of somthing or other method call signatures that the running server will call into at runtime. The method signatures are avaliable in several standard intro discussions. IOW we call into the code at someplace other than main();

    Java Code:
    // subject to immediate revision before anything else,,,.....
    public class HelloWorldExample extends HttpServlet {
       private String name = "Jack";
       public void doGet(HttpServletRequest request,
       HttpServletResponse response)
       throws IOException, ServletException
       {
         PrintWriter pw = response.getWriter();
         pw.println(name);// simple, huh?
       }
    }
    XML Code:
    <% 
    ......
    HelloWorldExample t1 = new HelloWorldExample(); 
    ......
    %>
    No, I'm not awake yet but this needs study.

    You are headed in the general direction.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

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

    Default

    At this point, you should get a good text and read up.

  10. #10
    Shaolin is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    Thanks for the reply guys.

    Quote Originally Posted by Nicholas Jordan View Post
    You are really close already. What we do here for base concepting is that there are five or six of somthing or other method call signatures that the running server will call into at runtime. The method signatures are avaliable in several standard intro discussions. IOW we call into the code at someplace other than main();

    Java Code:
    // subject to immediate revision before anything else,,,.....
    public class HelloWorldExample extends HttpServlet {
       private String name = "Jack";
       public void doGet(HttpServletRequest request,
       HttpServletResponse response)
       throws IOException, ServletException
       {
         PrintWriter pw = response.getWriter();
         pw.println(name);// simple, huh?
       }
    }
    XML Code:
    <% 
    ......
    HelloWorldExample t1 = new HelloWorldExample(); 
    ......
    %>
    No, I'm not awake yet but this needs study.

    You are headed in the general direction.
    So is the servlet limited to these six or so method call signatures ? And do you have a list of them ?

    I guess this is alittle different to how I am used to coding in php. In php I create custom functions/classes to handle different tasks (one to handle DB requests, another to handle data output etc). JSPs/Servlets are alittle different. I have a question (provided I have understood this correctly) wouldn't the lack of flexibility make it difficult when working on large projects ?

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

    Default

    Not sure what you mean by: lack of flexibility.
    Servlets run in a servlet container and are restricted in what they can do.
    Are you implying that with PHP, you can write and execute ANY code: GUI, Sockets, etc and have it run on a server?

    Servlets can instantiate and use other classes. Don't know about JSP.

  12. #12
    Shaolin is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Not sure what you mean by: lack of flexibility.
    Servlets run in a servlet container and are restricted in what they can do.
    Are you implying that with PHP, you can write and execute ANY code: GUI, Sockets, etc and have it run on a server?

    Servlets can instantiate and use other classes. Don't know about JSP.
    Yes, thats what I mean. Cool, how would I include other classes ? use the import statement or something ?

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

    Default

    Your getting into too much detail before you know how to program in java.
    As I said earlier, you code servlets the same as any other java class, remembering that it runs in a container and all that implies.

  14. #14
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Talking typing, the monster raises from the deep

    Quote Originally Posted by Shaolin View Post
    So is the servlet limited to these six or so method call signatures ? And do you have a list of them ?
    We can extend most classes. Those that are not declared final may be overriden. This is an abbreviated, short-cycle answer. What we get into here is the typing issue. A master-worker at Berkley laid down Java as an implementation platform for A.I. -- disentangling all the typing made for a recusive descent ,.... read that a little to literally and the driftwood will come to the beach.

    I have never written in an untyped language, and do not plan to. The approach to do untyped work in Java is an Object. That is sort of a Generic catch-all ~ an object is sort of anything inside the curly braces. Sounds like you are coming from an untyped language, I think Norm's advice to do moderated reading is the seasoned voice of reason. Try Marty Hall. Core Servlets, they have a website up ~ a google for that will get you there.

    Let that be your guide for the intro.

    Quote Originally Posted by Shaolin View Post
    I guess this is alittle different to how I am used to coding in php. In php I create custom functions/classes to handle different tasks (one to handle DB requests, another to handle data output etc). JSPs/Servlets are alittle different. I have a question (provided I have understood this correctly) wouldn't the lack of flexibility make it difficult when working on large projects ?
    Define large projects. Usually what that means is coders from cousin-land who are there for summer internship and cannot wait to get back in their Red Ferrari and do things: "As Seen on T.V."
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. Replies: 8
    Last Post: 08-11-2009, 03:57 PM
  2. Require Links for free swing component
    By Gajesh Tripathi in forum Advanced Java
    Replies: 2
    Last Post: 08-11-2007, 10:24 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
  •