Why doesn't tomcat see certain folders? - java

In my tomcat webapps directory I have various projects with servlets in them ..
say named test,beer etc
My question is irrespective of the web.xml or contents when i type localhost:8080/folderName I must be able to view the contents right? but it doesnt work for all folders , for some it says resource not found ,
Example my test folder opens in the browser and my beer folder doesnt , I restarted the browser and tomcat after adding or modifying folders , why can this happen , please explain

My dear friend there is a certain process/configuration/rule that tomcat follows in order to render any web content.It is not some magic happening.
Tomcat has a way of reading web deployments within the /webapp directory.
When we type some URL On the browser tomcat does the following:
Example.
URL : http://localhost:8080/foldername/xyz
Here tomcat takes the part of the URL after http://localhost:8080,that is foldername/xyz.
So here the first part which is foldername means name of the folder present in the /webapps folder.
So reading this tomcat goes inside that folder.Later tomcat is at the mercy of a file called web.xml.All mapping from /foldername/ i.e. /xyz in our case, onwards are present in web.xml.
In your case , if you type http://localhost:8080/foldername/ , tomcat knows that browser refers to webapps/foldername but does not know which resource html/jsp/servlet to forward the request so as to be able to generate a response.
Hence it gives a resource not found exception.
If you want to run the above URL (http://localhost:8080/foldername) then you need to configure a <welcome-file-list> tag in the web.xml file.
So for the folders which are working in your case with the above URL, just open their web.xml file and you shall find the <welcome-file-list> tag.

Actually this is what happened
I had a folder test which had some files (NO WEB-INF) , tomcat listed its contents on typing localhost:8080/test
My other folder beer had a WRONG WEB-INF web.xml configuration
So the following is clear to me now
Without a WEB-INF folder and web.xml tomcat will just list the contents of the directory but when you have a WEB-INF and web.xml and something is wrong in them it doesnt even list the directory contents .
Just my understanding of it .
Thanks!

Related

How to deny direct url access to files on Jetty

I'm running webapps on Jetty. I have set "dirAllowed" to "false" to disable the directory browsing on the defined contextpath by
webAppContext.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false").
But, users can still access other files (not located on the contextpath) through url (eg. http://somehost.yahoo.com:8081/abc.xml) and abc.xml is located under the root directory of the Jetty server.
Is there a way to block/deny direct url access to files located on Jetty? Thanks!
Not without adding some functionality to your webapp, there isn't. A Java Webapp is essentially a standardized directory structure beginning at the context root (myWebApp in the sample below).
myWebApp/
index.jsp
styles/
mywebapp.css
images/
myimage.png
WEB-INF/
web.xml
lib/
MyLib.jar
classes/
MyPackage/
MyServlet.class
Anything above WEB-INF is directly serve-able, anything below WEB-INF isn't. You could dream up some authorization scheme using Servlet Filters (http://www.oracle.com/technetwork/java/filters-137243.html) and restrict access to content above WEB-INF. Alternatively, if Authentication/Authorization is what you are after, look into Http Authorization and how it can be implemented in Jetty (http://www.eclipse.org/jetty/documentation/current/configuring-security-authentication.html). One way or another, you are going to be some coding or configuration to restrict access to the content above WEB-INF in a Java webapp.

Getting error running struts2 helloworld program

I m new in Struts2. Creating a hello world program using struts. When I run it got first screen where i put my input but when click on submit button giving following error.
Source of this helloworld example: http://www.tutorialspoint.com/struts_2/index.htm
HTTP Status 404 - /HelloWorldStruts2/hello
type Status report
message /HelloWorldStruts2/hello
description The requested resource (/HelloWorldStruts2/hello) is not available.
Apache Tomcat/6.0.29
Suggest what is the issue?
Got the solution.
In index.jsp there was <form> tag. When I changed it to <s:form> its working fine.
Or change from
<form action="hello">
to
<form action="hello.action">
This is what worked for me, specific to the question. Tomcat 8 was used:
Make sure you have created the classes folder under WebContent\WEB-INF.
In that create the logging.properties file and add the following content to it. (It doesn't matter if the file jumps on its own to Java Resources > Libraries) :-
org.apache.catalina.core.ContainerBase.[Catalina].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].handlers = \ java.util.logging.ConsoleHandler
Then in the location of Tomcat's webapps folder delete your HelloWorldStruts2.war file.
Now follow the site's instructions to Export your project to a new HelloWorldStruts2.war file (remember to overwrite the existing .war file with the same name if it's there).
Again, deploy this file by copying it to your Tomcat directory's webapps folder.
In the browser, go to http://localhost:8080/HelloWorldStruts2/index.jsp again, though I'm not sure it will work for sure this time.
But this time, watch the Tomcat server application's verbose output. You will get some kind of exception like this (leave out Warnings for now):
30-Mar-2014 17:39:29.273 SEVERE [localhost-startStop-7] org.apache.catalina.core.StandardContext.filterStart Exception starting filter struts2 java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register(XmlConfigurationProvider.java:213)
at org.apache.struts2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:102)
...
Seeing this, what I did immediately was copy the commons-lang3-x.y.z.jar to the WebContent\WEB-INF\lib folder, and then exported and deployed the project again.
This time, again the page wasn't showing, so I watched the logs and found out that Tomcat did not explicitly clear the contents of the webapps\HelloWorldStruts2 folder.
After deleting both HelloWorldStruts2 and HelloWorldStruts2.war and refreshing the index.jsp page in the browser, my project did work fine!

