1

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?

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
cbj
  • 37
  • 1
  • 8
  • 1
    Using a socket? Can you elaborate more on that part? Do you want to invoke the 3rd JSP programmatically from inside the 2nd JSP by a HTTP request or something? – BalusC Oct 20 '11 at 17:08
  • Yes, basically but I want it to be invoked in the background hence the socket. – cbj Oct 20 '11 at 17:12
  • what does your second JSP? displaying the passcode to the user? – Cygnusx1 Oct 20 '11 at 17:21
  • Yes, but it is only there for debugging. The only function of the 2nd .jsp is to generate the passcode. – cbj Oct 20 '11 at 17:22
  • what do you want to do whit the passcode in you 3rd JSP? persitence? showing it to the user? – Cygnusx1 Oct 20 '11 at 18:19
  • The 3rd jsp contains the function which determines what the user has access too. It is formatted in XML. I need to populate a table with the formatted XML data. – cbj Oct 20 '11 at 19:21

2 Answers2

2

You can use java.net.URL and java.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.

String url = "http://localhost:8080/context/3rd.jsp?passcode=" + URLEncoder.encode(passcode, "UTF-8");
InputStream input = new URL(url).openStream();
// ... (read it, it contains the response)

This way the passcode request parameter is available in the 3rd JSP by ${param.passcode} or request.getParameter("passcode") the usual way.

Better is however to just include that 3rd JSP in your 2nd JSP.

request.setAttribute("passcode", passcode);
request.getRequestDispatcher("3rd.jsp").include(request, response);

This way the passcode is available as request attribute in the 3rd JSP by ${passcode} or request.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?

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • So, how would you suggest doing it? I have a sample class to connect but using sockets but it's really confusing... – cbj Oct 20 '11 at 17:48
  • Why would you fiddle with low level sockets if `java.net.URL` is there? The code which I posted in my answer really works. Did you understand and try it? It uses sockets under the covers but hides all the nasty HTTP protocol details for you into only a few lines of code. As to the unrelated note on the design approach, just stop writing Java code in JSP files. You'll almost automatically be forced to do the right thing. – BalusC Oct 20 '11 at 17:49
  • But, isn't passing information through the URL insecure? Couldn't he pass it through the HTTP Header instead? – user919860 Oct 20 '11 at 18:01
  • @user919860: it isn't sent to the client or something. It doesn't ask the client to send that request. It takes place **entirely server side**. Look, the URL points to `localhost`. – BalusC Oct 20 '11 at 18:02
  • Anyways, people use sockets for various reasons. There's specific things that a Socket can do that a URL can't. The OP didn't ask for a solution using URL's so a solution using URL's isn't really helpful. If you were going to go with something other than sockets, then you could have suggested anything that the Java Enterprise framework might already provide. Such as servlet chaining, storing the password in a session, or just writing an html submission form. – user919860 Oct 20 '11 at 18:03
  • @user919860: the OP is clearly being ignorant, I won't waste time to explain in detail how to send HTTP requests over sockets. – BalusC Oct 20 '11 at 18:07
  • i agree 100% with @BalusC. The way JSP are used in this question is very inapropriate. The way i see it, only 2 JSP are needed: 1 to submit the username to a servlet/Controller. Servlet/Controller generate a passcode and whatever else needed, then a response is sent to the client via JSP #2. – Cygnusx1 Oct 20 '11 at 18:15
  • Although this method works fine, I need assistance on the next step. The 3rd jsp contains the function which determines what the user has access too. It is formatted in XML. How would I populate a table with the data i just requested from the 3rd jsp? – cbj Oct 20 '11 at 19:20
  • This is offtopic. Just ask a new question. As you already did. – BalusC Oct 20 '11 at 19:26
  • @ Balus C: Right, but in that case, why even use URL's when the Enterprise container uses sockets underneath as well? My only problem with your solution was that it didn't use sockets explicitly and the OP asked for sockets. – user919860 Jan 04 '12 at 17:40
0

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:

int port_number = 1080;
ServerSocket server = new ServerSocket(port_number);

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;

while((request = server.accept()) == 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 created request 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:

  1. What's the IP Address of the server?
  2. What port is the server listening in on?

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:

int local_port = 1079;
int remote_port = 1080;

InetSocketAddress localhost = new InetSocketAddress(local_port);
Socket client = new Socket();  //The client socket is not yet bound to any ports.
client.bind(localhost);  //The client socket has just requested the specified port number from the OS and should be bound to it.
String remoteHostsName = "[put something here]";
InetSocketAddress remotehost = new InetSocketAddress(InetAddress.getByName(remoteHostsName),  remote_port);  //Performs a DSN lookup of the specified remote host and returns an IP address with the allocated port number

client.connect(remotehost);  //Connection to the remote server is being made.

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.

user919860
  • 2,873
  • 4
  • 16
  • 16
  • The problem is that OP has the Java code in JSP files. This ain't going to work. JSP files are executed by HTTP requests only and a single JSP file is shared throughout the entire application. – BalusC Oct 20 '11 at 18:08