使用 Tomcat 获取请求并创建 HTTP 响应
我目前正在尝试为我的应用程序使用嵌入式 Tomcat,并尝试将其设置为获取 http 请求的 URL。
一些背景:
我使用与此处帖子的第一个答案相同的代码:如何嵌入 Tomcat 6?
我所做的唯一更改是:
private String catalinaHome = "/home/xyz/tomcat"; // This dir is created and has full access permissions
另外,我正在查看:http://tomcat.apache.org/tomcat-5.5-doc/catalina/docs/api/org/apache/catalina/ startup/Embedded.html
我找不到 server.xml 和 tomcat-users.xml,因此我创建了一个 tomcat-users.xml,因为我收到异常:内存数据库文件 /home/xyz/无法读取 tomcat/conf/tomcat-users.xml。
tomcat-users.xml:
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<role rolename="tomcat"/>
<role rolename="role1"/>
<user username="tomcat" password="tomcat" roles="tomcat"/>
<user username="both" password="tomcat" roles="tomcat,role1"/>
<user username="role1" password="tomcat" roles="role1"/>
</tomcat-users>
代码使用container.setRealm(new MemoryRealm()); 它从这里出现: http://tomcat .apache.org/tomcat-4.1-doc/catalina/funcspecs/fs-memory-realm.html 我应该有一个 server.xml 文件,并且默认情况下应该已经创建了一个。
1]我是否需要创建一个server.xml,其中的默认值应该是什么? 我从这里放置了一个默认文件: http://www. akadia.com/download/soug/tomcat/html/tomcat_server_xml.html,但只是想知道什么是正确的做法?
2]当我访问 http://localhost:8089/mywebapp/index.html 时,所有我得到的是请求的资源()不可用,尽管我在代码中的“webappDir”有一个index.html页面
3]我从嵌入式tomcat中唯一需要的是拦截以便获取传递给tomcat的URL我的代码。然后我可以解析 URL [做我的事情],然后创建一个 http 有效负载并发回一个 http 响应。
我将不胜感激任何指示,特别是 3] ?
谢谢!
I am currently trying to use embeded Tomcat for my application and am trying to set it up to get the URL of the http request.
Some Background:
I am using the same code as in the first answer for the post here : Howto embed Tomcat 6?
The only change I have made is :
private String catalinaHome = "/home/xyz/tomcat"; // This dir is created and has full access permissions
Also , I am looking at: http://tomcat.apache.org/tomcat-5.5-doc/catalina/docs/api/org/apache/catalina/startup/Embedded.html
There are no server.xml and tomcat-users.xml that I could find, so I created a tomcat-users.xml since I was getting an exception :Memory database file /home/xyz/tomcat/conf/tomcat-users.xml cannot be read .
tomcat-users.xml:
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<role rolename="tomcat"/>
<role rolename="role1"/>
<user username="tomcat" password="tomcat" roles="tomcat"/>
<user username="both" password="tomcat" roles="tomcat,role1"/>
<user username="role1" password="tomcat" roles="role1"/>
</tomcat-users>
The code uses container.setRealm(new MemoryRealm());
It appears from here : http://tomcat.apache.org/tomcat-4.1-doc/catalina/funcspecs/fs-memory-realm.html that I should have a server.xml file and there should already be one created by default.
1] Do I need to create a server.xml, what should be the default in it ?
I have put a file with default from here : http://www.akadia.com/download/soug/tomcat/html/tomcat_server_xml.html , but just want to know what is the right thing to do ?
2]When I access http://localhost:8089/mywebapp/index.html, all i get is The requested resource () is not available, though I have an index.html page at the "webappDir" in the code
3] My only need from the embedded tomcat is to intercept so as to get the URL passed to tomcat in my code. I can then parse the URL [do my stuff] and then create a http payload and send an http response back.
I would appreciate any pointers, especially for 3] ?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,对于你的第一个问题,你不需要 server.xml。如果您检查初始帖子的代码,他们会在那里设置参数。这就是 server.xml 所封装的内容。实际上,Tomcat 将解析 server.xml 以获取您在 java 文件中定义的属性,在该文件中实例化 catalina 调用以启动。但由于它是嵌入的,您可以在类上设置所有这些参数。
对于第二个问题,请检查日志目录并查看正在解析的内容。服务启动后会发生一些事情,因为一旦您调用端口,它应该已经重定向您。无论哪种方式,只需尝试 http://localhost:8089 并查看从 tomcat 返回的内容。它应该从服务器本身返回某种响应。
如果您这样做“http://localhost:8089/mywebapp/index.html”,您将尝试访问创建的上下文,并且可能未正确配置,但这只是现在的猜测。
首先尝试一下,然后告诉我你会得到什么。我们可以从这一点开始排除故障,看看我是否可以在这方面提供更多帮助。
Ok, for your first question, yo do not need server.xml. If you check the code of your initial post they are setting the parameters there. So that is what server.xml would encapsulate. In reality what happens is that Tomcat will parse server.xml for the properties you are defining on your java file where you instanciate the catalina call to start. But since it is embedded you are setting all those parameters on you class instead.
For your second question, check your logs directory and see what is being parsed. Something is happening after your service starts because it should already redirect you once you call the port. either way, just try http://localhost:8089 and see what you get back in return from tomcat. It should give you some kind of response back from the server itself.
if you do it like this "http://localhost:8089/mywebapp/index.html" you are trying to access a created context, and that might not be configured correctly, but that is just a guess right now.
Try this first and tell me what you get back. we can troubleshoot from this point and see if I can help more in that sense.
弱弱的问一句,你安装的是windows还是linux?
如果是 Linux,配置文件通常位于 /etc/tomcat6 上。 (至少在 ubuntu 上是这样)。回复您已安装的版本。我或许可以帮助你。
我想我还应该在这里详细说明一下。 Tomcat也是Linux中的一个服务,因此在ubuntu中您必须启动tomcat才能访问它。
然后它会在本地主机的端口 8080(通常如果不更改)上启动 tomcat。因此,您输入 localhost:8080 来访问配置 tomcat 的网站,该网站会为您提供 It Works 提示。
如果您有更多问题,请告诉我,我会尽力回答
Quick question, is this windows or linux you are installing on?
If it is linux the configurations filea are located usually on /etc/tomcat6. (at least on ubuntu they are). Reply back with the version you have installed. I might be able to help you out.
I guess I should also elaborate here a little more. Tomcat is a service in linux as well, so in ubuntu you have to start tomcat in order to access it.
then it starts tomcat on port 8080 (usually if not changed) of your localhost. hence you type localhost:8080 to access the website for configuration of tomcat that gives you a It works prompt for you.
Let me know if you have more questions, I will try to respond to the best of my knowledge