sockets in servlets - java

I have a java client that sends a message to the server .The server is a servlet deployed on jboss. My question is can the servlet send data back to the client as acknowledgment? is it possible in servlets?

From Java EE 5 tutorial chapter 4:
What Is a Servlet?
A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers. For such applications, Java Servlet technology defines HTTP-specific servlet classes.

Yes it is possible with servlets. Read the basics here.

Of course it's possible; a servlet that didn't have the ability to send data back to the client wouldn't be very useful.
There are many ways to do so but the most "raw" way is by writing to ServletResponse.getOutputStream(). The ServletResponse is passed to the service method of a Servlet implementation. Note that the data is of course going to be sent back as a valid HTTP response (identified using MIME, etc).

Ashwinm if you are looking for posting a result to a socket (on client) and not returing the result through mormal HTTP response.
Which I believe is not a servlet question then. You can anyway do anything in java. But have to look at normal "how to write to a socket" tutorial.
GenreicServlet might provide some extensibility or some solution.
EDIT
I read your commentes below later. Yes you need URLConnection in that case at the bare minimum.

An http servlet works on http request and can send back the http response. You can connect to it using URLConnection as described above.
If you don't want to get at this level (layer), you can work at with the packets directly (using sockets). Here you dont need a servlet actually. Here your server (a simple java application) will listen on a socket for any incoming connection and the client can connect to it.

Related

Prevent Http access to api

