Showing posts with label j2ee. Show all posts
Showing posts with label j2ee. Show all posts

Tuesday, April 7, 2009

J2EE Lesson 3 - Hello World from J2EE!

So far, we were able to configure our environment and install the necessary applications that we need. Its time we dive on to how to actually do things. :)

1. Launch Eclipse by double clicking on 'eclipse.exe'
2. You'll be prompted with a dialog box, just click 'Ok'
3. You'll be presented with this window:
Just click on the 'Workbench' icon. Highlighted in red. Medyo baka iba ang hitsura sa machine nyo kasi I'm using Ubuntu (a linux distro) pero same lang yan.
4. Lalabas na yung main window talaga. Click on 'File' -> 'New' -> 'Other'. Kagaya nito:
5. Scroll down to 'Web'.
6. Expand the group and select 'Dynamic Web Project'.
7. Click 'Next'
8. Put in 'PinoyJavaTutorial' sa Project name' and click 'Finish'. Kagaya nito:
9. You'd be ask if you want to open the 'Java EE Perspective', just click 'Yes'
Congratulations! We now have a J2EE project at our Eclipse workspace! :)
10. Right click on the newly created project and select 'New' -> 'Servlet' as shown in the red box below:
11. You'll be presented with a 'Create Servlet' window. Put in 'HelloWorldServlet' as shown below:
12. Click 'Finish'
13. You'll be presented with the predefined 'HelloWorldServlet.java'
14. Unlike sa usual Java app, yung JVM (Java Virtual Machine) ay hinahanap yung 'main()' na method. Sa servlets, pupunta sya kagad sa 'doGet' or 'doPost' na methods since yung 'main()' method natin ay nasa tomcat na. Bale ganto para malinaw:

usual Java application: JVM -> main method
by servlets: JVM -> tomcat -> doGet or doPost method

Depende kung pano natin sya tinawag, kung 'doGet' or 'doPost' ang gagamitin. In our example, sa 'doGet' sya pupunta kasi ang 'doGet' ang default. Sa mga succeeding lessons na yung difference ng Post and Get. Easy-easy lang muna tayo.
15. Put in the following codes under 'doGet':

PrintWriter pw = response.getWriter();
pw.print("Hello World from Servlet!");

Magkakaroon ng underline na kulay pula yan since PrintWriter is unknown. We need to import pa kasi yung PrintWriter na class. However, since we are using Eclipse, just press 'ctrl + shift + O' para mag-automatically import na sya. Magiging ganto na ang hitsura ng 'doGet' method natin:
16. Now, we need to configure our server from Eclipse. Click on 'File' -> 'New' -> 'Other'
17. Expand 'Server' then select 'Server'. Kagaya nito:
18. Click 'Next'
19. Expand nyo yung 'Apache' then select 'Tomcat v6.0 Server':
20. Click 'Next'
21. Sa next window, browse nyo lang kung san nyo nilagay yung extracted na 'Tomcat'. Yung value nito ay same ng value na nilagay nyo sa 'CATALINA_HOME' nung nag ko-configure tayo before.
22. Click 'Finish'
23. Now, dun sa Project natin, right click, 'Run As' -> 'Run on Server'. Like this:
If may lumabas na window kagaya nito:
Just click 'Finish'
24. Mag e-error yan since wala pa tayong default na page. Pero try putting in http://localhost:8080/PinoyJavaTutorial/HelloWorldServlet and you'll see this page:
You can even open your browsers and go to http://localhost:8080/PinoyJavaTutorial/HelloWorldServlet and you'll be greeted by our Hello World Servlet message.

Congratulations! You have now created your first Servlet! I'll explain on my next blog entry, which should be coming the week after next week, on how everything works. :)

J2EE Lesson 2 - Testing Apache Tomcat Installation

Ok, so we were able to properly configure 'JAVA_HOME' and 'CATALINA_HOME'. It's time we test our configurations kung tama. :)

So, how do we exactly test? Easy lang:

