Rake routes for Java web apps? - java

I'm a longtime desktop developer (C/C++) that's been doing web development with Rails since ~2005. I've now been thrust onto a Java web application at work and just do not understand how any developer can grok where all of the URI's are being directed.
I'm working in IntelliJ, and there are six different projects that contribute artifacts to the war. I know that the mappings are defined in web.xml, but it's impossible to tell from there which URI's are directed to which beans or whatever.
As I write this I already have a sinking feeling that there's no answer, but is there some ability to get a straight answer of which routes are exposed by the application and where they point to in source code a la rake routes?
UPDATE
#Dave: It's a mix of Jersey REST and Icefaces. I only vaguely understand what Icefaces is.

Is there an ant script or some other script anywhere that contains details to how the projects are pulled together and built/packaged into the war?
Ultimately everything is constructed into a base directory containing files, directories and a /WEB-INF directory. /WEB-INF is protected and it's contents are only accessible via URI's via the web.xml. Where everything outside of the /WEB-INF in the root directory is accessible via URI's (relative to the root directory) or URL's.
I'm not completely confident in what is all entitled with a rake route, but I believe your sinking feeling is correct - what you see in the web.xml is what you have to work with in java web-apps. The <servlet-class> tag maps the package.diectory.to.JavaClass to a given <servlet-name> and else where in the deployment descriptor, the is a <servlet-mapping> and <url-pattern> that maps the given url to a servlet-name.

Related

Is it possible to deploy multiple war files under the same deployment path?

