我一直在研究很多关于servlet的东西,我已经创建了一个应该从我的网络应用程序中获得一些位置,但由于我缺乏servlet的经验,我不知道如何修复它。另外,我不知道我应该在哪里放置我的HTML应用程序,我听说它应该在WebContent文件夹中,并且它就是,但它不起作用。你可以帮帮我吗?
更新:我正在尝试将一些将由我的servlet转换为txt
文件的信息上传,以避免每次上传文件:“... 我'我创建了一个应该从我的网络应用程序中获得一些位置 ......“
服务器:Apache Tomcat 7.0
Eclipse EE Mars
提前谢谢。
我的整个项目都在这里:DropBox
PS:我在下面发布的片段只是为了观察,但我不能使用脚本,因为我不拥有图书馆所在的业务,如果你想看到它在这个PostScript上面的链接上查看。< / p>
我的带有AJAX的HTML
<body>
<div id="navgrid">
<div id="header">Header</div>
<div id="tree1">
<ul class="jqtree_common jqtree-tree">
<li class="jqtree_common jqtree-folder">
<div class="jqtree-element jqtree_common"><a class="jqtree_common jqtree-toggler">â–¼</a><span class="jqtree_common jqtree-title jqtree-title-folder">node1</span>
</div>
<ul class="jqtree_common ">
<li class="jqtree_common">
<div class="jqtree-element jqtree_common"><span class="jqtree-title jqtree_common">child1</span>
</div>
</li>
<li class="jqtree_common">
<div class="jqtree-element jqtree_common"><span class="jqtree-title jqtree_common">child2</span>
</div>
</li>
</ul>
</li>
<li class="jqtree_common jqtree-folder">
<div class="jqtree-element jqtree_common"><a class="jqtree_common jqtree-toggler">â–¼</a><span class="jqtree_common jqtree-title jqtree-title-folder">node2</span>
</div>
<ul class="jqtree_common ">
<li class="jqtree_common">
<div class="jqtree-element jqtree_common"><span class="jqtree-title jqtree_common">child3</span>
</div>
</li>
</ul>
</li>
</ul>
</div>
</div>
<script type="text/javascript">
</script>
<script>
$(document).ready(function() {
var POSITIONS;
//var data is a dynamic JSON file that should be created in the backend.
var data = [{
label: 'node1',
id: 1,
children: [{
label: 'child1',
id: 2
}, {
label: 'child2',
id: 3
}]
}, {
label: 'node2',
id: 4,
children: [{
label: 'child3',
id: 5
}]
}];
$('#tree1').tree({
data: data,
autoOpen: true,
dragAndDrop: true
});
console.log($('#tree1').tree('toJson')); //This will give you the loading jqtree structure.
$('#tree1').bind(
'tree.move',
function(event) {
event.preventDefault();
// do the move first, and _then_ POST back.
event.move_info.do_move();
console.log($(this).tree('toJson')); //this will give you the latest tree.
POSITIONS = $(this).tree('toJson');
alert(POSITIONS);
$.post('http://sistema.agrosys.com.br/sistema/labs/CSS_HTML/', {
tree: $(this).tree('toJson')
});
alert("done"); //this will post the json of the latest tree structure.
}
);
var data = new FormData();
data.append("JqTree", POSITIONS);
alert('Sending: ' + POSITIONS);
$.ajax({
url: '/JqTree',
type: 'POST',
data: data,
cache: false,
dataType: 'json',
processData: false,
contentType: false,
success: function(response) {
alert("file has been successfully sent\n\n" + POSITIONS);
},
error: function(jqXHR, textStatus, errorThrown) {
alert('ERRORS: ' + textStatus);
}
});
});
</script>
</body>
我的Servlet
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Hello extends HttpServlet {
private static final long serialVersionUID = 1L;
public Hello() {}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.print("<html><body>");
out.print("<h3>Hello Servlet</h3>");
out.print("</body></html>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String position = request.getParameter("JqTree");
PrintWriter writer = new PrintWriter("Positions.txt", "UTF-8");
writer.println(position);
writer.close();
}
}
和我的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>JqTree</display-name>
<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>
<description></description>
<display-name>Hello</display-name>
<servlet-name>Hello</servlet-name>
<servlet-class>Hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>
</web-app>
答案 0 :(得分:1)
您需要将AJAX调用更改为
$.ajax({
url: '/JqTree/Hello',
其中/JqTree
是您的网络应用的上下文根,加上/Hello
,这是<url-pattern>
文件中Hello
Servlet配置的/WEB-INF/web.xml
。
$.ajax()
来电正在使用post
,因此您必须记住只会调用doPost()
。doPost()
无法返回任何输出,但$.ajax()
来电需要dataType: 'json'
。这有时会导致请求无声地失败。request.getParameter()
读取文件内容。您必须以不同的方式编写Servlet代码以处理multipart/form-data
,这取决于您是否在Servlet 3.0或2.x容器下运行。