Is it possible to prevent requests that are not using https from calling a get/post in code.
For example I am implementing a rest api with java and spring, and I would like to return a http status to the caller if they were not using https.
/api/getBlah using https would return the item, however using just http would not work and return the appropriate http status.
For an API server, I strongly encourage you to don't answer a redirect
If you do answer a redirect, if a developer make the mistake to use the http version of your API he will not see immediately that he used an insecure connexion
If you answer a "400 Bad Request", "401 Unauthorized" or a "405 Method Not Allowed" with a body explaining that only https is allowed, then the developer will immediately see the problem.
In short, answering a redirect http=>https in an API encourage bad practices.
There are a few different ways of handling this. Web Server Handling in my opinion, is the most common and makes the best use of the strengths of web servers and application serves.
Web Server Handling
As mentioned in the comments above, a common method of deploying Java web applications is to place a web server (such as apache or nginx) in front of a Java Application Server (tomcat, wildfly, jetty, etc.). The web server then proxies requests to the application server.
In a setup like this you can simply return a static response or redirect the user.. In this way, you don't need to return anything special from your own Java application. The web server does the lifting for you.
It should be noted (as in the answer from #Tom), that in the API use-case, an HTTP to HTTPS redirect is not best approach. This is more commonly
Application Server Separate Servlets
On the Java side, you could essentially create two separate web applications. The first would be configured to receive requests from the HTTP listener and would provide proper error messages or redirects. The other would be your main application and would be configured to only receive requests from the HTTPS listener.
Again responding with a static error would be best for an API.
Application Server Single Application
When a servlet handles requests, it will populate information in the ServletRequest, including information about if the requests was secure (used HTTPS). See the [ServletRequest documentation][2] and specifically the isSecure() method. You can inspect the ServletReuest within your application to determine the appropriate way of responding to the request.
Don't Answer
You can also simply configure your webserver/app server to not listen for HTTP requests. You'll miss the change to provide a sensible error to the user, but you'll achieve the affect of not allowing any requests over HTTP.

Java application server without HTTP

I have a client software that is written in C++/C# and a database. Now I don't want the client to access the database directly, so I thought about placing an application server in the middle. This one should get a short request from the client, ask the database for new data, do some filtering (that can't be done in sql) and then return the data to the client.
My search for this kind of software brought me to Glassfish or Tomcat but my problem in understanding is, that these always want to talk http with html/jsp. Because most of my data is encrypted anyways, I don't need such plain text protocols and would be totally happy with something that just takes a byte stream.
On the other hand would it be nice to have a server handle the thread pool for me (don't want to implement all that from scratch).
After more than a day of searching / testing I'm even more confused than at the beginning (ejb, beans, servlet, websocket, ... so many things to google before understanding just the simplest tutorials).
TL;DR: how do I get Tomcat/Glassfish to just open a socket and create a new thread for every request, without any HTML/CSS/JSP involved?
Jetty and Tomcat are so called servlet container and thus primarly targeted at HTTP exchanges. Glassfish is an application server that uses a servlet container as one of its modules. I would stop thinking in that direction - that's all more like web applications and web services - some levels too high what you are asking for.
I think you should more look into sth. like Netty which is merley a "high performance protocol" server. Take a look at the documentation here (even some sort of tutorial there which might fit your use case).
GlassFish is an "enterprise application server", targeting the Java EJB specification. That's surely overdone for your purpose. You can give Tomcat a try. It is a "servlet container", targeting Java Servlet specification. Servlets have one purpose: listening to an incoming URL (request), executing Java code and returning a response, usually over HTTP.
For sure, you may start your own (plain) ServerSocket, for example using a ServletContextListener (which will be started once your application starts). But you should go for a higher protocol to send the data, like Hessian and Burlap, which is implemented in both, Java and C++ and easy to set up.

Java App to receive http PUT and Get request

I am looking for advice on how to go about writing a small and simple application that will receive http GET and http PUT request, process the data (simple text files) and respond.
I have all ready done this using threads and sockets but there must be a simpler and more efficient way. Also when I run my application using wireshark I am not convince I am using the http protocol as I should be.
Thanks
Alexis
You can use Tiny Java Web Server. (http://tjws.sourceforge.net/)
Alternatively, if you are using Java 6 or later, you can use the Http server API.
I used HttpComponents for similar purposes - it provides functionality for HTTP server and client parts implementation. It's easy to learn and use.
You can consider embedding a webserver like Jetty (start/stop it from java app) if you want to get the full benefits of HTTP parsing.

Java: Read POST data from a socket on an HTTP server

I have a website (python/django) that needs to use a load of Java resources that may or may not be on the same server. Therefore I am writing a mini webserver in Java that will receive a request and then when processing is finished, POST some data back to a url on the site.
I have got the java code receiving connections on sockets and responding with some simple HTML.
My problem is that I will POST data to the Java server and that code needs to act on the data. How do I go about reading the data that is posted in the HTML request, if it is even possible. If not, is there any other way you would do this.
If you think I am going about this in completely the wrong way then please tell me and I will consider another method, but after conversing with some Java developers, this seemed like the best way for what I was doing.
Thanks
You probably don't need to write a http server yourself, just use some lightweight java web server/servlet container like jetty or simple
and looks here if you still want to know how to parse a http POST request http://www.jmarshall.com/easy/http/#postmethod
In your case I probably wouldn't work with low level socket connections.
I recommend you to use a servlet container with some sort of webservice or maybe only a simple servlet depending on your needs. With a servlet you can easily access the POST data, process it and return something to the caller.
If you don't want to use a stand alone servlet container like Tomcat you can try an embeddable servlet container like Jetty or winstone servlet container.
If you need something more sophisticated you can use some webservice technology like JAX-RS or JAX-WS. This allows you to provide a fully featured API for your Java resources in an easy way.

Sending a HTTP post to a web server with Java

As per title really I'm wanting to send a custom HTTP post request to a web server and I have little experience in this area. The web server uses an LDAP server for access control (not sure if that's important) for which of course I know the username and password. Could anyone flesh out some code to do this or at least get me started?
Edit for one of the comments, the server is running a LAMP stack with PhP 5+ and Apache 2+
You can use HttpClient module from Apache.
Although the java.net package provides
basic functionality for accessing
resources via HTTP, it doesn't provide
the full flexibility or functionality
needed by many applications.
HttpClient seeks to fill this void by
providing an efficient, up-to-date,
and feature-rich package implementing
the client side of the most recent
HTTP standards and recommendations.
Designed for extension while providing
robust support for the base HTTP
protocol, HttpClient may be of
interest to anyone building HTTP-aware
client applications such as web
browsers, web service clients, or
systems that leverage or extend the
HTTP protocol for distributed
communication.
LDAP and authentication are separate issues from sending POSTs to web servers.
Th server side needs to do that authentication. Set it up either in your code or in the web server itself.
If your client is a Java application, you can create a POST using UrlConnection.
If your client is an HTML page or JSP, you need a form with a POST action.

Categories

Resources