In my server-client model I'm using java for client side and for server side scripting using php5.
For communication I'm using simple http protocol.
In server I have some $SESSION variables(in php). I want access these variables in my java client.
How can it be posssible?
Thanks in advance.
AFAIK session variables is a server-side variables, that can be accessed by session id. You must refactor you application (to make java-client free of server side information) or send them (variables) from server to client manually.
You should use cookies instead of session variables if you're planning on sending it to the client side.
Your Java-client will make requests to the server, and the server will act like a web service, right?
If so, include all/any $_SESSION variables you might need with the XML/json/whatever response that the server sends back to the clients, something along the lines of:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<reply>
<request>
<method>getUsers</method>
<param name="page">1</param>
<param name="apiKey">API_KEY</param>
<param name="sessId">SESSION_ID</param>
</request>
<response>
<status>
<code>0</code>
<message>OK</message>
</status>
<users>
<!-- Request reply here -->
</users>
<session>
<variable1>$_SESSION['var1']</variable1>
<!-- Additional session variables you might need on the client here[...] -->
</session>
</response>
</reply>
The thing is, session variables are something the server uses to keep track of which client is accessing it now, and relate data to that particular client. If you need this information on the client, perhaps you could refactor the application to create this data on the client and pass it to the server in case the server needs that data?
Simple.
<script type="text/javascript">
var variable = '<?php echo $SESSION["variable_name"];?>';
</script>
The only thing you have to understand is that session variables has no difference from any other PHP variable.
Thus, you have to request it from PHP as well.
Of course, supplying a request with session identifier got from the previous calls. That's all, not a rocket science.
Related
I have created a websocket restful url like
ws://localhost:8080/wsserver/getPrice
and used a index.html page that consumes it as
socket = new Socket (url);
When i run the client i am able to get the price as per requirement.But what bothers me is that i have like
http://localhost:8080/goldBuying/index.html
as my page.
But i want that like
ws://localhost:8080/goldBuying/index.html
because i think that is only complete websocket implementation as client.
How to achieve that or what i have done is only possible.
Websockets are used for bidirectional data transfer between client and server. HTML files are used for displaying digital documents. These are two different things.
So, no, you can't use something like ws://localhost:8080/goldBuying/index.html, the ws:// protocol always defines a server that receives or sends data.
I have come across many examples of implementing a simple http server in Java. This one fits my needs: http://www.rgagnon.com/javadetails/java-have-a-simple-http-server.html
However, I can't find an example of how to generate, return, and maintain a session id from such a simple http server.
Is that even possible? Is there a way to modify the sample code referred above to incorporate this functionality?
Thanks.
HTTP does not have session support on it self once it is a stateless application protocol. So you need implement it by your self.
For example, on servlet containers like Tomcat there is a cookie called JSESSIONID that is generated and stored on the browser. The client sends back the cookie to the server on each request. Once each client has a different cookie the server can identify the client session.
When cookies are not allowed the parameter JSESSIONID is added to the URL for each request. This technique is called URL Rewriting.
There is a question, not specific for Java HTTP servers, that has implementation details for this problem.
HTTP Session Tracking
Let's say i've a lot of different of sub-domains ,
I want to redirect each request to the server so it will response with different action on each request of sub-domain
(with text or html generated on server)
I'm working with EJB 3.1 on Tomcat 7 using web.xml , and I want to use with only one webapp
Example:
http://companyName1.mycompany.com
responce to client: "hello aaa companyName1 ..."
http://companyName2.mycompany.com
responce to client: "hello bbb companyName2 ..."
http://companyName3.mycompany.com
responce to client: "hello ccc companyName3 ..."
What is correct web.xml or server.xml mapping to solve this issue ??
Thanks!!
You want server.xml, not web.xml. Add an alias for each subdomain in the appropriate Host block.
So you would have something like this:
<Host name="mycompany.com" appBase="webapps" ... >
<Alias>companyName1.mycompany.com</Alias>
<Alias>companyName2.mycompany.com</Alias>
<Alias>companyName3.mycompany.com</Alias>
...
</Host>
Then use request.getServerName() to determine the subdomain.
The previous answer regarding server.xml will definitely work, but requires a restart every time you need to add/modify it.
Another way would be to keep the standard server.xml, but use request.getServerName() to examine hostname the user used to access your server.
Here is my requirement.
I have a client that was sending a specific message format to some software service provider(ABC) using what ever network protocol.
Now this client is switching software service provider (XYZ) but does not want to change their software and wants to continue sending the same message of ABC provider.
Provider ABC uses some sort of positional based message format over some archaic network protocol
Provider XYZ uses XML over HTTP(s) web service not SOA just simple POST with XML
Some values can be directly mapped while others must be recalculated or modified. For instance converting client's account number from ABC Provider to account number of XYZ provider. this is for request and response.
The archaic network protocol will be converted to TCPI/IP at the network level so that is not an issue.
The client expect a response in real time. I.e: Client makes request, XYZ does what ever and response back to client.
So I need to create some sort of tunnel that accepts TCP/IP converts the message to XML, sends it HTTP(s) POST to XYZ, get back XML response, convert back to positional based, reply back through TCPI/IP to the client.
Is this an ESB type thing, should I just write some sort JAVA server app that will do this?
You have too many options. Starting from Python, Perl.. there are libraries/modules that provide your TCP/HTTP/telnet/... support. Using that you can do this.
ESB might be an overkill for this, it's a big framework for big integrations, but yes you can write your BCs (Binding Components) and do it. Performance I'm not sure if ESB would be as good as something more primitive like .pl/.py
In short there are too many options. You have to take the call based on the environment in which you have to deploy it.
E.g. you said "Some values can be directly mapped while others must be recalculated or modified. For instance converting client's account number from ABC Provider to account number of XYZ provider. this is for request and response.", if ABC-A/c-num to XYZ-A/c-num mapping is only available via a remote EJB call then you might want to consider writing the whole thing in J2EE. If you "calculations" need some specific libs you have to factor that in as well...
Hope this doesn't make your problem worse.. :)
PS: You should start accepting answers to your previous questions. as Home said.
Tried Mule ESB it's very light weight. I would say even more so then some of it's competition. I like how it's not particularly bound to SOA or soap. So you can construct any kind of end point from plain TCP/IP, HTTP, File, to Email to anything really it has more then a dozen connectors and you can even write your own. I also like the fact that the message can be anything. I could be wrong but even when others claim they have various connectors and message formats they seem to have SOAP somewhere hidden under the hood and it seems icky lol Some others seem good, but have poor docs. Didn't like the fact that some basic JDBC functionality is only available in the enterprise edition. I.e: To get Output parameters from your stored proc call you need the enterprise edition.
In reality I could have picked any server API that allowed me to create a server to receive HTTP, then write my code to transform a message, open a client connection to the next service send it off, receive the response, re-transform it back and return to the client. All while making sure threads and queues are behaving correctly within the server. Maybe I could have just used jetty and a servlet and do everything in one request. It's an option, but if I have to switch to TCP/IP then I just change the config a bit. Waiting to see what the client says.
The ESB is basically the glue and plumbing. All I had to do was write three custom transformer classes as the messages I deal with are proprietary banking formats and don't exactly map to simple XSLT/XML transforms... And about a 20 XML tags to put it all together...
This is all I needed to get this out of Mule (not including the tags to build my datasource and SQL query)...
<flow name="myFlow">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" />
<object-to-string-transformer />
<custom-transformer class="com.mycom.transformer.MyTransformer" />
<enricher target="#[variable:client]">
<jdbc:outbound-endpoint queryKey="getClientConfig" exchange-pattern="request-response" />
</enricher>
<custom-transformer class="com.MyCom.transformer.MyOtherTransformer" />
<http:outbound-endpoint exchange-pattern="request-response" host="xxx.xxx.xxx.xxx" port="80" path="some path" method="POST"/>
<custom-transformer class="com.MyCom.transformer.BackToOtherFormat" />
</flow>
So basically...
1- receive HTTP(s) with custom message
2- Parse message to get client number
3- Lookup client number in database to get client number for the other service...
4- Create the new message
5- Send off to other service
6- Transform response back
7- Return to client
<flow name="FileTransferFlow1" doc:name="FileTransferFlow1">
<file:inbound-endpoint path="C:\mule_projects\filetransfer\in" responseTimeout="10000" doc:name="IncomingFile"/>
<http:outbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP" responseTransformer-refs="Java"/>
<custom-transformer class="com.Transformer" doc:name="Java"/>
<file:outbound-endpoint path="C:\mule_projects\filetransfer\out" responseTimeout="10000" doc:name="OutgoingFile"/>
</flow>
I have a servlet hosted in the glassfish server. i want to communicate with it using telnet to understand what is going on behind the scenes when using html form get method.
What should i give in the Host field of the HTTP request?
Get /WebApplication1/NServlet HTTP/1.1
Host: localhost
If i want to send custom properties in the HTTP request as below, is it possible to extract their value using request.getAttribute() method.
Get /WebApplication1/NServlet HTTP/1.1
Host: localhost
Custom-Attribute: xyz
Another doubt is that is javax.servlet package not a part of java SE sdk. i had to install java ee to get it running.
The Host field is just the hostname part of the URL, e.g. Host: google.com for http://google.com/
Custom-Attribute: xyz would be exposed in the HttpServletRequest using getHeader(), not getAttribute().
If you want to use HTTP for your protocol (as you've suggested in your comments), check out HttpClient. As the name suggests, it's the client-side of the client/server HTTP implementation, and it should be relatively easy to determine what to set on the client side such that you see it on the server.
There's a great tutorial here. I would perhaps get a simple page working in the servlet first, and check it via the browser, and then implement the client side.
Based on your question, I don't there's enough information for anyone to answer you. Tomcat/Jetty/etc are basically web servers that contain servlets (and therefore JSP/JSF/Wicket etc etc) processors for dynamically generating content.
So, what is it you're trying to figure out, and why?