Html 使用 tomcat 7 运行 JSP 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13877797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 04:23:17  来源:igfitidea点击:

Running JSP files with tomcat 7

htmljsptomcat

提问by Bernice

Hi I just installed Apache Tomcat 7 and got the server running OK. When I entered localhost:8089 (8089 being the port I assigned to Tomcat 7) the apache website popped up and I got to run some jsp examples and they opened correctly.

嗨,我刚刚安装了 Apache Tomcat 7 并且服务器运行正常。当我输入 localhost:8089(8089 是我分配给 Tomcat 7 的端口)时,apache 网站弹出,我必须运行一些 jsp 示例并且它们正确打开。

However now I wish to create a jsp file myself (I am trying tutorials for the very first time) and when I open them, only the source code I written is showing in the browser.

但是现在我想自己创建一个jsp文件(我是第一次尝试教程),当我打开它们时,浏览器中只显示我编写的源代码。

The example I am trying is this:

我正在尝试的例子是这样的:

<html>
<head>
</head>

<body>
Hello World. <%= new java.util.Date() %>
</body>

</html>

and all I'm seeing in Chrome is : "Hello World. <%= new java.util.Date() %>"

我在 Chrome 中看到的是: "Hello World. <%= new java.util.Date() %>"

I am saving this file in this path:

我将此文件保存在此路径中:

C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\host-manager\WEB-INF\jsp

In WEB-INF there is an index.html file ready.

在 WEB-INF 中有一个 index.html 文件准备好了。

Am I doing something wrong? Where am I supposed to save my file? I tried watching tutorials on youtube but most of them were in arabic and I couldn't understand a thing!

难道我做错了什么?我应该在哪里保存我的文件?我尝试在 youtube 上观看教程,但大多数都是阿拉伯语,我什么都听不懂!

Thanks in advance for any help!

在此先感谢您的帮助!

回答by JB Nizet

Your JSP file must NOT be in WEB-INF.

您的 JSP 文件不能在 WEB-INF 中。

You must create a directory named as you want (myFirstWebAppfor example), put your JSP in any subdirectory you want except WEB-INF (for example myFirstWebApp/foo/bar/hello.jsp), then deploy the webapp by copying the myFirstWebApp firectory to the webappsdirectory of Tomcat. The JSP will then be accessible using the URL

您必须创建一个您想要命名的目录(myFirstWebApp例如),将您的 JSP 放在除 WEB-INF 之外的任何您想要的子目录中(例如myFirstWebApp/foo/bar/hello.jsp),然后通过将 myFirstWebApp firectory 复制到webappsTomcat 目录来部署 webapp 。然后可以使用 URL 访问 JSP

http://localhost:8089/myFirstWebApp/foo/bar/hello.jsp

WEB-INF is where you put the files that you don't want to make accessible from the outside:

WEB-INF 是您放置不想从外部访问的文件的地方:

  • the jars (under WEB-INF/lib),
  • the classes (under WEB-INF/classes),
  • the deployment descriptor (web.xml, optional in servlet 3.0 webapps configured through annotations),
  • and any other file you want anywhere under WEB-INF (typically, configuration files).
  • 罐子(下WEB-INF/lib),
  • 课程(下WEB-INF/classes),
  • 部署描述符(web.xml,在通过注解配置的 servlet 3.0 webapps 中可选),
  • 以及您想要的任何其他 WEB-INF 下的文件(通常是配置文件)。

Why don't you read the documentation? http://tomcat.apache.org/tomcat-7.0-doc/appdev/deployment.html

你为什么不阅读文档?http://tomcat.apache.org/tomcat-7.0-doc/appdev/deployment.html

回答by Grim

Rename your jsp-file to:

将您的 jsp 文件重命名为:

C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\ROOT\index.jsp

now, navigate to

现在,导航到

http://localhost:8089/

Short and Easy.

简短易行。

回答by Victor

Put your jsp file in ROOT folder as

将您的jsp文件放在ROOT文件夹中

/Users/<username>/Desktop/tomcat/webapps/ROOT/test.jsp

Run your application like

运行你的应用程序

http://localhost:8080/test.jsp

http://localhost:8080/test.jsp

回答by Suresh Gupta

Save your jsp file in your webapps directory with .jsp extention

将您的 jsp 文件保存在您的 webapps 目录中,扩展名为 .jsp

C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps and types at the address bar of the browser- http:\localhost:8089/exmp.jsp and press Enter key

C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps 并在浏览器的地址栏中键入- http:\localhost:8089/exmp.jsp 并按 Enter 键