Wednesday, February 16, 2011

Configure Tomcat Server in Eclipse WTP

Refer to the Download, Install.... post to download and install tomcat.. The rest.. just follow below....


refer the 4 steps(pasted below) of the link : http://www.vogella.de/articles/EclipseWTP/article.html

If tomcat does not start correctly.. then refere to the last section of this post...

1. Eclipse Web Tool Platform

Eclipse WTP provides tools for developing standard Java web applications and Java EE applications. Typical web artifacts in a Java environment are HTML pages, XML files, webservices, servlets and JSPs. Eclipse WTP simplifies the creation these web artifacts and provides runtime environments in which these artifacts can be deployed, started and debugged. In Eclipse WTP you create "Dynamic Web Projects". These projects provide the necessary functionality to run, debug and deploy Java web applications. If you are completely new to Java web development you may want to read Introduction to Java Webdevelopment .

Eclipse WTP supports all mayor webcontainer, e.g. Jetty and Apache Tomcat as well as the mayor Java EE application server. This tutorial uses Apache Tomcat as a webcontainer.

2. Tomcat Installation

Download the Apache Tomcat server 6.0.x from the following webpage http://tomcat.apache.org/. On MS Windows you can use the self-explanatory MS installer to install Tomcat. For a Tomcat installation on other platforms, please use Google.

After the installation test if Tomcat in correctly installed by opening a browser to http://localhost:8080/. This should open the Tomcat main page and if you see this page then Tomcat is correctly installed.

After verifying that Tomcat is correctly installed, stop Tomcat. Eclipse WTP is try to start Tomcat itself. If Tomcat is already running Eclipse WTP will not be able to use it for development.

3. Installation of

I assume you have downloaded the Eclipse IDE for Java developers . Use the Eclipse Update Manager to install all packages from the category "Web, XML, and Java EE Development" except "PHP Development".



4. WTP Configuration

Select Windows -> Preferences -> Server -> Runtime Environments to configure WTP to use Tomcat Press Add.

Select your version of Tomcat.

Tip

To compile the JSP into servlets you need to use the JDK. In my case the "Workbench default JRE" is pointing to the JDK. You can check you setup by clicking on "Installed JRE".

Press Finish and then Ok. You are now ready to use Tomcat with WTP.

4.1. Server

During development you will create your server. You can manager you server via the server view. To see if you data was persisted stop and restart the server. You can do this via the Windows -> Show View -> Servers -> Servers

The following shows where you can start, stop and restart your server.





Now,

Troubleshooting in case the Tomcat doesn't work...... or is throwing error for some missing jars.. then follow the link : http://mianniu.com/programming-world/java-lang-noclassdeffounderror-orgapachejulilogginglogfactory-at-org-apache-catalina-startup-bootstrap

Exception in thread “main” java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory
at org.apache.catalina.startup.Bootstrap.(Bootstrap.java:54)
Caused by: java.lang.ClassNotFoundException: org.apache.juli.logging.LogFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)
… 1 more
Could not find the main class: org.apache.catalina.startup.Bootstrap. Program will exit.

Solutions

1. In Eclipse, Open the “Server” tab.
2. Double click on the “Tomcat6″ entry to see the configuration.
3. Then click on the “Open launch configuration” link in the “General information” block.
4. In the dialog, select the “Classpath” tab.
5. Click the “Add external jar” button.
6. Select the file “/usr/share/tomcat6/bin/tomcat-juli.jar”
7. Close the dialog.
8. Start tomcat 6 from Eclipse.


Monday, November 8, 2010

Step 2 : First Servlet and it's basic flow

Before the servlet architecture flow; let's analyse on the Tomcat architecture flow in the figure below:

The request is in the form of urls, specified from the client-side.


Tomcat Configuration in web.xml:When tomcat starts, it first reads the conf/web.xml file, which is the heart of the web application. This file is also known as web application descriptor file.

We need to modify the web.xml for our context. We can use the $CATALINA_HOME/conf/web.xml if we want all of our contexts to share the same configuration, but because every web project is different, use: $CATALINA_HOME/webapps/app1/WEB-INF/web.xml for app1 example.

1. Turn on the automatic reload, so if i change a JSP i don't have to restart the tomcat server every time but the server will detect this change automatically and compile properly. Turn it off when the site goes live, to improve efficiency. To make this change open $CATALINA_HOME/conf/web.xml and near the end, before it should say:

(DefaultContext reloadable = "true")

The request url and the related servlet or file mentioned in the client-side request needs to be specified in this descriptor file. for this purpose, 2 tags needs to be coded in the xml file as below.
note: "<" and ">" will be replaced with ( and )

(!-- mapping for the default servlet--)
(servlet-mapping)
(servlet-name)default(/servlet-name)
(url-pattern) / (/url-pattern)
(/servlet-mapping)

