Results 1 to 14 of 14
Thread: Require some insight!
- 06-29-2008, 06:08 AM #1
Member
- Join Date
- Nov 2007
- Posts
- 38
- Rep Power
- 0
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
- 07-14-2008, 06:53 PM #2
precepts
There are plenty of samples code for every Java practice on the Sun webserver. In general these are the best place to start.
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.
In Java, everything and I mean everything goes in a class. To create an instance of a class, we just do, which of course relies on a def for b somewhere. The minimal class would be:Java Code:B b = new b();//
which seems trivial, but just stuff everything inside the curly braces and do get-going work.Java Code:class B{}
Free standing or in an IDE?Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 07-14-2008, 09:09 PM #3
Member
- Join Date
- Nov 2007
- Posts
- 38
- Rep Power
- 0
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:
When I want to use that class in the PHP file I do the following:PHP Code:<?php public class MyClass { private $name; MyClass() { $name=""; } setname($setName){ $this->$name = $setName; } } ?>
addname.php:
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.PHP Code:<?php require('MyClass.php'); $a = new MyClass(); $a->setname("john"); ?>
- 07-15-2008, 01:02 AM #4
style
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:
When I want to use that class in the Java file I 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; } }
Can you remove the tabs from your source editor?Java Code:MyClass myClass = new MyClass();// Round braces are empty parameter list
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
- 07-15-2008, 03:10 AM #5
Member
- Join Date
- Nov 2007
- Posts
- 38
- Rep Power
- 0
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.
- 07-15-2008, 03:30 AM #6
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:
Here's a simple test jsp program: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>"); } }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.
- 07-15-2008, 04:07 AM #7
Member
- Join Date
- Nov 2007
- Posts
- 38
- Rep Power
- 0
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.
- 07-15-2008, 03:01 PM #8
remarkable progress
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? } }No, I'm not awake yet but this needs study.XML Code:<% ...... HelloWorldExample t1 = new HelloWorldExample(); ...... %>
You are headed in the general direction.Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 07-15-2008, 04:00 PM #9
At this point, you should get a good text and read up.
- 07-15-2008, 07:53 PM #10
Member
- Join Date
- Nov 2007
- Posts
- 38
- Rep Power
- 0
Thanks for the reply guys.
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 ?
- 07-15-2008, 08:29 PM #11
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.
- 07-15-2008, 10:01 PM #12
Member
- Join Date
- Nov 2007
- Posts
- 38
- Rep Power
- 0
- 07-15-2008, 10:30 PM #13
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.
- 07-15-2008, 11:36 PM #14
typing, the monster raises from the deep
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.
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
-
Take Java skills assessments- assess your skills and provide insight on new tests
By michelle in forum Jobs OfferedReplies: 8Last Post: 08-11-2009, 03:57 PM -
Require Links for free swing component
By Gajesh Tripathi in forum Advanced JavaReplies: 2Last Post: 08-11-2007, 10:24 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks