URL mapping of servlets and JSP files with Tomcat and Eclipse - eclipse

I have a web application project in Eclipse that is configured to deploy to a local Tomcat server. Let's call the web application Blah. Here are two questions -- I must be missing something very simple, but I can't find an easy way to change these settings within Eclipse without fiddling with the Tomcat configuration files.
1.
When the application is deployed, the URL I can use to browse to some servlet/JSP is localhost:port/Blah/servlet. I would like to get rid of the Blah prefix.
2.
I would like to set up redirects for some JSP files to "hide" the .jsp extension. For example, I'd like localhost:port/login to be served by localhost:port/login.jsp, preferably without the browser seeing a 30x redirect status code.
3.
I would like to set the default URL, i.e. localhost:port/, to redirect to a particular JSP or servlet (again, preferably without issuing a redirect status code).
Any help, including links to relevant resources, would be greatly appreciated. Please note that I am looking for a way to configure these things from within Eclipse, if possible. (If not possible, I would like to do the minimal amount of changes to the scary Tomcat XML files.)

This means that you need to deploy your application as the root application. It's easily done by naming your war file ROOT.WAR (or your exploded war directory ROOT), or by defining a context for your web app with an empty string as the path attribute.
Then you don't want a redirect, but a forward. Or you simply want to map the JSP (which IS a servlet) to a given path. Define a servlet and a servlet mapping in the web.xml file, as you would for a servlet class, but use <jsp-file> instead of <servlet-class>.
This is done using the <welcome-file-list> element in the web.xml.

Related

Where do I place my templates for Blog-related mail notifications in Liferay 7.0?

The documentation states that I can configure the liferay server to use my own templates for the email messages. Specifically, if I add these properties to a portal-ext-env.properties in $CATALINA_BASE/conf/liferay:
blogs.email.entry.added.enabled=true
blogs.email.entry.added.subject=${resource:com/liferay/portlet/blogs/dependencies/email_entry_added_subject.tmpl}
blogs.email.entry.added.body=${resource:com/liferay/portlet/blogs/dependencies/email_entry_added_body.tmpl}
Liferay will supposedly use the templates in the specified paths (com/liferay/portlet/blogs/dependencies/email_entry_added_subject.tmpl and com/liferay/portlet/blogs/dependencies/email_entry_added_body.tmpl). The thing is, it's not very clear what these paths are relative to. Are these files relative to $CATALINA_BASE? For example, would the above configuration result in Liferay looking up $CATALINA_BASE/com/liferay/portlet/blogs/dependencies/email_entry_added_body.tmpl for creating the body of an email message? If this is not the case, where does Liferay lookup templates for Blog-related email messages?
After some digging, I've found that you place the templates in the $CATALINA_BASE/webapps/ROOT/WEB-INF/classes folder. Paths that you reference in the properties (e.g. blogs.email.entry.added.body=${resource:com/liferay/portlet/blogs/dependencies/email_entry_added_body.tmpl}) are relative to the aforementioned classes folder.
So, if I wanted Liferay to use a template file in the ff. relative path: org/foo/my_email_entry_added_body.tmpl, I would do two things:
Place the file in $CATALINA_BASE/webapps/ROOT/WEB-INF/classes/org/foo/my_email_entry_added_body.tmpl.
Add the following line to $CATALINA_BASE/portal-ext-env.properties: blogs.email.entry.added.body=${resource:org/foo/my_email_entry_added_body.tmpl}.
I consulted my co-worker and got a better understanding of why this is. The architecture of a Liferay application is such that it comes bundled with a Tomcat server. According to the documentation, WEB-INF/classes is a directory that a web app deployed to a Tomcat server looks up for classes and resources:
A class loader is created for each web application that is deployed in a single Tomcat instance. All unpacked classes and resources in the /WEB-INF/classes directory of your web application, plus classes and resources in JAR files under the /WEB-INF/lib directory of your web application, are made visible to this web application, but not to other ones.
Specifically, this folder is high in priorty in the web app's classpath.
When you see Liferay code similar to ${resource:path/to/foo}, it's looking up resources in its classpath. One of the paths in that classpath is WEB-INF/classes. Hence, if path/to/foo is placed in WEB-INF/classes, Liferay will find path/to/foo there.

Make per-context JNDI variable available to Tomcat in Eclipse

I'm using Tomcat 8.5.6 inside Eclipse 4.6.1. I have my web-app project/context foo, which has a JAX-RS (using RESTEasy 3.1.0.CR3) endpoint of bar, so I can fire up Tomcat inside Eclipse and access:
http://localhost:8080/foo/bar
I have a variable named foobar which I want to access inside my JAX-RS implementation using JNDI:
final String foobar = (String) new InitialContext().lookup("java:comp/env/foobar");
I plan on deploying the produced WAR in production using Tomcat autodeploy. I want to configure the foobar variable for Tomcat externally to the WAR. How can I do that so that I can test it in Eclipse?
After a lot of reading, I found what I thought to be the $CATALINA_HOME of Eclipse: …\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\. So I created a context file for foo at …\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\conf\Catalina\localhost\foo.xml to correspond to my project/context, and put the following inside it:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Environment name="foobar" type="java.lang.String" value="123"/>
</Context>
Yes, I know that Eclipse erases this directory whenever I rebuild. But after building, I saved to file at least want to see if it works. It doesn't. I get an error:
javax.naming.NameNotFoundException: Name [foobar] is not bound in this Context. Unable to find [foobar].
I want to at least get it working so I can know how to do this in production, and worry later about the context file deletion thing in Eclipse. So what did I do wrong? Why can't Tomcat in Eclipse find this JNDI variable?
Note: I am not using a web.xml file and have no desire to do so; besides, this variable should be defined outside the WAR in the production deployment.
Update: The good news is that (on Windows 10 Professional Anniversary Edition 64-bit) using the same Tomcat but in standalone mode, I put the same foobar.xml file inside the standalone Tomcat's conf\Catalina\localhost\foo.xml, and my JAX-RS application picked it up just fine. So how can I define a JNDI variable in Tomcat inside Eclipse for testing?
It appears that in order to get Eclipse+Tomcat to recognize the per-module context files, you have to go into the server configuration (double-click on the server) and turn on the Publish module contexts to separate XML files. This way Tomcat will use the specific context XML file you created. Otherwise it apparently puts them in conf/server.xml and ignores the context-specific file you created.
There is still the problem that Eclipse regenerates this file each time you do a rebuild, destroying whatever JNDI variables you placed there. I'm trying to get the workaround in https://stackoverflow.com/a/22380248/421049 to work, but not yet succeeding. Anyone have any better ideas?
At least I'm able to reproduce a production environment now --- albeit temporarily, until the next rebuild.
Your link to Markus' answer on https://stackoverflow.com/a/22380248/1794485 allowed me to get this working, or at least as described in his workaround. But the remaining problem to solve was ordering.
As he said, you can workaround this by having a local copy of the META-INF/context.xml somewhere else, and adding this folder to the Deployment Assembly in the project properties of the Eclipse project.
This didn't pick up for me initially though. It looks like that while the Deployment Assembly in the properties shows as sorted by name, in fact it has an order like any other path. When I then removed the src/main/webapp entry (so the one containing the normal META-INF/context.xml) and added it back in, this effectively moved it down the pecking order. The next Tomcat deploy and startup in Eclipse finally put my preferred copy of META-INF/context.xml in .metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\myapp\META-INF
If in doubt about the true sequence of that Deployment Assembly path, have a look under your Eclipse project on the file system - at .settings\org.eclipse.wst.common.component.

Deploy GWT to Tomcat (servlet not running)

What is the correct way to deploy a GWT app to Tomcat? I have made a GWT app with server side code (servlets). It works in Hosted mode but only when I copy the WAR folder (after compiling) to Tomcat webapp directory and rename the war folder correctly.
My GWT app servlet is in URI /mygwtapp, so I renamed the folder mygwtapp. The app loads correctly with the problem that servlet do not run i.e. /mygwtapp/servlet does not run.
All the libraries needed by the server side code are in the WEB-INF/lib folder. What could be the reason for this?
Thanks.
By default, Tomcat serves an app named 'mygwtapp' from the context path '/mygwtapp'. (Whereas the GWT built-in jetty serves it from the context path '/'.)
Your servlet paths will usually be '/mygwtapp/*'. This means, that in conjunction with the context path, your servlets are now accessible from '/mygwtapp/mygwtapp/*'. (Try it: Simply enter the full URL in your browser - the Servlet will usually complain that something is missing, or that it doesn't support GET, but you'll know for sure now, where it lives.)
So you have two options:
Tell the client side to call the servlets at '/mygwtapp/mygwtapp/*' (I think, this is already taken care of automatically when using the #RemoteServiceRelativePath annotation)
Adjust the Context path of the web application in Tomcat, as explained in http://tomcat.apache.org/tomcat-6.0-doc/config/context.html
One other option is to use GWT.getModuleBaseURL() then in your client side code append servlet name. This will make it work on jetty or Tomcat without any special config.

Java Dynamic Web Project with Eclipse

Currently to access my dynamic web project (running in a tomcat servlet container) I access the following url:
http://localhost:8080/[Eclipse_Project_Name]
I have a couple questions about this:
Where is the configuration that
is forcing the url to require the
Eclipse project name? I don't see
this in the web.xml.
Say I'd like
to change the url used to access my
project. Maybe I want it to by at
the root: localhost:8080/, or maybe
a different directory structure
altogether. How do I do this?
Thanks
Where is the configuration that is forcing the URL to require the Eclipse project name? I don't see this in the web.xml.
When you create a New Dynamic Web Project, the first page of the wizard asks you for a Project name and in the third page, you can change the web module settings such as the Context root which defaults to the project name (the context root is the part of the URL you're talking about).
This information is not stored in the web.xml, it is stored in the .settings directory of the project (to be precise, in org.eclipse.wst.common.component) and will be added later to /Servers/Tomcat v6.0 Server at localhost-config/server.xml in a <Context> element when you will Add the project to the Tomcat server.
Say I'd like to change the url used to access my project. Maybe I want it to by at the root: localhost:8080/, or maybe a different directory structure altogether. How do I do this?
Right-click on your project then Properties > Web Project Settings. There you can change the context root. Then go to the Server View, right-click on the Tomcat server and select Clean... and you should get prompted to accept the modification of the server configuration.
to change the context root in eclipse please follow the below procedure
Right click on Project-->Properties-->WebProject

How do I get Eclipse to look in the build path for JSP includes instead of the web source?

We have a portion of our Dynamic Web Application that gets unpacked from a WAR, and placed in the build directory during compile. Because some of the files included in the web source are references to files from that WAR, then Eclipse can't find them. How do I get Eclipse to look instead in my build directory for the include files?
That's not caused by Eclipse. As for every normal request URL, the URL in the page attribute of the <jsp:include> should just match any of the url-pattern-s definied in webapp's or servletcontainer's web.xml to be able to get a resource. In your case you just need to define a servlet which gets the resource from the classpath and streams it to the outputstream of the response and map this servlet with an url-pattern in web.xml which in turn can be used by <jsp:include>.