I'm using Tomcat 7.0 with Eclipse. The tomcat server is synchronized with eclipse. After creating my first class, I put the .java file under src/(default package)/HelloWorld.java (not good practice I know but just for testing)
The content is just as follows, fairly simple:
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
#WebServlet("/HelloWorld")
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println ("Hello World");
}
}
Well many tutorials claim that I must use javac to compile the code. But I did nothing and it ran with no problem. Also everytime I change the code it updates immediately just like magic. Something must be working but I don't know what it is.
Yeah it's obviously a newbie question so any help is welcome. Also It's better if you have any systematical and easy-to-follow tutorial links. I'm searching for them for several days but got lots of inconsistent answers.
To me, You mix 2 main technologies - tomcat as a web container and eclipse as your IDE. Their integration confuses you.
Lets leave JSP for now and talk only about servlets, because it confuses even more
Tomcat cannot work with source files (*.java). You Must compile your application with javac for example and create something called WAR - web archive - a zip file that will contain your compiled class and adheres some EE standards that tomcat understands (its also possible to use folder instead of zip, but lets put it aside as well, its not relevant for this explanation).
Among others this war (once compiled correctly) will contain your compiled servlet class HelloWorld.class).
Once tomcat is started and recognizes your war file in deployment folder it opens it and loads in runtime.
No compilation, only runtime loading.
Now people talk here about JSP.
In fact JSP is something that is technically equivalent to servlet but resembles HTML.
You put the file with extension .jsp and build your WAR. java compiler can't read JSP files, so you should put them into your war file somehow (usually build tools/IDE do it for you). Bottom line is you have JSP files as you've created them in your war.
Now you put your war into Tomcat, it recognizes it as before and loads. At this point it still does nothing with your JSPs.
So, your war is deployed, tomcat is started and go to 'http://localhost:8080/myfirstjsp.jsp' from your browser
At this point (first invocation of your JSP) a lot of thing happen:
Tomcat gets your browser's HTTP request
Tomcat recognizes that it should process the JSP file
Tomcat parses your JSP file
Tomcat compiles it internally to some class file that you're not aware of (its stored internally in Tomcat),
Tomcat loads this file in runtime and treats it as a compiled Java class.
Next time you'll invoke the JSP, it will be already compiled.
The last question here is how Eclipse connected to all this story :)
In fact Eclipse has an integration with tomcat, so all the war-creating-and-deploying stuff is transparent to do. That's why you push 'play' on eclipse and it compiles your project, creates a war, makes sure tomcat is aware of this war (configures deployment related stuff), starts tomcat and voila! - you have you application working.
Its important to understand what happens at what level
Hope this helps
Mark
It looks like you are using the tomcat server plugin with Eclipse. In this case, as soon as you save your .java file, eclipse compiles it and updates the class files in tomcat server automatically.
Eclipse is an IDE, it does most of the things for you automatically like compiling the code, setting up classpath to include the required jar files etc.
If you want to follow the tutorials, I would suggest you use a plain text editor and a standalone tomcat server. Then, you will have to do all the steps mentioned in the tutorial(compiling the servlet class using javac, copying the .class file to tomcat server etc.)
No. Java EE container (Tomcat) cannot compile .java files automatically/implicitly. In fact the JSP engine of container will parse the JSP and generate the class file - JSP-wiki
Take a look at Eclipse Build Story.
Related
I have a project that runs inside the WebLogic server. 80 % of changes I do to the code affect JSP files only (not the class files). Those JSP files contain the frontend logic.
Whenever I change the JSP page, I need to do the following steps to see my changes:
mvn install inside the source directory.
Redeploy the application in WebLogic Admin Console ("Deployments", "Update").
Is it possible to avoid these two steps, if the only thing I change are JSP files (I don't touch Java files therefore, no class files are modified)?
Note that the application in question is set up in WebLogic so that it reads the directory with the class files (myProject/target), not a WAR file.
I have no definite information whether or not JRebel supports WebLogic. Its only alternative known to me, DCEVM, does not support WebLogic according to the last statement here.
Update 1 (02.05.2018 15:23 MSK): Changing
<wls:resource-reload-check-secs>-1</wls:resource-reload-check-secs>
to
<wls:resource-reload-check-secs>1</wls:resource-reload-check-secs>
in myProject/src/main/webapp/WEB-INF/weblogic.xml didn't help. When I deploy the application, then make a change to a JSP file, then run mvn install, the changes do not take effect.
There is page-check-seconds properties in weblogic.xml (beside others). Take a look at "weblogic.xml"
If you run in dev mode and not prod wls will redeploy each time a jsp changes at source
No, for several reasons. JSPs have to be compiled. They are not simple html pages. They are also cached by the server. You most definitely have to redeploy the project.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is it possible to compile .java files in tomcat server.I researched the net but couldn't find the required answer,if yes then please elaborate how!!
PS: I am new to development...so apologies if this ones a stupid question :)
NOT POSSIBLE
Tomcat expects .class compiled java files. but still you can compile the java file to generate .class file and replace the on under classes folder in the tomcat and redoploy it.
From Does Tomcat automatically compile java servlets?
Tomcat cannot work with source files (*.java). You Must compile your application with javac for example and create something called WAR - web archive - a zip file that will contain your compiled class and adheres some EE standards that tomcat understands (its also possible to use folder instead of zip, but lets put it aside as well, its not relevant for this explanation).
Among others this war (once compiled correctly) will contain your compiler servlet class HelloWorld.class).
Once tomcat is started and recognizes your war file in deployment folder it opens it and loads in runtime. No compilation, only runtime loading.
Now people talk here about JSP. In fact JSP is something that technically equivalent to servlet but resembles HTML. You put the file with extension .jsp and build your WAR. java compiler can't read jsp files, so you should put them into your war file somehow (usually build tools/IDE do it for you). Bottom line you have jsp files as you've created them in your war. Now you put your war into Tomcat, it recognizes it as before and loads. At this point it still does nothing with your JSPs.
So, your war is deployed, tomcat is started and in browserr you go to '/myfirstjsp.jsp' At this point (first invocation of your jsp) a lot of thing happen:
Tomcat gets your browser's http request
Tomcat recognizes that it should process jsp file
Tomcat parses your JSP file
Tomcat compiles it internally to some class file that you're not aware of (its stored internally in Tomcat),
Tomcat loads this file in runtime and treats as a compiled Java class.
Next time you'll invoke the jsp it will be already compiled.
No it is not possible. Tomcat is an applicationserver.
To compile .java files you have to use the tools from the jdk.
Compilation of java classes are performed via JDK and javac (java compiler) command.
To compiling your code You need to use command line or IDE, then you can deploy it in AS (such as tomcat)
*Only jsp files automatically compiled in AS
I have an extremely simple web app for experimenting in java. I have the following java class defined:
UtilDate.java http://www.philbair.org/samples/UtilDate_java.jpg
And a JSP file as follows:
UtilDate.java http://www.philbair.org/samples/getdate_jsp.jpg
The tomcat folders/files look like this (this is not official syntax, just an abstract representation of the folder structure):
UtilDate.java http://www.philbair.org/samples/tree.jpg
mywebapp/WEB-INF/classes/com/udate/UtilDate.class
The jsp file is under the root 'mywebapp' folder (name is inconsequential), and all these are under the webapps folder in tomcat.
After compiling this java class (UtilDate.java), I restart tomcat to make sure it's not hanging on to any old cache stuff.
I get the following...
javax.servlet.ServletException: java.lang.NoClassDefFoundError: UtilDate
when I try to put the jsp page in the browser.
If I replace the uDate() method in the print statement of the jsp file with
out.print("hello");
I get the word hello on the browser output.
This is crude code for testing a simple web app. I am not looking for advice on best practices (although that advice is excellent from what I've seen on this site), I just want to know why this doesn't work. It worked once, and after recompiling the class, it stopped working. I can run the class on my command line and it works just fine (outputs the date as in the main method).
I'm running Windows 7, and tomcat 7.0.34. I'm not using eclipse, or netbeans, etc. at the moment. I'm using a simple text editor, compiling the java class with the command line javac, creating the web folders manually, and placing all the files where they belong myself instead of having maven or ant or anything else build it for me. No war file at the moment. Just working with the files right in the tomcat folders under webapps. I know there are more sophisticated and recommended ways of doing this, but it's only a test for my own understanding with nothing to shield me from what's going on.
Again, this worked once.
Thanks in advance for any help.
you forgot to add the statement package com.udate; at the top of your java class. Add and compile , you should be through
I am using Tomcat 7.1 and Eclipse Indigo to develop a Java Web Application.
I just want to ask if anybody knows where the JSP translated files (the java files) are stored? Its often i receive exceptions indicating the jsp java file line, but if i can not see the file it is more difficult to correct the bug.
Doubleclick the Tomcat entry in Servers tab in Eclipse. You'll see something like this in Server Locations section:
If you haven't changed the server path and it thus defaults to Eclipse's workspace metadata, then the Tomcat's /work directory is not in Tomcat's own installation folder, but in the path as shown in the above screenshot.
Unrelated to the concrete problem, if you adhere the JSP coding recommendations and you avoid Java code in JSP altogether, then debugging will be so much easier, along with a lot of other advantages.
The JSP translated files are stored (in Tomcat) in /work/Catalina/localhost/[your_app_context]/org/apache/jsp/.
Should check in other containers or web servers.
you can find the jsp compailed java classes in your workspace:
And the path is: workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\auctuus\org\apache\jsp\jsp\gbp\settings
I have this Java code some other person wrote, specifically JSPs. I am trying to understand where everything is.
In my index.jsp (the main file which is loaded) it imports a certain namespace (I suppose tomcat does all the compiling, I don't know):
<%# page import="org.sgrp.SearchResults"%>
This physical location doesn't exist in my CLASSPATH so I suppose it's referring to a namespace inside the .jar code structure (correct me if I'm wrong).
So how am I suppose to find the source code for this? Does Tomcat set a specific CLASSPATH location for each project?
EDIT
I'm trying to understand if Tomcat follows a certain structure so I can find where the source code for this stuff is.
From the perspective of a web
application, class or resource loading
looks in the following repositories,
in this order:
* Bootstrap classes of your JVM
* System class loader classes
* /WEB-INF/classes of your web application
* /WEB-INF/lib/*.jar of your web application
* $CATALINA_HOME/lib
* $CATALINA_HOME/lib/*.jar
from the Tomcat docs.
The most likely locations for classes specific to your application are the WEB-INF/lib/*.jar and WEB-INF/classes. However as others have mentioned, this will contain compiled class files and you wont be able to see the exact java source (even after decompiling)
jars are compressed javabyte code. In order to get the sources you'll have to decompile them. There are a few utilities out there, but working with decompiled code is a bit of a nightmare if you ask me, I wouldn't recommend it, especially not if you didn't already know this. :D
The source code will generally be stored elsewhere.
As far as your specific questions on Tomcat, I don't know, having never working with Tomcat before.
You can look for the jars in two places:
/web-inf/lib directory (it has all the custom jars in use by the app)
The build file (mvn, ant etc)
Source code might not be there at all. So, first of all, you need to find which jar exactly has that imported class. Then just search the web to find the project/API website. There you can look for the source, if its an open source library. Otherwise, decomplilation is the only option left, and that might not be very helpful.
class path is, in my experience, commonly set in the ant build file of each project