server-side java applet...how to create? - java

How would you create a server-side java applet?

If I understand correctly, you are looking for Servlets. Read the linked documentation.
Otherwise, your question doesn't make sense - the server is processing multiple requests, without any GUI, and applets are GUI.

If you just want java code that runs on the server, you probably do want Servlets. Or perhaps JSP, if you're just looking for something to do simple processing.

A Java applet on the client side does not necessarily require a Java webserver on the server side. Since the only communication protocol you'd like to use is HTTP which is universal, any HTTP server would suffice. You can use a "plain vanilla" webserver like Apache HTTPD with PHP. You can also use a Java Servletcontainer like Apache Tomcat which supports JSP/Servlet. You can also use a C#/.NET webserver like IIS which supports ASP. Just make use the capabilities of the webserver which you're currently already using to serve the webpage with the applet.
All you basically need to do in the Applet is to fire and process HTTP requests. You can do that with java.net.URLConnection (mini tutorial here) or with the more convenienced Apache HttpComponents Client (tutorial here). You can use Applet#getCodeBase() to obtain the context URL where the applet is served from.
URL url = new URL(getCodeBase(), "script.php"); // PHP code
// or
URL url = new URL(getCodeBase(), "servletUrl"); // Servlet code
// or
URL url = new URL(getCodeBase(), "script.asp"); // ASP code
In the server side you just return response in whatever format you like the usual way. A plain vanilla String or a more easy processable JSON or XML format. All mentioned languages provides facilities/libraries to encode/decode the data in JSON/XML formats.
As to sending parameters from Applet to the server side, just pass HTTP request parameters along as a query string in the request URL (HTTP GET) or in request body (HTTP POST). In PHP you can gather them by $_GET and $_POST and in Java Servlet by request.getParameter().
As to returning the data from the server side, in PHP you just use echo to write the response. In Java Servlet you just write to response.getWriter() and in ASP I actually have no idea, but you should got the picture now. In the Applet you should then read and process the response accordingly. See the aforementioned tutorial links how to do that.

Related

HTTP client for JSTL

Currently i'm using JSTL tag to get the URL from third party API. for this i have added the standard-1.0.6.jar in the classpath. I used tomcat server.
I would like to know how this really works technically. Which HTTP client is used by c:import? whether there is separate http client for JSTL built inside standard.jar, or will it use http client used by tomcat server?
If i need to access the internet to get the same third party API using a internet proxy server, which part of HTTP client i need to modify (in standard.jar for any jstl http client or in tomcat httpclient)?
It's just using the standard Java SE URLConnection class. You can use it in plain Java as follows:
URLConnection connection = new URL("http://stackoverflow.com").openConnection();
InputStream input = connection.getInputStream();
// ...
Or by the URL#openStream() shortcut:
InputStream input = new URL("http://stackoverflow.com").openStream();
// ...
The InputStream contains the HTTP response. Just read/write it to an arbitrary OutputStream the usual way.
See also:
Using java.net.URLConnection to fire and handle HTTP requests

Can we read cookies using core-java that has been written using .NET MVC code?

Can we read cookies using core-java, that has been written using .NET MVC code? I have not find any help how to read cookies using core java code?
More Background Details -
Actually we have a java desktop application and we are planning to launch that java desktop application using JWS and that is working absolutely fine.
The issue is -- we ask some user related information from user on web page and launch java desktop application using JWS. Now we would like to have that information provided by user on web page in our java application.
We have write that information into cookies and how can we read that information from java code ?
Yes, you can receive cookies that have been set by another application (as long as the path value in the cookie matches). Cookies are part of the HTTP protocol and it does not matter how thay were defined. The client sends them in future requests depending on the URL path.
To access cookies in Java, have a look at getCookies() in HttpServletRequest.
update
The cookies set by your web-application that launches the Java client will have been set in the context of the browser client. Cookies are added to a HTTP response and cached by the client receiving them.
In the case that you describe you cannot access the same server-session from the Java client without trickery.
The solution I would use is to generate a unique ID in the web-app that is passed as argument to the Java client which can in turn request the values needed from the other session using a fetch of a URL using the generated ID as parameter. (This in essence connects the two HTTP sessions as being part of the same user process.)
For instance you could use a HttpURLConnection and a URL like <web-app>/data?id=<ID> to fetch/download the values as XML from your web application.
Core-java? Then try java.net.*:
A cookie is just a header line with "Set-Cookie: " before the URL content.
http://www.hccp.org/java-net-cookie-how-to.html

Debug tool for creating and sending HTTP messages (e.g. Curl)

I am developing a software which will be communicating with a server using HTTP (but it is not a web browser). Since the server part is not ready yet, I would like to debug my client software by sending HTTP messages to it. I know that I can send HTTP requests using Curl. But I am not sure if that is sufficient here.
I'm imagining an environment where I send a request from my application, check that it is correct using Wireshark and then reply to the request using some software. Using Curl, I think I would have open a listening port..?
I'd use a simple node.js server for this. You can write your own HTTP server in a few lines of code an simulate various return codes, response headers or response entities easily: http://nodejs.org/
PS: There are proxies that simply print out the HTTP messages. This might be helpful for you too, because you don't have to deal with WireShark anymore just for HTTP-level logging.
You can't do that with cURL. It is an http client, not a server.
The simplest way to do this is to actually implement a mock server application that just returns a static (i.e. hardcoded) message every time. You can do this using any server-side language you like (php, python, ruby, ...), or, you can even do it without a server side language, using just static files served by a webserver such as apache or nginx.
For example, if the server part (the API) would respond to /articles.json with something relevant (a JSON object containing some articles), you could put a file named articles.json that contains some hand-written data in your server's root. Then, your application would think it's calling an API when it's actually just downloading a static file.
You can use firebug addon of firefox browser to see content of HTTP request/response.
It does't require any server (but of course, if you dont have server which process requests from browser and send responses to browser, you 'll always see response "unable to connect").
If you still need to mock response, you can create simple server which is able to respond with mock responses, for example java servlet at tomcat server, with code like this:
public class MyMockServlet extends HttpServlet {
..
private String mockHeaders = "...";
private String mockResponse = "my response";
public void service(HttpRequest request, HttpResponse response){
setHeaderAndBodyInResponse(response);//your method
}
}

java socket to read content of webpage

Is it possible to use java socket API to read content of a webpage, ex: "www.yahoo.com"? Can somebody here show an example?
And how about reading content of a page protected by the web app login screen?
Thanks in advance,
dara kok
It's possible but not advisable. The webpage is returned using HTTP, which is more than just a stream of bytes. This means that in order to use a socket you application would need to understand the instructions in the HTTP responses and behave accordingly.
To programitically access a webpage use Jakarta Commons HTTP Client.
With regards to secure webpages, it will depend on how they are secured, however given HTTP Client can maintain cookies you should be able to perform the login through code too.
Further to Nick's answer (i.e. use the Jakarta commons HTTP Client). The login security depends on how the login page is implemented, if it is an apache .htaccess secured site you will need to place username/password information in the request header. Alternatively (and generally more usual), if it is an html form, you will need to deconstruct the form fields from the original HTML and send those as key/value parameters in the http GET/POST request

How to interact with a java servlet running on glassfish with telnet?

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?

Categories