简体   繁体   English

我需要 servlet 来进行服务器端 Java 编程吗?

[英]Do I need servlets for server-side Java programming?

I need to program a small server to handle requests from an Android application.我需要编写一个小型服务器来处理来自 Android 应用程序的请求。 (The user sends a number to the server, the server does some math on the numbers received from each user - the average, let's say - and returns it). (用户向服务器发送一个数字,服务器对从每个用户收到的数字进行一些数学计算 - 比方说的平均值 - 并返回它)。

I've just read this simple introduction from Oracle ( http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html ).我刚刚阅读了 Oracle 的这个简单介绍( http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html )。

It explains how to leave a server listening on a specific door.它解释了如何让服务器监听特定的门。 Normal java, no special application needed (such as Tomcat).普通java,不需要特殊应用(如Tomcat)。

I know people generally use 'servlets' (with Tomcat) to accomplish this kind of thing.我知道人们通常使用“servlet”(使用 Tomcat)来完成这种事情。 Why is that?这是为什么? What are the advantages?有哪些优势? Can't i just program a sample server like the one described in that simple tutorial, and leave it always running on a computer?我不能像那个简单教程中描述的那样只编写一个示例服务器,然后让它始终在计算机上运行吗?

Using a socket is very simple.使用套接字非常简单。 At first.首先。

You create your server class, you have it bind to a port and away you go.你创建你的服务器类,你把它绑定到一个端口,然后就可以了。

The first hurdle you will hit, covered in the comments, is multi-threading.您将遇到的第一个障碍(在评论中介绍)是多线程。 But a simple producer/consumer pattern will solve that for you in no time.但是一个简单的生产者/消费者模式将立即为您解决这个问题。

The next problem you will hit is protocol .您将遇到的下一个问题是协议

  • Who talks first?谁先说话?

  • How do you respond?你如何回应?

  • How do you deal with an invalid request?你如何处理无效的请求?

  • What happens if the stream collapses during a request?如果流在请求期间崩溃会发生什么?

  • Do you open a new socket for each request or does a client hold onto a socket and write multiple requests?您是为每个请求打开一个新的套接字还是客户端持有一个套接字并写入多个请求?

  • Maybe you want some sort of non-blocking IO?也许你想要某种非阻塞 IO?

This is where HTTP comes in, it is a protocol for communicating over TCP/IP (actually over anything, you could use bits of paper and a bike).这就是 HTTP 的用武之地,它是一种通过 TCP/IP 进行通信的协议(实际上,您可以使用一些纸片和一辆自行车)。 It defines answers to all the above questions (and many more).它定义了上述所有问题(以及更多问题)的答案。

So, you run a webserver (tomcat, glassfish) and it deals with the raw sockets and sending the right information.因此,您运行一个网络服务器(tomcat、glassfish),它处理原始套接字并发送正确的信息。

A servlet is an abstraction, when Tomcat has a connection and it has negotiated compression, encryption etc it will pass a request onto the servlet. servlet 是一个抽象,当 Tomcat 有一个连接并且它已经协商压缩、加密等时,它会将请求传递给 servlet。

The servlet doesn't have to worry about the raw socket, it reads the request and writes a response. servlet 不必担心原始套接字,它读取请求并写入响应。

It's worth pointing out that HTTP isn't the only protocol, it's just the one happens to be used for web-browsing.值得指出的是,HTTP 并不是唯一的协议,它只是恰好用于 Web 浏览的协议。 And so the one used by web-servers.网络服务器使用的也是如此。

You do not need servlets.您不需要 servlet。

Servlets are helpful because they manage the socket handling for you but the drawback is you need a container (such as Tomcat) to run your servlets on. Servlet 很有用,因为它们为您管理套接字处理,但缺点是您需要一个容器(例如 Tomcat)来运行您的 servlet。 You may want to look at Netty which is really built to do the kind of work you are talking about.您可能想看看Netty ,它确实是为完成您所谈论的工作而构建的。

Servlets are the standard way of handling the HTTP protocol in Java. Servlet 是 Java 中处理 HTTP 协议的标准方式。

If you want to use HTTP for your client-server communication and you want to use Java on the server side, then you should use servlets.如果您想在客户端-服务器通信中使用 HTTP并且想在服务器端使用 Java,那么您应该使用 servlet。

Servlets are a simple and effective solution for communicating in Internet with Java. Servlet 是一种简单而有效的解决方案,用于在 Internet 上使用 Java 进行通信。 For your needs Netty, as suggested by rancidfishbreath, is a valid alternative but I recommend servlets with Tomcat because it's a simpler solution.根据您的需要,如rancidfishbreath 所建议的那样,Netty 是一个有效的替代方案,但我推荐使用 Tomcat 的 servlet,因为它是一个更简单的解决方案。 Having to use Tomcat is not a problem: it's light and free.必须使用 Tomcat 不是问题:它轻巧且免费。

Discard the idea of using raw sockets for the reasons already explained by bmorris591.出于 bmorris591 已经解释的原因,放弃使用原始套接字的想法。

You may want to look at something other than Java as the server-side solution.您可能希望将 Java 以外的其他东西视为服务器端解决方案。 Perhaps Node.js , PHP , or maybe even Ruby on Rails might be a better, no-bells-and-whistles options.也许Node.jsPHP ,甚至Ruby on Rails可能是更好的、没有花里胡哨的选择。 I can probably list 100 more options (Python, Django, etc, etc, etc) that would be way more simpler than trying to learn Java and Servlets and all that comes along with it.我可能会列出 100 多个选项(Python、Django 等,等等),它们比尝试学习 Java 和 Servlet 以及随之而来的所有内容要简单得多。 You don't necessarily need to have a Java app talking to a Java app, and there are loads of options for getting simple HTTP servers running custom solutions that don't require a huge learning curve.您不一定需要让 Java 应用程序与 Java 应用程序通信,并且有很多选项可以让简单的 HTTP 服务器运行自定义解决方案,这些解决方案不需要很大的学习曲线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM