简体   繁体   English

如何创建一些东西来从雄猫服务器中的页面接收数据?

[英]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进行POSTGET的示例,但是我没有看到很多有关如何在服务器端(尤其是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.

相关问题 如何从两个客户端上的服务器接收数据 - How to receive data from server on two clients 错误:将数据从html页面发送到服务器,并从服务器接收一些数据 - error: send data from html page to server and receive some data from server 如何在angularjs中接收从另一个页面发送的POST数据 - how to receive POST data sending from another page in angularjs 如何从其他html页面的html表单POST接收数据? - How to receive data from html form POST at other html page? 如何接收从客户端应用程序传递到jsreport服务器的数据 - How to receive the data that is being passed from client app to the jsreport server 如何从其他服务器接收数据(如推送通知) - How to receive data from another server (like push notification) 如何通过 JS fetch 向服务器发送数据/从服务器接收数据 - How to send/receive data to/from server via JS fetch 如何使用AJAX从跨域服务器接收json数据? - How to receive json data from cross domain server using AJAX? 如何在从服务器接收数据后更新React Google Map - How to update React Google Map after receive data from server 如何等待来自服务器的数据(数组),然后在 js 中将一些东西推入其中? - How wait for data(Array) from server and then push something into it in js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM