[英]how to create something to receive the data from a page in a tomcat server?
I've been seeing lots of examples of POST
ing and GET
ing using AJAX
, but I don't see many examples on how to receive that information on the server side, specially with a tomcat server. 我已经看到了很多使用AJAX
进行POST
和GET
的示例,但是我没有看到很多有关如何在服务器端(尤其是tomcat服务器)接收该信息的示例。 I have this page, and I would like to know what should I have to receive/handle that information on server-side (Tomcat)? 我有此页面,我想知道我应该如何在服务器端(Tomcat)上接收/处理该信息?
Thanks in advance. 提前致谢。
$(function() { alert("file has been succesfully sent"); var data = new FormData(); var cssData = $("#custom-css-text").val(); data.append("custom_css", cssData); $.ajax({ url: 'myserver', type: 'POST', data: data, cache: false, dataType: 'json', processData: false, contentType: false, success: function(response) { alert("file has been succesfully sent"); }, error: function(jqXHR, textStatus, errorThrown) { alert('ERRORS: ' + textStatus); } }); });
textarea { border-style: outset; }
<!DOCTYPE html> <html> <head> <title>HTML5, CSS3 and JavaScript demo</title> <script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script> </head> <body> <textarea id="custom-css-text">testing</textarea> </body> </html>
You probably want to create a Servlet to handle that request in Tomcat. 您可能想要创建一个Servlet来在Tomcat中处理该请求。 The most barebones way to do that would be to create a war file . 做到这一点的最准方法是创建一个战争文件 。
Eclipse can usually set up one of these projects for you pretty quickly, and the way it works is by using a web.xml to map urls to java classes. Eclipse通常可以很快为您建立这些项目之一,其工作方式是使用web.xml将URL映射到Java类。
<servlet>
<servlet-name>ServletServlet</servlet-name>
<servlet-class>weblogic.servlet.ServletServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletServlet</servlet-name>
<url-pattern>/myservlet/*</url-pattern>
</servlet-mapping>
The above snippet, taken from the 'create a war file' link, will associate the class weblogic.servlet.ServletServlet
with the endpoint http://<server>:<port>/myservlet/
on the server you deploy the war file to. 上面的摘录来自“创建war文件”链接,它将把weblogic.servlet.ServletServlet
类与将war文件部署到的服务器上的端点http://<server>:<port>/myservlet/
。 。
The class itself just needs to extend HttpServlet, and you can override the doGet()
method. 该类本身只需要扩展HttpServlet,并且您可以重写doGet()
方法。
Any GET
request to anything on your servlet's path will enter this method, and Tomcat will populate the request's information as necessary. 对Servlet路径上任何内容的任何GET
请求都将输入此方法,并且Tomcat将根据需要填充请求的信息。 You can then take the Writer
out of the response object and write data to it, which, when your method returns, will be sent back to the client. 然后,您可以将Writer
从响应对象中取出并向其中写入数据,当方法返回时,该数据将被发送回客户端。
As an example for your case: 以您的情况为例:
Web.xml Web.xml
<servlet>
<servlet-name>CSSServlet</servlet-name>
<servlet-class>com.foo.CSSServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CSSServlet</servlet-name>
<url-pattern>/myserver/*</url-pattern>
</servlet-mapping>
CSSServlet.java CSSServlet.java
public class CSSServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response){
response.setContentType("text/html");//I'm not sure if 'text/css' is valid here. Might be worth trying.
PrintWriter out = response.getWriter();
out.print(myCss); //Get your css initialized into this var.
}
}
See this tutorial for another quick intro. 有关另一个快速入门,请参见本教程 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.