Tomcat是否自动编译java servlets? [英] Does Tomcat automatically compile java servlets?

查看:88
本文介绍了Tomcat是否自动编译java servlets?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Tomcat 7.0与Eclipse。 tomcat服务器与eclipse同步。创建我的第一个类后,我把.java文件放在src /(默认包)/HelloWorld.java(不是很好的做法,我知道但只是为了测试)



内容如下,相当简单:

  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);
}
}

很多教程声称我必须使用 javac 来编译代码。但我没有做任何事情,没有任何问题。每次我更改代码,它会立即更新,就像魔术一样。有些事情一定是有效的,但我不知道是什么。



是的,这显然是一个新手的问​​题,所以任何帮助都是值得欢迎的。还有更好的是,如果你有任何系统和易于遵循的教程链接。我正在寻找他们几天,但有很多不一致的答案。

解决方案

对我来说,您将两种主要技术 - tomcat作为Web容器和eclipse作为IDE进行组合。他们的整合让你感到困惑。
现在离开JSP,只讨论servlet,因为它更混淆了



Tomcat无法使用源文件(* .java)。您必须使用javac编译您的应用程序,并创建一个名为WAR - web archive的zip文件,该文件将包含您编译的类并遵守tomcat所理解的某些EE标准(也可以使用文件夹的zip,但可以把它放在一边,它与此解释不相关)。



其他这个战争(一旦编译正确)将包含你的编译器servlet类HelloWorld .class)。



一旦tomcat启动并识别出部署文件夹中的war文件夹,它会打开它并在运行时加载。
没有编译,只有运行时加载。



现在人们在这里谈论JSP。
事实上,JSP是技术上等同于servlet但类似于HTML的东西。
您将文件扩展名为.jsp并构建您的WAR。 java编译器不能读取jsp文件,所以你应该把它们放在你的war文件中(通常是为你创建工具/ IDE)。底线你有jsp文件,因为你在你的战争中创造了它们。
现在你把你的战争进入Tomcat,它像以前一样承认它,并加载。在这一点上,它仍然对你的JSP没有任何作用。



所以,你的战争是部署的,tomcat是启动的,在browserr中你可以访问http:// localhost:8080 /myfirstjsp.jsp'
此时(首次调用您的jsp)很多事情发生:


  1. Tomcat获取您的浏览器的http请求

  2. Tomcat识别它应该处理jsp文件

  3. Tomcat解析你的JSP文件

  4. Tomcat在内部编译一些您不知道的类文件(它存储在Tomcat内部),

  5. Tomcat在运行时加载该文件,并将其视为编译的Java类。 >

下次你会调用jsp,它将被编译。



这里的最后一个问题是Eclipse如何连接到所有这个故事:)
事实上,Eclipse与tomcat集成,所以所有的战争创建和部署的东西都是透明的。这就是为什么你在eclipse上播放并编译你的项目,创造一场战争,确保tomcat知道这个战争(配置部署相关的东西),启动tomcat并瞧,你有你的应用程序工作。



重要的是要了解在什么级别发生什么事情



希望这有助于
标记


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 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 'http://localhost:8080/myfirstjsp.jsp' At this point (first invocation of your jsp) a lot of thing happen:

  1. Tomcat gets your browser's http request
  2. Tomcat recognizes that it should process jsp file
  3. Tomcat parses your JSP file
  4. Tomcat compiles it internally to some class file that you're not aware of (its stored internally in Tomcat),
  5. 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.

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 to. Thats 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

这篇关于Tomcat是否自动编译java servlets?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