Imagine the following situation:
A webapplication gets deployed to a tomcat server. A jersey servlet is started that serves requests at http://localhost/myServlet
Now, when somebody is requesting
http://localhost/myServlet/this/path/shall/be/handled, the myServlet should feel responsible for this request and handle it appropiately.
Edit: To be more specific: I do NOT know the path the user is requesting. Think about this like a virtual file system where the user requests myServlet/path/to/file. MyServlet shall be responsible for this GET request. As you can see mapping those URLs to Annotations is not possible. I'd like to annotate like myServlet/*, if that is more understandable.
Could anybody point me to the right direction? I feel a bit lost, but I am quite sure this is possible!
Jersey Servlet (com.sun.jersey.spi.spring.container.servlet.SpringServlet) is the end point for REST API (if we are using Jersey REST) Call. So, When ever the servlet get any request , The same request is been processed by its Handler.
When the application get a request with myServlet with the appropriate url-pattern then Its corresponding Handler will activate and process the request for the appropriate response.
Related
yesterday i started brainstorm for a project of mine and I'm not sure if its the correct approach.
On my website I'm having an (kind of an order form) which sends a post to a target URL, which works with a simple curl php script. The target is an external service (where I have no access no rights, nothing). I only know that I will get a POST with further processed data back from the service, which I have to save into my DB.
in steps:
Users fills out the (order) form and posts data to an external url on my website.
data gets externally handled and after finishing that resents a post.
read incoming post data.
save data into DB.
Success page on my website.
My thoughts were to handle the incoming data with a servlet (spring maven project) but I'm not sure if this is a correct approach. Is there a better why to do this. Or is the first step with the php scripts wrong. thx for any help.
A simplest workflow could be
1. Forward the initial (Order form with values) request to a servlet
2. Invoke a post request using java to an external url inside this servlet (Using Apache http client, or libraries such as HTMLUnit)
3. Once you get the incoming response in your servlet, you can update your database.
If you are using spring, the controller could forward initial request to a business class which will handle this post processing and delegate the database update to respective DAO.
There are a number of suitable ways to handle this, and the decision is largely a matter of preference and what you're familiar with. Spring can handle this sort of job quite well.
Note: Maven is a build system for Java and some other JVM languages. I recommend using it, but it's not part of Spring; what you're probably looking for is Spring MVC.
What is the difference between request dispatcher's forward method and the concept of servlet chaining?
Example
RequestDispatcher rd= req.getRequestDispatcher("pathToServlet");
rd.forward(req,resp);
What this does is forwards the request without involving the client(browser) interaction. But can we achieve the same using Servlet Chaining?. If we can then what is the difference?.
It's not different. "Servlet chaining" is just a term coined in dark J2EE 1.1/1.2 ages when servlet filters didn't exist. Indeed, it's basically the approach of using RequestDispatcher#forward() to forward from one to other servlet (and ensuring that the response isn't already committed as that would otherwise result in IllegalStateException).
Since J2EE 1.3 (Servlet 2.3, over a decade ago already!) servlet filters were introduced which made the process so much more clean and easy. Since then, "Servlet chaining" is frowned upon and usually marked as "bad design". These days, you'd ultimately like to end up with only one front controller servlet and several business models.
I think the concept of 'chaining', as it relates to configuring the server instead of using the forward() method, is that you can configure certain types of requests to activate a particular chain of servlets.
For instance, if a request is from within an Intranet you might want the users to see some internal advertising. So you could have all these requests go through as AddInternalBanner servlet first.
The forward() method is useful if a particular servlet decides it should pass the request along.
I need to call a servlets POST method from another servlet and pass a blob in the servlets parameters. Is this posible, if so how can it be done. PS: I cant use Apache HttpClient
You need to create and send a HTTP request yourself. You cannot make use of forward/redirect/include because you want to change the method from GET to POST and you want to send a multipart/form-data request.
As HttpClient (and other 3rd party library?) is apparently not an option, your best bet is to use the standard Java SE API provided java.net.URLConnection. Long story short: Using java.net.URLConnection to fire and handle HTTP requests At the bottom you can find a multipart/form-data example.
Please note that this problem is not specific to servlets. In other words, you must be able to execute this code in a plain vanilla Java application with a main() method. This allows for easier testing and finetuning. Once you get it to work, just let the servlet execute the same piece of code.
Unrelated to the problem, I have the impression that there's a major design failure somewhere, certainly if the both servlets runs in the same webapplication context. The other servlet where you want to send the POST request to is apparently too tight coupled and should be refactored.
You can get a dispatcher to another servlet in your application and forward it or include it as #Ryan suggests. The code should be something like this inside your first servlet:
ServletContext context = this.getServletContext();
RequestDispatcher dispatcher = context.getRequestDispatcher("/otherurltoservlet");
// change your request and response accordingly
dispatcher.forward(request, response);
Do you mean call from your application to another web service? If so, then something like HttpClient is what you want. If you mean you want to programmatically invoke another servlet in your app, then you're looking to either forward to it or include it.
I want to excute a servlet to get server name,server port and ContextPath from request.
But I don't want to invoke servlet by user interacting. I want excute this servlet by Java code.
I'm not sure it's possible.
Please give me recommendation.
Why not just simply make use of URL to request the servlet.
For more sophisticated use try HttpClient.
If you mean you just want to execute some code to extract information from the request, why are you trying to invoke a servlet? Just write a method somewhere, and call it.
You could also use a Servlet Filter to extract this info on every request, add the results to the HttpServletRequest object as an attribute, so that any other servlets that process the request can find it and use it.
If you mean you want to make the servlet load at startup, you can add this within the web.xml configuration:
<servlet>
...
<load-on-startup>1</load-on-startup>
</servlet>
If your servlet is doing something like calling a service every x minutes without a user interaction then you probably want to do something different.
You can use a ServletContextListener to listen to when the web context is started and to then start whatever code you want.
http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletContextListener.html
Thanks for your answers
Actually, my problem is : I have an web application and a WebService (on diferent server).
I want web application invoke webservice (after specific duration) to send data (domain, port,context path..) to webservice so that I can know where my web application is deployed.
To solve this problem I have created a servlet to call WebService and by using this servlet I have HttpServletRequest object and I can get domain, port, context path.Then I can send data to webservice.
But I don't know how to execute this servlet(after specific duration) without user interaction
I am wondering how I can use google protocol buffers to accept a request and send a response back to a client? I am thinking about writing a servlet which will take a request.
Is the following trail of thought the correct way to implement this:
1. Have a .proto file which is the message definition for the incoming request.
2. Write a servlet which accepts this request, does various tasks like querying database
and then sends a response. Will this response require a separate .proto message definition with all the fields that make up the response?
3. Will the client just invoke the doGet() method of my servlet and pass the request, it should then return a response as a protobuff object?
Any suggestion or idea will be very much appreciated.
Typically you'd want a request message and a response message, yes. You'd also probably want a method name to describe the action - that's certainly how the built-in PB services work.
The client wouldn't invoke doGet() - it would make a request (probably a POST rather than a GET) and your servlet would receive it.
Now, ideally you could have a general "ProtocolBufferServlet" which could service the requests by handing them off to services implementing the appropriate interfaces.
I suggest you look at the documentation for Protocol Buffer services and the Java services generated code for more information. You could implement an RpcChannel which worked over servlets, or get the client to make the HTTP post directly. You'd probably use dependency injection of some kind at the server side to tell the servlet what was implementing the service.
HI,
I have this up and running. I ended up posting a http request as a post to my servlet. I was able to take the request protocol buffer, read the request, do some processing and then send back a response. It was actually really simple once I got it working. We used the 1 .proto file to define the request and response message structure.