Configure Servlet, JSP with Tomcat 7 - java

I am beginner in Java EE and when I install Tomcat 7, its works fine, I can access http://localhost:8080.
Now I understood that servlet and jsp jars files come under tomcat 7 and no need to download from oracle site.
So my question is when I compile my first servlet, it's say import javax.servlet.*; and import javax.servlet.http.*; not found.
How can I solved this?
Also one more thing In that servlet, I am also looking for visited user ip address. So I can know the location of that user.
I am using window platform and I configured JDK properly and its work fine.

hmmm, this is common question for beginner in Java EE, even I was too. Don't worry, here I am explaining you - How to solve this problem?. Here I am assume that you know How to install and configure tomcat 7, if not, visit http://www.coreservlets.com/Apache-Tomcat-Tutorial/
Basically tomcat 7 (even old one) come with servlets and jsp jars, you can find those jar in tomcat's lib folder
Tomcat 7.0\lib
servlet-api.jar
jsp-api.jar
Now you need to place these files in classpath (let me know if you don't know How to set files in classpath) By default tomcat not setting classpath for servlets.
After that if you compile your java file, it should works. let me know if you face any problem or difficulties.
if you want user location, you need to use request.getRemoteAddr() and it will give you the ip address, regarding location name (city, state, country and even more information), please visit http://www.maxmind.com/ services.
Here is the some links
http://www.maxmind.com/app/geolitecity
Example - http://geoip.cvs.sourceforge.net/viewvc/geoip/java/CityLookupTestV6.java?view=markup

If you are compiling using command line window, you have to specify class path for the servlet-api.jar in the lib directory of the tomcat installation directory or servlet-api.jar should be added to the windows class path. For using in command line,
javac -classpath <tomcat-installation-directory>/lib/servlet-api.jar myapp.java
If you are using IDE, need to add the server to the IDE and/or set the runtime environment to Tomcat.

Related

Error running Tomcat 7.0.34: Can't find catalina.jar

I have a Java web application that I have been working on 6 months ago and when I last worked on it, it would run perfectly. When I ran it again 6 months later I got this error "Can't find catalina.jar". I'm using IntelliJ 13.0 and tomcat 7.0.34.
Can anybody help me with this problem?
I had the same problem and solved it like this:
The method is to chose right Tomcat Home and Tomcat base directory in the wizard Run/Debug Configuration -> Server -> Configure...
Catalina.jar should already be present in tomcat/lib directory.
Is your tomcat home set up correctly, and is the jar there.
Also ensure that the user trying to access the lib directory and Catalina.jar etc.. has access to do so (Unix).
This problem may have occurred because Tomcat was loaded into the Program Files directory. Intellij IDEA cannot read files from Tomcat/lib inside program files. It can be decide: to run the IDEA with administrator rights or download Tomcat to another directory.
I have already fixed this problem,you need to correct the Tomcat Home's path.
My problem is that I renamed the file of apache-tomcat-8.5.9 to Tomcat,then the Idea could not distinguish the version.

What needs to be set up on Unix for a java web client?

I created a client using proxy in Oracle JDeveloper to call web service. JDeveloper deployed automatically and the code works well under windows environment. But now I'm required to migrate the code to unix server. I deployed the project to a .war file and copy it to unix which cannot be executed correctly using "jar" command.
Could anybody give me a whole picture or a high-level step-by-step instruction about what I need to set up to execute war on unix? I'm new to this area, and for now I got some suggestions like installing a Tomcat first.
What I have now:
1. a war file including .class, .java, WEB-INF, META-INF, MANIFEST.MF deployed from JDeveloper
2. JDK 1.6.0_25 installed on unix usr/java/
3. Tomcat installed on unix, but not under bin or local or usr directory(is it ok?)
Some specific questions:
1. what else do I need?
2. where to drop the .war?
3. do I need to unzip or re-compile the war?
4. how can I run the main class in the war?
Errors poped-up now:
1. cannot find main class
2. I tried to un-zip war and compile the class including main, and get "cannot find symbol" for the webservice specified classes' name.
Thanks to whoever attempt to help!
Even though this question probably should've been asked on ServerFault instead of here, sounds like you got all you need. Try dropping the .war on tomcat's webapps directory.
You should be able to just drop the .war file in $CATALINA_BASE/webapps and tomcat should pick it up when you start it.
$CATALINA_BASE is where you installed tomcat, unless you've configured tomcat for multiple bases.
You only need a war file if you are developing web application. From what you describe, you need to create a zip file which contains all the libraries and classes you need and copy it to Unix. Then you unpack it and execute it using java command.
Alternatively you can create a jar file with all dependencies included using Maven Shade Plugin and execute your code using jar command.

Can not put anything into /var/lib/tomcat7/webapps/

I was starting on JAVA web development today and encountered some problems, I installed my tomcat7 on my ubuntu machine. Now when I browse to //localhost:8080, I get the default welcome page saying:
This is the default Tomcat home page. It can be found on the local filesystem at: /var/lib/tomcat7/webapps/ROOT/index.html
Tomcat7 veterans might be pleased to learn that this system instance of Tomcat is installed with CATALINA_HOME in /usr/share/tomcat7 and CATALINA_BASE in /var/lib/tomcat7, following the rules from /usr/share/doc/tomcat7-common/RUNNING.txt.gz.
But strangely when I try echo $CATALINA_HOME nothing shows up.
Also I can not copy/create anything in the default /var/lib/tomcat7/ROOT though it's just a matter of providing few permissions but I was wondering whether it is the right way to do it?,
What I would like to do is create a separate directory in my home where I can put my web application and tomcat can read hem from there. Is there a way to do it? In apache I can do it by changing the document-root and directory but I don't know how to do it for tomcat
But strangely when I try echo $CATALINA_HOME nothing shows up.
This is because the packaged version sets CATALINA_HOME just prior to launching Tomcat, after reading it from a configuration file (typically somewhere in /etc).
Also I can not copy/create anything in the default
/var/lib/tomcat7/ROOT though it's just a matter of providing few
permissions but I was wondering whether it is the right way to do it?
The permissions problem has to do with you not being root (or the Tomcat user). Most packaged Tomcat installations (deb or RPM) tend to install with a specific user in mind, and copying stuff in as a different sometimes won't work.
/usr/share/tomcat7 is your CATALINA_HOME directory, and it has links to the other directories, such as /var/lib/tomcat7/webapps, /etc/tomcat7, etc. You shouldn't copy directly into a web application, you should package the web application into a WAR file and "deploy" it. The advantages are numerous.
What I would like to do is create a separate directory in my home
where I can put my web application and tomcat can read hem from there.
Is there a way to do it?
Yes, one is created when "deploying a web app". Look to the standard Tomcat7 documentation, and consider installing the "manager" web application if you like a non-command line interface. Now that you know what "installation" of a web app is called, it will likely be an easier task.
In apache I can do it by changing the document-root and directory but
I don't know how to do it for tomcat
Tomcat has a different, slightly more restrictive set of requirements for a document-root. You need to learn it, and just come to terms with the idea that it's never going to be the same. Basically under the "webapps" directory, is a WAR file or a expanded directory that came from a WAR file. Editing in-place is not a good idea for Tomcat, as the CGI-equivalents are not read from disk each time they are ran, they are linked into the memory of Tomcat. This means that a change in the directory might not affect your web application, which is good enough reason to not bother changing the on-disk files for a deployed web application.
Modify the web application, repackage it, and update the deployment. It's really the "right" way to go with Tomcat.
Give permission 777 to the webroot folder
sudo chmod -R 777 Webroot
After moving to the tomcat folder

How to propagate .java file change to .class under eclipse, tomcat & stripes

i'm new to java and i got some problems. i'm developping a web application using the framework stripes on tomcat 6.0.
I'm working with eclipse EE on windows. i successfully managed to get the stripes example application (called Bugzooky) up and running on my tomcat server. I imported the .war file and stripes libs in Eclipse.
Here is the stripes archive containing the examples and libs
But now i'm trying to change the example source files to learn how does it work. but whatever the modification made to the source files "WebContent/WEB-INF/src/*.java", nothing change! even after restarting the server. i noticed that the classes are compiled into .class files in the "ImportedClasses" folder, and tomcat always use these files, they are never updated, and if i removed one of them, the application just don't start. it look like my source files don't exists!
I also tried to build my webapp from scratch, but when i tried to use the features used in the example files (like SecurityFilter.java):
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import ...
It ends up with plenty of:
the import javax.servlet.Filter cannot be resolved
I checked the Librairies and it look like i'm using exactly the same as the example.
It's probably something i didn't understood about java but i googled 100 times yesterday, and i can't find the solution (i probably didn't search the right place because i don't really understand my problem).
I hope you'll be able to help me.
I'm not sure about the first problem. With regard to the problem when creating a new project:
the import javax.servlet.Filter cannot be resolved
This means that the web project in Eclipse is not associated with an application server. If this is done correctly, then Eclipse will automatically include the server's libraries (the server is actually a concrete implementation of under each the Servlet API) in the project's buildpath.
Did you integrate Tomcat in Eclipse? You can add servers in the Servers view at the right bottom box. Did you associate the web project with the particular Tomcat instance? You need to select the server in the Targeted Runtimes section of the project's Properties. This is by the way normally to be done during creating the Dynamic Web Project.
You need to recompile all the changed .java files, rebuild the .war file and redeploy it to Tomcat. This is most easily done with an Ant build script. Look for a file called build.xml. If it is there install Ant if you don't already have it installed and you can then just execute the ant command in the directory with the build.xml file and then copy the resulting .war file over the existing one in Tomcat. This is all basic Java App server work flow stuff.

Tomcat does not recognize the MySQL .jar library

I tried several different ways such that Tomcat loads the MySQL drivers when running my web application. I'm using Ubuntu 8.04, and the libraries come from the libmysql-java package. They are located in the directory shown below:
~$ ls /usr/share/java/mysql*
/usr/share/java/mysql-connector-java-5.1.5.jar
My CLASSPATH includes this file:
~$ echo $CLASSPATH
.:/usr/lib/jvm/java-6-sun/bin:/usr/local/tomcat/lib/servlet-api.jar:/usr/share/java/mysql-connector-java-5.1.5.jar
I even put a copy of the .jar file in my WEB-INF/lib/ directory in my web app:
/usr/local/tomcat/webapps/ohms/WEB-INF/lib$ ls
mysql-connector-java-5.1.5.jar
After making these changes, I restart Tomcat, re-compile my classes, restart Tomcat again. Also, I am importing the necessary libraries using import java.sql.*;
However I am still getting the java.lang.ClassNotFoundException: com.mysql.jdbc.Driver error when it runs the line Class.forName("com.mysql.jdbc.Driver").newInstance();
What am I doing wrong here?
Put it in TOMCAT_HOME/lib. (I seem to recall that on older versions of Tomcat, it's TOMCAT_HOME/server/lib?)
It's not going to work in WEB-INF/lib since the container needs access to the library, and, that is putting it in the classloader of one web app, not the container. While I would have imagined the CLASSPATH approach would work, it's not the standard way to do this. Perhaps there is some other snag that's preventing it from working.
Tomcat ignores any CLASSPATH environment variable, as do all Java EE app servers and IDEs. That does you no good at all. I don't set one on any machine that I use.
The JAR needs to go in Tomcat server/lib for Tomcat 5.x and /lib for 6.x.
You don't need to call newInstance(); Class.forName("com.mysql.jdbc.Driver") is sufficient to register the driver class.
Copy mysql connector to /usr/share/tomcat7/lib
One other thing you might want to do, if you don't want to put a file into the Tomcat lib directory. It states in catalina.sh that it will check setenv.sh for a variable set to include outside jars. So for example, as I did it, you may create this file:
$CATALINA_HOME/bin/setenv.sh
with the contents:
CLASSPATH=lib/mysql-connector-java-5.1.33-bin.jar
The path to the jar will be relative to where you start up Tomcat, also known as your working directory. So, for example, in my case I start up Catalina from an Ant script two levels up from $CATALINA_HOME and my lib directory is directly underneath that point.

Categories