Deploying WebContent folder from github as a WAR file - java

I downloaded the following project as a zip file from https://github.com/blueimp/jQuery-File-Upload
I was hoping to use my IDE to build me a WAR file but gave up after encountering some netbeans related dependencies.
After I managed to compile it in eclipse and obtained a classes directory which I placed within the WEB-INF folder of WebContent.
The webContent folder looks like a an average WAR folder so I zipped up WebContent and renamed it to WebContent.war which I deployed into my tomcat 6 webapps directory. I am using the same JRE as I compiled it within to run tomcat.
However it won't run.
Content of the web.xml (located in WEB-INF is:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>fileupload</display-name>
<servlet-mapping>
<servlet-name>UploadServlet</servlet-name>
<url-pattern>/UploadServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description/>
<display-name>UploadServlet</display-name>
<servlet-name>UploadServlet</servlet-name>
<servlet-class>info.sudr.file.UploadServlet</servlet-class>
</servlet>
</web-app>
I can see the following message in my tomcat error log:
28-Dec-2013 20:20:27 org.apache.catalina.startup.ContextConfig applicationWebConfig
SEVERE: Parse error in application web.xml file at jndi:/localhost/WebContent/WEB-INF/web.xml
java.lang.IllegalArgumentException: Servlet mapping specifies an unknown servlet name UploadServlet
If I then remove the following from web.xml (or move it to below the definition containing the servlet class):
<servlet-mapping>
<servlet-name>UploadServlet</servlet-name>
<url-pattern>/UploadServlet</url-pattern>
</servlet-mapping>
I can see the bear bones html of the index.html located in WebContent but I can't access the servlet itself.
Am I right to suspect the form action being called in the index.html may need some mapping in the web.xml file also as I do not see how upload can do anything:
<form id="fileupload" action="upload" method="POST" enctype="multipart/form-data">
I have tried changing action to action="UploadServlet" and action="/UploadServlet" but to no avail.
I have uploaded the WAR file here (Just click the smaller download button in the middle!)

Related

Java EE 404 Error on XHTML but Not HTML

I'm learning Java EE through a simple "Hello World" application, and when I run the program using index.html it works well. However, when I run the same program with index.xhtml, it throws a 404 Error saying "The requested resource is not available."
My folder structure is as follows:
When I run the program using index.html, I use the following URL: http://localhost:8081/index.html, and the page shows up with Hello World. And when I run using index.xhtml, I tried both: http://localhost:8081/example2/index.xhtml and http://localhost:8081/index.xhtml. Both give a 404 Error.
My web.xml is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
</web-app>
I've also tried multiple browsers (Chrome and Firefox). Any ideas why I'm getting a 404 on XHTML but not HTML?
You can config the home page in the web.xml with the code below:
<display-name>NameOfProject</display-name>
<!-- Configuration of your home page -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.xhtml</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
So you can use both:
http://localhost:8081/NameOfProject/ or
http://localhost:8081/NameOfProject/index.xhtml
The solution was pretty simple. When I checked the Tomcat logs, there was a ClassNotFoundException because Tomcat does not come with the jsf-api and jsf-impl jars. I created a new directory in WEB-INF called lib and put the 2 jars there. Yes, you need both jars. Now the XHTML file is found and the 404 goes away.

How to place html files in a Java Dynamic Web Project as a REST service using Eclipse

I created a Dynamic Web Project in Java using Eclipse JEE to make a REST web service. I am trying to add a html file simply to add a form that allows me to test my web services in a faster and easier way.
I have read that just placing the html files under the WebContent folder or the WEB-INF folder does the trick. But since I already have a web.xml file it's disrupting the expected behaviour:
This is my web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>REST web service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>ws,com.fasterxml.jackson.jaxrs.json</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>REST web service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
So where should I place my html files and what should I modify in my web.xml file to get it working (if it needs to be modified)?
In your web.xml you have a mapping for /* which means that the REST web service will receive all the connections, you can either make REST web service return the appropriate resource based on the url or change its mapping (<url-pattern>) to something more specific.

HTTP Status 500 wrapper can not find servlet class

I have created simple dynamic web project on eclipse. I am trying to submit the html form and passing the request to servlet. When I click on submit i get this exception:
HTTP Status 500 wrapper can not find servlet class com.tcs.navigator.Servlet.labServlet or a class it depends on
in jsp form actoin tag I had given same action path as per web xml which is :
action = "labServlet"
content of web.xml is :
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<welcome-file-list>
<welcome-file>home.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description>To Upload Files for processing</description>
<display-name>labServlet</display-name>
<servlet-name>labServlet</servlet-name>
<servlet-class>com.tcs.navigator.Servlet.labServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>labServlet</servlet-name>
<url-pattern>/labServlet</url-pattern>
</servlet-mapping>
</web-app>
I tried following workarounds:
clean tomcat directory
open/close project
clean projects
restart eclipse
Verifying class path
created new dynamic web project with same code
But still the same exception persist.
all libraries like servlet-api.jar are present in build path.
you have kept capital S in "Servlet.labServlet" . system is getting it as a Class named Servlet. name your servlet starting with caps like "LabServlet" in the code and start all your package name with small letters. so your complete class path should be
com.tcs.navigator.servlets.LabServlet

Jersey Services with Tomcat and Eclipse

I'm developing a rest service with Jersey 2.0 (I downloaded from http://repo1.maven.org/maven2/org/glassfish/jersey/bundles/jaxrs-ri/2.5/jaxrs-ri-2.5.zip) and I'm using Tomcat 7.0.47. I run Tomcat from Eclipse and my machine is a Mac.
I'm using the Eclipse tool to generate a WAR and to deploy the service.
This is my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>MyServices</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.service.services.Services</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
I include all Jersey jars into WEB-INF/lib, except javax.servlet-api-3.0.1.jar that it is into Apache/lib
When I deploy in Tomcat, it shows a very strange error caused by:
Caused by: java.lang.ClassNotFoundException: org.glassfish.jersey.client.ClientConfig
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)
... 70 more
My solution:
Add into Tomcat/lib all libraries that you download from Jersey and
are including into /ext folder of the Jersey .zip
Add into Web-Inf/lib of my project only libraries that are under /lib folder of the Jersey zip file
Add into Web-Inf/lib of my project javax.ws.rs-api-2.jar that you can find in /api folder of Jersey
With this, I don't have problems to run Tomcat with Jersey.
And this is my web.xml for Jersey 2.0
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>MyRESTServices</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.myservice.services</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Where com.myservice.services is the package where I have my services
Thanks for your comments!!
It works for me on a PC. Did you make the eclipse project a maven project? If you do that then you can use org.glassfish.jersey.bundles jaxrs-ri version 2.5 (or whichever) as a dependency in your pom and then when you do run->maven-install from eclipse it will build a war file with all the necessary dependencies in it.
Adding Jersey to an Eclipse project without maven is simple: Click properties for the project, go to the Java Build Path. Click on the Add Library on the left, select user library. After you click next you will see "user libraries" in the upper right. Click that. Click new. Add a name: jaxrs-ri-2.5 for example. Click add external jars, and add the jars in the api, lib and ext folders. You're done. This will probably also include them in any war file you make with eclipse.
Worked for me.
Go with jdk1.7.0_04 and install apache-tomcat-7.0.55 and set the environment variable for this setting and set eclipse for same for compiling building and runtime. then everything will be OK for Jax-RS-2.1

appengine java development server displaying source code

When I access a jsp page like this on an appengine development server:
localhost:8888/index.jsp/
it's displaying the source code of index.jsp in the browser. if you access without the trailing slash (i.e. index.jsp) then it renders jsp but with the trailing slash (i.e. index.jsp/) it displays the source code
Any idea why is this? and how to fix it?
It seems to happen only in development server and not in production. Production gives a 404 Not Found error, which is fine.
I am using SDK 1.6.4
web.xml:
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<servlet>
<servlet-name>RegisterPage</servlet-name>
<jsp-file>/register.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>RegisterPage</servlet-name>
<url-pattern>/signup</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
==========
so...
index.jsp -> renders page
index.jsp/ -> returns source code
register.jsp/ -> returns source code
register.jsp -> renders jsp
signup/ -> renders register.jsp
signup -> renders register.jsp
so it seems like it's the urls with *.jsp/ that have the issue
You should move all the *.jsp files into the /WEB-INF directory, and update your web.xml.
This way the *.jsp files will not be accessible directly, and the source code will be hidden.
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<servlet>
<servlet-name>RegisterPage</servlet-name>
<jsp-file>/WEB-INF/register.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>RegisterPage</servlet-name>
<url-pattern>/signup</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>IndexPage</servlet-name>
<jsp-file>/WEB-INF/index.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>IndexPage</servlet-name>
<url-pattern>/index</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/index</welcome-file>
</welcome-file-list>
I have the some problem when i have used "redirect" and apache tomcat 7, because the redirect is not supported in new version of apache. For solve your problem search news in changelog of your version of apache (if you use it) for the tag you used in your page, or publish code of your page to be able to suggest other solutions. May be that you're using deprecated tags. Also, keep in check the file localhost[DATE].log for more details

Categories