Tomcat: Get short URL for Jersey based Rest Service

I have a Jersey based Rest service running on a tomcat server. There is no UI, just a server that offers some rest services. Now, to access this service the URL that i have to type in is pretty long. Something like localhost:8080/MyApp/url_pattern/classPath/method where MyApp is the webapp that i deployed, url_pattern is the pattern that i defined in the servlet-mapping in web.xml, classPath and method being the #Path annotations for the Class and method respectively. Is it possible to shorten it such that I get rid of the MyApp and url_pattern part of this URL. Something like localhost:8080/classPath/method.
PS: There is just one webApp running on this server, so no point having the MyApp part
I don't think you can remove all what you desire from the url but you can definitely remove the MyApp part by making it the root application for tomcat.
Answer on this related link describes it pretty well, how to set your application as the root application. So you can access your REST services without having the app name in url:
Setting default application in tomcat 7
Content copied from the above link:
First Method:
first shutdown your tomcat [from the bin directory (sh shutdown.sh)]
then you must delete all the content of your tomcat webapps folder (rm
-fr *) then rename your WAR file to ROOT.war finally start your tomcat [from the bin directory (sh startup.sh)]
Second Method:
leave your war file in CATALINA_BASE/webapps, under its original name
- turn off autoDeploy and deployOnStartup in your Host element in the server.xml file. explicitly define all application Contexts in
server.xml, specifying both path and docBase. You must do this,
because you have disabled all the Tomcat auto-deploy mechanisms, and
Tomcat will not deploy your applications anymore unless it finds their
Context in the server.xml.
Note:
that this last method also implies that in order to make any change to
any application, you will have to stop and restart Tomcat.
Third Method:
Place your war file outside of CATALINA_BASE/webapps (it must be
outside to prevent double deployment). - Place a context file named
ROOT.xml in CATALINA_BASE/conf//. The single element in this context
file MUST have a docBase attribute pointing to the location of your
war file. The path element should not be set - it is derived from the
name of the .xml file, in this case ROOT.xml. See the Context
Container above for details.

How to read a directory in webapp folder of Maven web application

I'm working in maven web application. I need to read a directory(For ex: Files) in my webapp folder as follows,
Java.io.File file = new Java.io.File("path");
But I don't know how to specify the path of the directory here.
You shouldn't give local path addresses. Path should be a relative address, e.g. /files/images under your web archive (.war) folder.
To use relative paths properly, I suggest you to add your target folder to the resources definiton of POM.xml, check out these pages
http://www.mkyong.com/maven/how-to-change-maven-resources-folder-location/
http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
You can refer to resources folder easily with something like this:
this.class.getResource("Mydirectory/SubDirectory");
When in doubt how relatives paths work, it's always best to do something like that:
System.out.println(new File("/my/desired/directory").getAbsolutePath());
This will print out the path in which classpath will look for the files.
Assuming:
servlet container webapps dir is located in: /var/lib/tomcat6/webapps
your webapp is called my-webapp.war
You should see the following output: /var/lib/tomcat6/webapps/my-webapp/my/desired/directory
Another pointer: you have mentioned that you are looking for webapp directory. I hope you know that this directory will not end up in *.war - it's contents will.
War files are not always expanded when they are deployed to an app server, so it's possible that a relative path won't exist in a filesystem at all.
Best bets are to use getResource from the class loader, which will return things in the class path (the WEB-INF/lib directory, etc), or to use the getResource() method of ServletContext to find things in the web application itself.

directory structure in Tomcat 5(s)

I was wondering if it was possible to maintain a directory structure within the application folder in side webapps folder, i.e. I have created a folder called ITC357, which acts as my application folder which carries all my files, so the directory path is as follows:
C:/Program Files/Tomcat5/webapps/ITC357
I'm doing an assignment and I would like to deploy that ITC357 in a separate folder can I do this? if so how?
It is not clear what you are asking:
If you asking if it is possible to have directories inside C:/Program Files/Tomcat5/webapps/ITC357 then the answer is "Yes".
If you are asking if it is possible to put your webapps file in a separate directory then the answer is "Inadvisable". The tomcat framework looks for certain files within the webapps/<name> tree; e.g. a context.xml file, a web.xml file, classes / JARs, etc. You could code your servlet to look for other things in other places, but this causes various problems with deployment (and undeployment) and security.
If that doesn't cover it, please clarify your question.
If you are writing JSPs, then the URL of the JSP will include any subdirectories that you create. So if under ITC357 you create a subdirectory called "foo", and in that subdirectory you have a JSP called "bar.jsp", and your context name is "plugh", then the URL of that JSP will be "http://whateverserver.com/plugh/foo/bar.jsp".
If you are using servlets, then it is up to your code to decide what to do with any URLs passed to it. You could map "http://whateverserver.com/plugh/foo/twisty.do" to "c:/program files/tomcat5/webapps/ITC357/WEB-INF/classes/foo/twisty.class", or you could map it to someplace totally different. (I prefer to map to a package name that matches the URL unless there's good reason to do otherwise.)

Categories

Resources