[英]Configuring a Java HTTP servlet in Netbeans 7.3
I've recently upgraded to Netbeans 7.3, and I quite don't get much of its project configuration and interface. 我最近升级到了Netbeans 7.3,但是我对它的项目配置和界面知之甚少。 For example, once I used to see the source packages with java classes, now I'm not able to see them only in the File panel. 例如,曾经使用Java类查看源程序包,现在仅在“文件”面板中看不到它们。 I have a package called batchUtility containing an HTTP servlet called batchQueryServlet. 我有一个名为batchUtility的程序包,其中包含一个名为batchQueryServlet的HTTP Servlet。
I have to post a JSON to batchQueryServlet via the jQuery.ajax() function and I'm not able to map the servlet. 我必须通过jQuery.ajax()函数将JSON发布到batchQueryServlet,但是我无法映射servlet。
Since Netbeans 7.3 doesn't ship a config file, I've created one by myself clicking on the web-inf folder>new>web.xml file and wrote as follows: 由于Netbeans 7.3不提供配置文件,因此我自己创建了一个配置文件,方法是单击web-inf文件夹> new> web.xml文件,并编写如下:
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>batchQueryServlet</servlet-name>
<servlet-class>/batchUtility.batchQueryServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>batchQueryServlet</servlet-name>
<url-pattern>/batchQueryServlet</url-pattern>
</servlet-mapping>
</web-app>
but still, when issuing the request, I get a 404 error: 但仍然,在发出请求时,我收到404错误:
$.ajax({
url: "/batchQueryServlet",
contentType: 'application/json',
data: json,
Accept: "text/html",
success: function(data, textStatus, jqXHR) {
alert(data);
},
type: "POST"
});
Now, I'm pretty sure it doesn't find the class because if I call the servlet name I get a classNotFound exception. 现在,我非常确定它找不到该类,因为如果调用servlet名称,则会收到classNotFound异常。 How could I fix that? 我该如何解决? (A quick and dirty solution would be using a JSP but I'd rather not do it). (一种快速而肮脏的解决方案将使用JSP,但我宁愿不这样做)。
PS: I use version 7.0.34 of Tomcat. PS:我使用的是Tomcat 7.0.34版。
EDIT The problem was I should use a proper src/java path for storing my .java classes instead of a plain folder (this is the reason I couldn't see the packages). 编辑问题是我应该使用正确的src / java路径来存储.java类,而不是使用普通文件夹(这是我看不到软件包的原因)。 Now I've fixed it. 现在,我已修复它。 Thanks anyway for your time! 无论如何,谢谢您的时间!
You have a leading slash in 您的斜线为
<servlet-class>/batchUtility.batchQueryServlet</servlet-class>
Remove it and things should work. 删除它,一切正常。
Change your Ajax url:
to either "batchQueryServlet"
or "/web-app-name/batchQueryServlet"
将您的Ajax url:
更改为"batchQueryServlet"
或"/web-app-name/batchQueryServlet"
EDIT : 编辑 :
Once deployed your web application's folder structure should be like: ( /
indicates a directory) 部署后,您的Web应用程序的文件夹结构应类似于:( /
表示目录)
tomcat-home/
|- webapps/
|- BatchWebApp/ //<-- Context-Root (Web-app's name)
|- *.html, *.jsp etc.
|- WEB-INF/
|- web.xml
|- lib/
|- *.jar files
|- classes/ //<-- ALL your servlets go here
|- batchUtility/ //<-- with the required package/folder structure
|- batchQueryServlet.class
If you're using an IDE (like Eclipse) it does the same for you. 如果您使用的是IDE(例如Eclipse),它会为您做同样的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.