Welcome file in java - java

I don't know how to access the html(fp.html) file under webcontent folder.
Deployed app structure
fp(app name)
|__ fp.html
|__ META-INF
|__ WEB-INF
My web.xml has this configuration
<?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">
<display-name>fp</display-name>
<servlet>
<servlet-name>FpServlet</servlet-name>
<servlet-class>com.fp.FpServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FpServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>fp.html</welcome-file>
</welcome-file-list>
</web-app>
If I access the file localhost:8080/fp/fp.html like this, It shows 404 error.
But accessing localhost:8080/fp/ working fine.
Kindly help me to overcome this problem.

Remember that Tomcat can host multiple applications. So in general your folder structure would look something like this:
~/<yourtomcatfolder>/webapps/<yourapplication>
You put any HTML files you want to access directly under that folder, which means you can then access the file like this:
http://localhost:8080/yourapplication/fp.html
Obviously the port and everything is configurable, so the above is just an example. You put also any CSS and JS files similarly, and you can have sub-folders.
Then you put your classes (servlets etc.), libraries etc. under the special WEB-INF folder under your application's directory.
~/<yourtomcatfolder>/webapps/<yourapplication>/WEB-INF/classes
~/<yourtomcatfolder>/webapps/<yourapplication>/WEB-INF/lib
Anything under WEB-INF is not accessible from outside (so no one can download your class files and decompile them etc.)

Related

Tomcat Landing Page not comming on localhost:8080

I created a Dynamic Web Project on Eclipse with the name JavaEEWebContent. The file structure of the project looks something like this.
After compiling when I do a "Run on Server", it runs fine.
When I go the link http://localhost:8080/JavaEEWebContent/, it works fine and displays me the homepage(in my case its index.html).
The problem:
When I go to http://localhost:8080 it shows
I know that the landing page for http://localhost:8080 looks something like this :
Any idea why it is not coming for me or what could go wrong?
Also I am using Tomcat 8.5.54.
This is how my web.xml looks like if that helps:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<welcome-file-list>
<welcome-file>index3.html</welcome-file>
<welcome-file>index2.html</welcome-file>
<welcome-file>index2.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>addNumbers</servlet-name>
<servlet-class>com.servlet.addNumbers.AddNumbers</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>addNumbers</servlet-name>
<url-pattern>/addNumbers</url-pattern>
</servlet-mapping>
</web-app>
Please help.
Nothing's wrong. Eclipse is deploying your web application, and only your web application, when it launches Tomcat. What you're expecting to see is a separate web application. You'll find it in the web-app/ROOT folder of your downloaded copy of Tomcat.

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

How to change path to index.html (AngularJS) in GAE j2ee

I have a j2ee app running on GAE. There is that pre-generated index.html, which is called every time I run the server - let's say http://localhost:8080. But I want to write my own front-end using AngularJS. For that, I created the whole project with Yeoman and copied all the files to folder in my j2ee project.
Right now -> I can call manually the AngularJS webapp like this: http://localhost:8080/webapp/app/index.html
But obviously, that is not the way one wants to go. I would like to call it simply on http://localhost:8080. How can I achieve this?
So it took me longer than expected, but here's how I got it working using the welcome file:
Here's my folder structure:
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_2_5.xsd" version="2.5">
<servlet>
<servlet-name>So123</servlet-name>
<servlet-class>so123.So123Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>So123</servlet-name>
<url-pattern>/so123</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>webapp/app/index.html</welcome-file>
</welcome-file-list>
</web-app>
Hope this helps.
https://developers.google.com/appengine/docs/java/config/webxml#The_Welcome_File_List
You can also change the root directory in the appengine-web.xml file. Full documentation here.
<public-root>/app</public-root>

Deploying WebContent folder from github as a WAR file

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!)

Categories