1. Go to 'Run'
2. Go to the folder kung san mo inextract ang 'Tomcat'. Yung nilagay mo sa 'CATALINA_HOME' sa system variables sa 'Lesson 01' ng blog ko. Sa case ko, sa 'C:\APPS\apache-tomcat-6.0.18\'
3. Then 'cd bin'. Kagaya nito:
4. Type 'startup'
5. May lalabas na window na kagaya nito:
Pag may mga errors na lumabas, post it here sa comments so we can troubleshoot. Pag same lang, ng screenshot, you can proceed to the next step.
6. Open 'Firefox' or 'Internet Explorer'
7. Go to 'http://localhost:8080'
8. Dapat, magiging ganto ang hitsura ng page:
Kung hindi ganyan ang lumabas, just let me know through the comments sa baba.

Now, kung yan ang mga lumabas, it means ok lahat. Instead of launching the tomcat server sa command line, we can just simply double click 'startup.bat' sa 'bin' folder. Highlighted in red:

To terminate the server, just launch 'shutdown.bat' from the command line or by double clicking it. :)

Savvy? :)

J2EE Lesson 1 - Machine Setup

On this lesson, we'll work on setting up your computer to run and create java programs. Bale ito ang mga kelangan natin:

1. JDK - yung box na ang title ay "Java SE Development Kit (JDK)", click mo yung download na link. Kindly take note na "Java SE Development Kit (JDK)" at hindi "Java SE Runtime Environment (JRE)" ang title nung box na iki-click yung download na link. Bale ang JDK ay yung gagamitin nating compiler at 'runtime environment' (yung program na magra-run ng application).

2. Eclipse - ito yung IDE (Integrated Development Environment) na gagamitin natin. Ano ba ang IDE? An IDE or Integrated Development Environment is a program that provides an environment for the development of softwares and handles interactions with the compiler. Hence, sa IDE tayo magdedevelop at magla-lunch ng program at deployment sa Web Server. Actually, optional ang paggamit ng Eclipse sa Java programming. Kasi pwede naman gumamit ng notepad and command line. Kaso, on my personal view, sa totoong development environment, wala naman gumagamit ng notepad lang as the primary tool. Eclipse also makes develoment easier kasi hindi na kelangan mag issue ng mga java at javac command sa cmd. Its a good thing to know how to compile and run java programs using a command line but is not necessary to develop Java programs.

Pwede din hindi Eclipse ang gamitin. Pwedeng JCreator, NetBeans, BlueJ or even IBM's Websphere Studio Application Development (WSAD) and Rational Application Development (RAD) kung meron kang license at installer from IBM, atbp. Pero sa mga tutorial na to, I use Eclipse. Simply because Eclipse is easy to use at hindi masyadong mabigat sa system. Open source din sya at industry standard din like NetBeans. Even IBM's WSAD and RAD are based on Eclipse's platform.

3. Tomcat - 'To yung gagamitin nating Web Server. Since we'll be working on J2EE, dapat meron tayong Web Server. Ang i-download nyo ay yung 'Core' tapos yung zip file.


Installation:
1. Installation of JDK is pretty much straight forward. Just click on the 'next' or 'ok' button. In my case pati sa mga screenshots ko, hindi ko sya nilagay sa 'Program Files'. Gusto ko kasi magkakasama silang lahat at para madali kong ma-manage. Gumawa ako ng folder named 'APPS' tapos dun ko nilagay.

2. Installing Eclipse is even easier. Just extract the zip file and you're done.

3. Same thing sa Eclipse, just extract and you're done.


Configuration:
1. Go to 'Control Panel' -> 'System' -> 'Advanced' tab
2. Click 'Environment Variables'. May lalabas na window kagaya nito:
3. Click nyo lang yung 'New' button sa may 'System variables' na group. Naka red na box sa screenshot.
4. Sa 'Variable name:', lagay nyo lang: 'JAVA_HOME' at sa 'Variable value:' naman ay yung path kung san mo ininstall ang JDK. Kagaya nito:
5. Click 'Ok'
6. Click nyo ulit yung 'New' button. This time, 'CATALINA_HOME' naman ang ilalagay at yung 'Variable value:' ay kung saan saan nyo inextract yung tomcat. Sa case ko, I extracted it under 'APPS' folder, so ang 'New System Variable' ko ganto:


That's it pancit. Oks na tayo sa config. Paki restart na ang PC. :)