Let's say I have:
foo.war
bar.war
Is it possible that I deploy them both somehow to the same deployment path? E.g., to access it at:
http://localhost/baz
Are the content of the war files merged somehow? How are file conflicts handled (e.g., let's say both of them has an index.jsp file)?
Thx in advnace!
The servlet specification explicitly forbids this. Deployed web applications may not have identical or overlapping context roots. From the Servlet 3.0 specification, section 10.5:
Since the context path of an application determines the URL namespace of the contents of the Web application, Web containers must reject Web applications defining a context path that could cause potential conflicts in this URL namespace. This may occur, for example, by attempting to deploy a second Web application with the same context path.
Yes & no.
I don't think it's possible to somehow merge them into the same file system path within a servlet container like Tomcat (unless you were to write some kind of complicated, intelligent script to do so). For starters, each .war will have a WEB-INF/web.xml file, and each will rely on the contents of its own file to function -- which would win?
But you conceivably could...
Deploy to 2 different contexts (or containers, or hosts), and employ some kind of load balancer (hardware or software) to route some requests to one, other requests to the other.
Use an "overlay" strategy (such as Maven Overlays) to make a second (and final) .war that is a derivative and extension of another .war file

How to combine Yeoman scaffolding with existing Java directory structure

In my existing web project the directory structure for the served html content while development with jetty is "myProject/src/main/webapp/"
Now, I want to integrate an angularjs project here.
I've played a little bit with Yeoman.
If I'm scaffolding with yeoman, I'm wondering how I can integrate it into our existing dev and deployment structure.
I suppose to use the main folder "myProject" to run yeoman scaffolding would be fine. Then I would get a "myProject/app/" diretory for all my frontend stuff. Should I instruct somehow (how?) my jetty server to use ".../src/main/webapp/" as an alias for the new app directory?
We use jetty mainly as a proxy for requesting the backend. Is there also a way to do a live reload similar to "yeoman server" in combination with jetty?
Take a look at my answer on how to do Django-Yeoman integration.
Architectural concepts will be the same, even external articles (definitely must-reads) are Java-based.
In short:
Use yeoman-maven-plugin. If you are on Gradle that's still ok. Even better, since you will have better control over which grunt tasks are being invoked.
Your project structure should resemble this:
pom.xml
src/
main/
java/
...
resources/
...
webapp/
WEB-INF/
yo/
dist/
<<the rest of the Yeoman-generated stuff>>
Yeoman generators, including the one initialising the frontend part, should be invoked exclusively from yo directory.
The plugin takes care for copying production-ready yo/dist to WEB-INF.
All you have to do is to serve the latter as a static resource.
Config for Spring MVC (dispatcher servlet):
<!--Yeoman static content-->
<mvc:resources location="WEB-INF/yo/" mapping="/**"/>
One should aim for similar config when using other technologies, like Jetty, or pure Servlet config.
The rest, particularly dev setup, is described in referenced answer.

Website works locally, but when deployed on Tomcat server it no longer finds Servlets

Background
I'm working on a project for school. The goal is to create a learning website in the end. I've been creating my website locally and testing it going great so far, so I dediced to place it on the server to test it out there (deploy .war file). And as soon as I did that the whole website seems to not work.
Problem
The problem is that as soon as I try to click on any of my links that go through servlets, I get an error from tomcat saying that the requested resource is not available (http status 404). Which is sort of weird because it does work locally.
What have I tried
The first thing I noticed was that going to the server .war file is going to a snapshot url first (in my case: http://145.92.6.85:8080/PDL-1.0-SNAPSHOT/) instead of just landing on the index.jsp page right away (which happens locally) so my first thought was that it was using a whole different way to access the files I was looking for. Again strange because locally everything is working fine (using tomcat as well). I looked inside my project and tried to access the files I was looking for using the map structure, and this seemed to work. So instead of using the servlet I specified on the web.xml it's just ignoring it.
So then I decided it probaply can't find my web.xml file. I did a little searching online and found that it should be placed inside the map WEB-INF. Currently web.xml was sitting in META-INF, so placed it in WEB-INF, but to no avail. The pages I'm looking for still can't be found. I also read that some people said the classes for the projects (which I assume to be my servlets?) need to be placed inside a classes directory inside the WEB-INF folder. I decided not to do this, because currently I've got them all set up in different packages (Source Packages -> controllers/models etc.). And that seemed a lot more logical to me, and besides it's working locally so why would this not work on the server?
As far as I can tell my web.xml is set up correctly, but I'll show some code to indicate what the problem is.
Code
This is the URL I'm trying to follow (inside a navigation bar) (follows: http://145.92.6.85:8080/PDL/inlog)
<li>
Log In
</li>
this is the web.xml
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>controllers.login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
The inlog.java file (inside Source Packages->controllers or PDL/src/main/java/controllers)
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher rd = request.getRequestDispatcher("/pages/inlog.jsp");
rd.forward(request, response);
}
This is where it breaks because it could not be found.
Finally
Whilst writing this question I do feel as if my URL's and stuff are maybe a little bit too changing and perhaps should be more dynamic than the current static ones (since the server enviroment is different I guess). Could anyone give me some tips as how to make the urls more dynamic, so they would work locally and on the server straight away (something like the PHP equivalent of $_SERVER['SERVER_NAME'] and the like? Any help would be greatly appreciated! Though I feel that the urls im redirecting to are the problem I just don't know how to solve this.
What is the name of the war file? Is it PDL-1.0-SNAPSHOT.war? Did you try accessing
"www.ip or domain name/PDL-1.0-SNAPSHOT/"?
If your war file has the version name, then you will have to edit the Tomcat configuration file to map the war file name, to the context root.
Please see http://tomcat.apache.org/tomcat-6.0-doc/config/context.html for more information, if you are using Tomcat 6.
OK, so you're messing the structure of the webapplication.
I do not know how you deploy it to the local server, but if you pack it as a war, you should follow simple rules. example of a valid structure

functionality of tomcat servlet container

ServletContainer reads the user defined servlet class name from web.xml file by converting web.xml file into DOM object. I don't understand how servlet container converts this and where this DOM object (web.xml data) resides inside web-app directory of server?
The Java EE specification mandates a specific directory and packaging structure (war) for web applications so that the web app can be deployed on any servlet container (Tomcat is one of them) without any modifications. Now, each servlet container can unpack it in which ever way it wants and as a developer you no need to worry about it.
Now, Tomcat places all the deployed applications in the \tomact-install-dir\webapps directory. Each web app will be in its own folder with webapp name as thefolder name.
Perhaps this is the first place to take a look at when deploying the first web application. tomcat deployment hierarchy.
web.xml should be placed inside the WEB-INF on your webapp deployment. Some servlet containers allow you to have generic or reusable web.xml files inside their configuration directories but that's not standard AFAIK (It's better for your webapp to be selfcontained most of times)
You shouldn't need to know about the parsing of the XML file since it's all handled by the servlet container under the hood. (Just place it in the right place)

context.xml vs web.xml in web application

I am developing a small web application application. The objective is to create one welcome index.html page with Ajax + one servlet to handle ajax requests.
Although I thought I would be fine with a web.xml only, I don't want to deploy to /, but to /MyApp. NetBeans's project properties offers options to set a context path, which helps me deploying to /MyApp. However, it automatically adds a /META-INF/context.xml file, which is a bit confusing.
My questions are:
1) Do I really need a context.xml file do deploy to /MyApp instead of /?
2) If answer to 1) is no, how to accomplish the same with web.xml only?
3) What is exactly context.xml to web.xml?
/META-INF/context.xml is a Tomcat-specific config file. It's used to configure how your app is deployed to Tomcat, including, among other things, the context path at which it exists. Other containers have similar files that can be included in a WAR for container configuration. To answer your questions:
No. The embedded context.xml is only one way to set the context path, and as I indicated, it'll only work in Tomcat. In Tomcat, default behavior is to deploy webapps to a context that has the name of the war file, without the ".war" extension.
You can't set a context path in web.xml. That's your application's deployment descriptor. It configures your application, and the context path is external to your app. It belongs to the server/container you're deploying the app to. Configuring a context path is always done in the container's configuration.
If by "config.xml", you meant "context.xml", then I think I've already answered that. If not, clarify your question.

Categories