I have seen lot of examples for Servlet3.0, but I couldn't find a single answer with Servlet2.5. But I'm sure there should be a way to do it.
Can anyone tell me how to add a servlet programmatically with tomcat 6.
Thanks.
Of course you can write a servlet that takes all requests you might react on (maybe bound to *.do or the like) and that handles those registrations. But there is no support by the servlet standard and I don't think Tomcat 6 supports programmatic configuration out of the box. However, there might be a framework that does, but I'm not aware of.
Related
I have an application currently running based on servlets (app server glassfish). External applications make an HTTP call to that servlet and get response once successfully achieved what its designed for.
I need now to shift the whole application to JSF2.0 as it needs a front end and sticking to servlet/jsp is not an option.
My questions are:
What's the recommended way to achieve the scenario of external applications getting things done through http calls in my JSF2.0 web app (that is currently I am achieving with servlets)
I need to maintain some counters in Application scope; Would you advise me to keep using servlets in my jsf app too and are the application scoped beans callable in servlets?
The way how you described the old application sounds much like a web service.
JSF is a component based MVC framework, not a web service framework. So JSF is basically the wrong tool for the job. If you want to continue with the standard Java EE stack, look at JAX-WS (SOAP) or JAX-RS (RESTful) instead. The latter is "the" standard web service framework these days.
See also:
Servlet vs RESTful
What's the recommended way to achieve the scenario of external applications getting things done through http calls in my JSF2.0 web app (that is currently I am achieving with servlets)
Like BalusC said, JSF is not exactly what you need and is not designed for what you intend to use it for, webservices are. That being said, you could achieve your goals using JSF if developing an effective web service is not in the cards for you ,either because of a lack of technical expertise on your team or the time factor or other technical constraints require you to serve your customers via a webapp (I honestly can't think of any but hey, you never know)
That being said, I'm assuming you already know your way around JSF2: take a look at this answer to a question somewhat similar to yours. Ultimately, since you're in the Java EE 5-6 stack, I'd strongly suggest you begin work on developing webservices (for basic requirements,they're fairly straightforward to develop). You can download a tutorial here or use oracle's tutorial
I need to maintain some counters in Application scope; Would you advise me to keep using servlets in my jsf app too and are the application scoped beans callable in servlets?
Unless you give us your specific use case, there are very few and far between reasons maintain a servlet within a JSF app. Yes, application,session and request scoped beans are accessible from within a servlet. They're simply stored as objects within the scopes they're named for, application scoped beans within the applicationScope, session scoped beans within the session and request scoped beans within the request.
You may use your JSF webapp to serve low level http requests, but it's a square peg in a round hole chief.
Just about to start a java EE project.(Biz requirement is under change still.)
For the web tier, we looked at various java web frameworks and eliminated component-based ones such as JSF, Wicket. Now it comes to spring mvc 3 or struts 2.
Googled it and found little useful info. Can anyone talk about their pros and cons? Thanks.
I took over a Struts 2 + Guice web app that used the REST plugin to do convention over configuration. It was very easy to work on at first but I ran into a couple of hurdles that were either difficult or impossible to overcome.
One of these was that I needed to have internal dot/period characters in the path of the URI and Struts 2 + REST did not allow this, as it would interpret the dot to indicate a file extension and try to marshall to the appropriate view (e.g. like catching .xml and .json).
So I ended up rewriting the webapp in Spring 3 and was able to fix all those issues I couldn't handle in Struts 2. I've been much happier with Spring 3 and found it just as fast to code in as Struts 2. I've stuck with annotation based configuration as much as possible and tried to use the JSR versions where ever possible (330 #Inject and 303 #Valid, etc), so that if I decide to get rid of Spring I am not stuck with custom Spring annotations.
My vote is Spring 3.
In this other stackoverflow question you have a lot of answers comparing struts and Spring. Though many of them don't mention explicitly the version 3 of Spring the comparison would be similar to the version 2.5.
As many of them say, I'd prefer Spring. It makes things easier when you use annotations. One fact I don't like in Struts 2 in comparison with Spring-mvc is that you have to add getters and setters for every property you want to get in the actions. I think Spring is cleaner in this way.
#Javi
You have various options in Struts2 to avoid these getter and setter one of them id ModelDriven interceptor
Well my vote is for Struts2 since i am working on it from so long but this does not mean Spring MVC is bad i have worked on it also and its equally good..
Choice is all yours and it depends what word you like most Spring or Struts
How can you run a servlet on a server without putting it in the web.xml. I want to start a server from main and pass what servlet it should run
Thank you
That depends entirely on the server you're using. If it's an embedded server like Jetty, then you can easily do that in flavor of a ServletHolder.
context.addServlet(new ServletHolder(new HelloServlet()),"/*");
If it's other, then you need to consult the documentation of the server in question. It's usually not possible in non-embeddable servers like Tomcat, Glassfish and so on.
See also:
Embedding Jetty tutorial - ServletContext.
Update: as per the comments on your question, you're after all looking for a solution in the wrong direction. Reread the MVC pattern and more specifically the front controller pattern. You shouldn't use servlets as domain objects, but just simple constructable Java classes which don't extend HttpServlet. Finally you just end up with a single servlet which constructs/picks the right domain object based on the current request. You can find some insights and a basic kickoff example in this answer.
How can I consume a webservice that hasn't explicitely created a crossdomain.xml?
I understand it's for security and to prevent cross-site scripting, but it does seem like a major limitation to the Flex framework.
For example, if I want to consume a webservice, which is suppose to be language agnostic, then I can't with Flex. The webservice/server has to be specifically prepared for Flex/Flash. If it's not, then it cannot be consumed.
That can't be right can it?
Use a proxy server like Apache or BlazeDS to forward requests from the host the SWF originated from to the host with the web services.
If your webservice is on another domain, you'll need a crossdomain file. Pure and simple.
I have found two ways around this:
Use an HTTPService
Use external interface calls to Javascript embedded on your flash file's container page, which will then call the web service
Option 2 is more complex, but I prefer it. This link should help you out with the ExternalInterface class: http://blog.flexexamples.com/2008/03/11/returning-values-from-javascript-in-your-flex-applications-using-the-externalinterface-api/#more-555
What I did was "put a middle man". So basically, I put a php script that reads from the other side what I need. Yes, unfortunately, that requires a "server". You could try a JavaScript as well.
What's the best way to implement friendly URL in ASP.net?
The ASP.NET Routing Framework provided in .NET 3.5 SP1 is a good one.
Although it is very new, it can handles many URL-related tasks and most frequently used URL-friendly schemes very well.
It can be used outside of MVC, too.
The best way to do this is to look into the new MVC toolkit from microsoft (http://www.asp.net/mvc/)
See http://weblogs.asp.net/scottgu/archive/2007/11/13/asp-net-mvc-framework-part-1.aspx for an example.
Sure, its only beta right now, but the core of it is the routing system that makes it possible to make intelligent urls based on actual content.
I.e. to edit product with id 5 you'd have an url that looked like
/Product/Edit/5
If I read the specifications correctly, you can use this routing system for anything (i.e. so you don't have to recode the entire site to use it), and it can default to allow direct references to an existing file to have precedence over its own rules (i.e. /myfile.aspx will still use the file, instead of a route). That way you can mix and match the technologies and urls while you make the trancendance to the new routing based system.
I have used UrlRewriter.Net library. It is small but powerful and easy to configure.
If you're looking to do this in earlier versions of the .Net Framework, you can use the RemapURL tool from the IIS 6.0 Resource Kit. We use it, and all it takes is installing the dll, creating a very simple ini file with your friendly urls and their associated endpoints, and enabling the dll in Web Service Extensions Very simple how-to here.