(-- mapping for the invoker servlet--)
(servlet-mapping)
(servlet-name)invoker(/servlet-name)
(url-pattern) /servlet/* (/url-pattern)
(/servlet-mapping)

The above configuration allows you to invoke any servlet that is in the "classes" folder of your web application. But let's say you want to invoke only certain servlets and restrict access to the others. Then you would need to avoid using the above method and declare only the servlet that will be accessible from the web.

Let's say we only want the servelt helloworld to be accessible, then in the web.xml file
in ( $CATALINA_HOME/webapps/app1/WEB-INF/web.xml ) do the following:

A) add to web.xml first this servlet declaration statements:

(-- mapping for the default servlet from app1--)
(servlet)
(servlet-name)helloworld(/servlet-name)
(servlet-class)helloworld(/servlet-class)
(load-on-startup)5 (/load-on-startup)
(/servlet)

B) then add a mapping for this servlet like this, in the same web.xml:

(-- mapping for the helloworldservlet--)
(servlet-mapping)
(servlet-name)helloworld(/servlet-name)
(url-pattern) /helloworld (/url-pattern)
(/servlet-mapping)

At this point you will be able to access your servlet helloworld as follows:
http://localhost:8080/app1/helloworld

code for heloworld.java servlet

import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;

public class helloworld extends HttpServlet{

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print("Hello World from GET method ");
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print("Hello World from POST method ");

}
}

How does the request flow go?
1) The user requests through URL, say, request URL contains, /servlet/my_servlet?param=value.
2) At the receipt of this request, the server(tomcat) will match this URL, with the list of (servlet-mapping) tags from the web.xml.
3) At a found match, the Mapping will be traced to the corresponding (servlet-name) and the respective class is triggered from within the web.xml file. The web. xml file is located at the conf folder of the server.

The basic Servlet lifecycle:(references: http://www.roseindia.net/servlets/introductiontojavaservlet.shtml )

> Java servlets are server side programs . They are applications that run on servers that comply HTTP protocol.
>The javax.servlet and javax.servlet.http packages provide the necessary interfaces/classes for servlets.
>Servlets: extend the HttpServlet class ; override the doGet /doPost methods. Other methods such as init, service and destroy also called as life cycle methods might be used .





Further reading :



PS: If you are plannign for developin Servlets in Eclipse, Then update the Eclipse plugins for J2EE development by installing and updating eclipse through 'Help' menu. Once done,


Once updated eclipse, You need to create a 'dynamic web project using eclipse project wizards.


Now create your first servlet in the (src) folder. However, this will throw errors in your source code due to the lack of javax.servlet libraries. Hence import this library form the 'plugins' folder of eclipse using the 'Buildpath' context menu of the project. This shoudl resolve the errors.

Wednesday, September 29, 2010

Step 1: Download,Install and start Tomcat Web server

Reference book used: JAVA Servlets, Developer's Guide - Karl Moss;OSBORNE


Download and Install : download and install the latest version of apache Tomcat Web server from http://tomcat.apache.org/download-60.cgi

Once downloaded , Extract thezip file to find the folder structure similar to the one in Fugure.1 below.


Figure.1


Every Tomcat (or webserevr) download will have 2 parts: the Servlet container named Catalina and the JSP engine named Jasper.The webapps folder in the main folder depicts the default web-application which would be executed when the server is started and requested via weburl.


Make sure that either the


1)JAVA_HOME environment variable is set to its Java's jdk root folder.


or


2) JRE_HOME environment variable is set to its Java's jre root folder





If you don't have JAVA installed go an download the JDK from the Sun Download page .(reference:http://www.cristhianny.com/others/tomcat5_setup_first_servlet.html)


Now, we need to set the Environment variable for the servlet container as 'CATALINA_HOME' for the root directory of the WebServer, which is c:\tomcat7.0.


Starting your Tomcat Server:


traverse to your tomcat bin folder in your command prompt and execute the command 'startup' .


(or)


just execute the command %CATALINA_HOME%\bin\startup in your command prompt.


Note: This command wouldn't work if your java environment variable isn't set correctly.


Figure.2

Testing of server setup: The default webapplication can be tested by browsing the url , http://localhost:8080/



Some common errors and troubleshooting techniques whie testing:


1) If the testing of the web app fails, It could be since the port 8080, which is the default http post is already in use. In this case, the port number could be changed to somethin greater than 1024. But when doing so, we need to remember the 2 changes; one if the url and the other in the server.xml file in the conf folder of the server.


2)Another error is when the Localhost isn't found. This can be rectified by altering the LAN setting and altering the server behind the proxy.(google...)



Common Note:Other coomon Application servers apart from Apache Tomcat are: BEA Weblogic and IBM Websphere


------------------------------------------------------------


TOMCAT CONTEXTS :
(reference:http://www.cristhianny.com/others/tomcat5_setup_first_servlet.html)

A context is a 'folder structure' where you will store your web application files (html, jsp, servlets, images, flash, etc...). You can have a different context for every web application that is running on your server. The contexts are stored in $CATALINA_HOME/webapps/

For instance, if i am working in 2 web applications called "app1" and "app2" then i can crate 2 folder with the same name inside $CATALINA_HOME/webapps/. Just be sure to follow the following conventions when creating a context:

Let's say i want to create a project called app1, then i would have the following folder structure at $CATALINA_HOME/webapps/:

$CATALINA_HOME/webapps/ app1/



  • WEB-INF/

  • src/

  • classes/

  • lib/

  • web.xml

- Basically, web.xml is the configuration file of our context or web application.
- IN src we will have our java source files /Servlets source code. But we will only have the .java files, not the .class files there.
- In the folder app1 we can put our jsp pages or html pages
- In lib we will have jars,like database jdbc drivers and other vendors' libraries.
- In classes we will have the .class which are our servlets.