如何使用套接字将数据从一个jsp传输到另一个jsp
我有 3 个 .jsp。第一个询问用户他们的用户名。提交表单后,它将被带到第二个 jsp,其中为用户创建唯一的密码。我将如何获取此密码并使用套接字将其传递给第三个 jsp?
I have 3 .jsp's. The first one asks the user for their username. Once the form is submitted it is taken to a 2nd jsp where a unique passcode is created for the user. How would I go about taking this passcode and passing it to a 3rd jsp using a socket?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
java.net.URL< /code>
和
java.net.URLConnection
以编程方式触发和处理 HTTP 请求。它们在幕后使用套接字,这样您就不需要摆弄有关 HTTP 协议的低级细节。您可以将参数作为 URL 中的查询字符串传递。这样,在第 3 个 JSP 中可以通过
${param.passcode}
或request.getParameter("passcode")
获取passcode
请求参数通常的方式。不过,更好的方法是仅将第三个 JSP 包含在第二个 JSP 中。
这样,
passcode
就可以通过${passcode}
或request.getAttribute("passcode")
通常在第 3 个 JSP 中作为请求属性使用。方式。另请参阅:
与具体问题无关,但是这是一个非常令人讨厌的黑客行为,其目的超出了我的范围。您的应用程序中存在严重的设计缺陷。这些 JSP 很可能与业务逻辑紧密耦合,而业务逻辑实际上属于普通且可重用的 Java 类,例如 servlet 和/或 EJB 和/或 JAX-WS/RS,您只需在 Java 类中导入并调用它们即可通常的 Java 方式。 JSP 旨在生成和发送 HTML,而不是充当业务服务,更不用说 Web 服务了。另请参阅如何避免 JSP 文件中出现 Java 代码?
You can use
java.net.URL
andjava.net.URLConnection
to fire and handle HTTP requests programmatically. They make use of sockets under the covers and this way you don't need to fiddle with low level details about the HTTP protocol. You can pass parameters as query string in the URL.This way the
passcode
request parameter is available in the 3rd JSP by${param.passcode}
orrequest.getParameter("passcode")
the usual way.Better is however to just include that 3rd JSP in your 2nd JSP.
This way the
passcode
is available as request attribute in the 3rd JSP by${passcode}
orrequest.getAttribute("passcode")
the usual way.See also:
Unrelated to the concrete question, this is however a terribly nasty hack and the purpose of this is beyond me. There's somewhere a serious design flaw in your application. Most likely those JSPs are tight coupled with business logic which actually belongs in normal and reuseable Java classes like servlets and/or EJBs and/or JAX-WS/RS which you just import and call in your Java class the usual Java way. JSPs are meant to generate and send HTML, not to act as business services, let alone web services. See also How to avoid Java code in JSP files?
那么,您希望通过向第二个 JSP 提交表单来将用户名从第一个 JSP 提交到第二个 JSP,对吧?
但是,对于第二个和第三个之间的交互,您希望避免使用 JSP 文件背后的通信机制并使用您自己的通信机制,对吗?
那么,您如何实现这一点取决于您的通信发送地点和目的地。例如,它们是在同一台机器上还是在不同的机器上?
一般来说,您需要在此处设置客户端-服务器类型的关系。我想您会希望第三个 JSP 充当服务器。
第三个 JSP 要做的就是等待客户端尝试与其通信。但是,在执行此操作之前,您首先需要将端口绑定到您的应用程序。端口由操作系统分配并提供给请求进程。
当尝试在 Java 中实现此功能时,您可能需要尝试如下操作:
在上面的示例中,ServerSocket 已经绑定到指定的端口 1080。它不必是 1080 - 1080 只是一个示例。
接下来,您将需要监听并等待请求的到来。您可以通过以下方式实现此步骤:
Socket request = null;
这将导致服务器套接字不断循环,直到最终收到请求。当请求到来时,它将创建一个新的 Socket 对象来处理该请求。因此,您可以稍后返回循环并继续等待和接受请求,同时子线程使用新创建的
request
Socket
处理通信。但是,对于您的项目,我猜您不需要一次与多个客户进行通信,所以我想,如果我们在收到请求后就停止倾听,那就没问题了。
那么,现在进入客户端应用程序。这里,它与我们在服务器上的有点不同。首先,客户端的套接字将主动尝试连接到其端口上的远程主机,而不是侦听端口并等待请求。因此,如果没有服务器侦听该端口,则连接将失败。
因此,需要知道两件事:
使用 Java Socket 类获取连接有捷径,但我假设您将在同一台机器上测试它,对吗?如果是这样,那么您将需要客户端和服务器有两个单独的端口。这是因为操作系统不允许两个单独的进程共享同一端口。一旦进程绑定到端口,就不允许其他进程访问它,直到该端口将其释放回操作系统。
因此,为了使两个独立的 JSP 在同一台物理机器上进行通信,您需要客户端的本地端口,并且需要它正在侦听的服务器的端口号。
那么,让我们对客户端应用程序尝试以下操作:
这应该会对您有所帮助。
最后需要注意的是。实际上您无法使用同一个 JVM 运行这两个应用程序。您需要两个单独的进程来运行客户端和服务器应用程序。
So, you want the username to be submitted from the first JSP to the second, by submitting a form to the second, right?
But, for interaction between the second and third, you want to avoid using the communication mechanisms behind the the JSP files and use your own, right?
Well, how you might implement doing this depends on where you're sending your communication from and to. For instance, are they on the same machine, or on different machines?
Generally speaking, you'll need a client-server type of relationship to be set up here. I imagine that you would want your third JSP to act as the server.
What the third JSP will do is will sit and wait for a client to try to communicate with it. But, before you can do that, you'll first need to bind a port to your application. Ports are allocated by the Operating System and are given to requesting processes.
When trying to implement this in Java, you might want to try something like the following:
In the above example, the ServerSocket is already bound to the specified port 1080. It doesn't have to be 1080 - 1080 is just an example.
Next, you will want to listen and wait for a request to come in. You can implement this step in the following:
Socket request = null;
This will cause the server socket to keep looping until it finally receives a request. When the request comes in, it will create a new
Socket
object to handle that request. So, you could come back to your loop later on and continue to wait and accept requests, while a child thread handles communication using your newly createdrequest
Socket
.But, for your project, I would guess that you don't need to communicate with more than one client at a time, so it's okay if we just simply stop listening once we receive a request, I suppose.
So, now onto the client application. Here, it's a little bit different from what we had with the server. First off, instead of listening in on the port and waiting for are request, the client's socket will actively try to connect to a remote host on their port. So, if there is no server listening in on that port, then the connection will fail.
So, two things will need to be know, those are:
There are short-cuts to getting the connection using the Java Socket class, but I assume that you're going to test this out on the same machine, right? If so, then you will need two separate ports for both your client and server. That's because the OS won't allow two separate processes to share the same port. Once a process binds to the port, no other process is allowed to access it until that port releases it back to the OS.
So, to make the two separate JSP's communicate on the same physical machine, you'll need both a local port for your client, and you'll need the server's port number that it's listening in on.
So, let's try the following for the client application:
That should help you along your way.
A final note should be made here. You can't actually run these two applications using the same JVM. You'll need two separate processes for client and server applications to run.