Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-29-2008, 08:08 AM
Member
 
Join Date: Nov 2007
Posts: 36
Shaolin is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-14-2008, 08:53 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 789
Nicholas Jordan is on a distinguished road
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
Code:
B b = new b();//
, which of course relies on a def for b somewhere. The minimal class would be:
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?
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-14-2008, 11:09 PM
Member
 
Join Date: Nov 2007
Posts: 36
Shaolin is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-15-2008, 03:02 AM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 789
Nicholas Jordan is on a distinguished road
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:
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:
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.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-15-2008, 05:10 AM
Member
 
Join Date: Nov 2007
Posts: 36
Shaolin is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-15-2008, 05:30 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
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:
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:
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 05:33 AM.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-15-2008, 06:07 AM
Member
 
Join Date: Nov 2007
Posts: 36
Shaolin is on a distinguished road
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:

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; } } }
HTML 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 06:12 AM.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-15-2008, 05:01 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 789
Nicholas Jordan is on a distinguished road
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();

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? } }
HTML Code:
<% ...... HelloWorldExample t1 = new HelloWorldExample(); ...... %>
No, I'm not awake yet but this needs study.

You are headed in the general direction.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 07-15-2008, 06:00 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
At this point, you should get a good text and read up.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 07-15-2008, 09:53 PM
Member
 
Join Date: Nov 2007
Posts: 36
Shaolin is on a distinguished road
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();

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? } }
HTML 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 ?
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 07-15-2008, 10:29 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 07-16-2008, 12:01 AM
Member
 
Join Date: Nov 2007
Posts: 36
Shaolin is on a distinguished road
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 ?
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 07-16-2008, 12:30 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 07-16-2008, 01:36 AM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 789
Nicholas Jordan is on a distinguished road
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."
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Take Java skills assessments- assess your skills and provide insight on new tests michelle Jobs Offered 4 06-24-2008 05:34 AM
Require Links for free swing component Gajesh Tripathi Advanced Java 2 08-12-2007 12:24 AM


All times are GMT +3. The time now is 12:35 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org