I am still running into some issues that already have been discussed here, however I still do not know what I am doing wrong.
My application structure is:
/usr/share/tomcat/webapps/greeting
--index.html
--src
--META-INF
--WEB-INF
--web.xml
--classes
--sk
--simo
--Greeting.class
When requesting http://localhost:8080/greeting/ I receive an HTTP 404 response.
When requesting http://localhost:8080/greeting/hi, I receive an HTTP 405 response. This is not an issue, as the Servlet only processes POST requests.
My question is: How can I make the Tomcat server provide the index.html file.
This is my web.xml file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
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_3_0.xsd"
version="3.0">
<display-name>Hello, World Application</display-name>
<description>
This is a simple web application with a source code organization
based on the recommendations of the Application Developer's Guide.
</description>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/greeting/*</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>
</web-app>
Some information on the server:
[miso#edubox conf]$ /sbin/tomcat version
Server version: Apache Tomcat/7.0.76
Server built: Mar 12 2019 10:11:36 UTC
Server number: 7.0.76.0
OS Name: Linux
OS Version: 3.10.0-957.1.3.el7.x86_64
Architecture: amd64
JVM Version: 1.8.0_212-b04
JVM Vendor: Oracle Corporation
1) Definition of each servlet consists of two parts: 1) Binding of servlet class to a logical name and 2) Mapping this logical name to URLs.
In your web.xml you have defined only the 2nd part. Now you should add there also the servlet class name. If you keep your logical name "default", then add following code:
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>sk.simo.Greeting</servlet-class>
</servlet>
2) Alternatively, use #WebServlet(value="/greeting"). But then remove your servlet mapping from web.xml.
3) Independent on the answers above, the mapping of folders to context root is important. If you have not change it (I suppose you have not, this is good), then path "/usr/share/tomcat/webapps/greeting" means, that everything in your application is available via URLs that have context root "/greeting". It means, if you want to call a servlet via "/greeting/hi", servlet should be mapped to "/hi", not to "/greeting" and not to "/greeting/hi". So use following in your servlet: #WebServlet(value="/hi"). This you can call it via ".../greeting/hi".
The main issue was an errorneous redeployment. As soon as I deleted the app's top level directory "greeting" and restored its contents, the index.html finally has shown up.
The servlet mapping now follows the example of the Tomcat 7 "sample" app:
web.xml:
<servlet>
<servlet-name>Greeting</servlet-name>
<servlet-class>sk.simo.Greeting</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Greeting</servlet-name>
<url-pattern>/hi</url-pattern>
</servlet-mapping>
I reference it via:
<form action="hi" method="POST">
This works fine.
Related
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.
I am following a simple webservice tutorial and can't seem to interact with the Java code. I suspect my web.xml has an error but I'm not sure. There are no obvious errors and the index.jsp is server without any problems.
So, when I'm running it on the server, it opens index.jsp and I then try the following urls, but I'm getting 'HTTP 404 Errors'
http://localhost:8080/RestApi/ - works, shows html page
http://localhost:8080/RestApi/rest - http 404 error
http://localhost:8080/RestApi/rest/hello - http 404 error
http://localhost:8080/RestApi/rest/hello/somevalue - http 404 error
Here is what i have
Dynamic web project with jersey libs imported.
A note on this - I got an error for class not found and saw that I had to use Glassfish.org... instead of the com.sun one, don't know why, but there ya go.
My web.xml is as follows. No errors.
<?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">
<display-name>RestApi</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>
<display-name>Rest Web Services App by me</display-name>
<servlet>
<servlet-name>exampleServlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.rest.example</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>exampleServlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
My java class is as follows. No errors.
package com.rest.example;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
#Path("/hello")
public class HelloWorld {
#GET
#Path("/{param}")
public Response getMsg(#PathParam("param") String msg){
String output = "Welcome to the world of Rest : "+msg;
return Response.status(200).entity(output).build();
}
}
You are using the old Jersey 1.x property
com.sun.jersey.config.property.packages
For Jersey 2.x it should be
jersey.config.server.provider.packages
As a general rule, anything where you see com.sun.jersey is for Jersey 1.x and org.glassfish.jersey is for 2.x.
try:
http://localhost:8080/rest/RestApi/hello
check my case
the rule seems like this: /{url-pattern}/{project}/{path}
I run my case with jetty
another example
the above issue can occur due to following reasons :
First check the name of your application deployed in the webapps folder of the tomcat, whether it is matching your url or not that is giving 404.But in the above case, as it is showing the welcome page, then it is not the concern here.
Check the url pattern mention in the web.xml as it must be the same as in the url you are hitting.
<url-pattern>/rest/*</url-pattern>
Third thing to check is that the path defined in the rest class with #Path annotation.
Check the web.xml for following entries if you are using jersey jars 2.x as suggested above by #Paul Samsotha
<servlet>
<servlet-name>Jersey RESTful 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.pizzeria</param-value>
</init-param>
</servlet>
There may be a possibility to get this error when your server has been up without any errors but the context of your application hasn't. You can check it in the logs.For further information please refer this Link ---> 404 error due to SEVERE: Context [/example] startup failed due to previous errors
I am trying to demo simple Ajax/Servlet request.For this i have created a new Dynamic web project in eclipse and added a simple Servlet to take the request and present the same back to UI. I have included my Servlet details in web.xml. I am using Tomcat as my server. No Build scripts yet(i felt not needed at this point)
Servlet Code:
response.setContentType("text/html");
PrintWriter out =response.getWriter();
String txt = request.getQueryString();
out.println(txt);
Js Code:
$(function(){
$.get('/jirarequest','OK',function(op){
$('#maindiv').appendTo(op);
});
});
Html Code:
<?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>Jira-Synchronization</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>JiraSyncServlet</servlet-name>
<servlet-class>src.JiraSyncServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>JiraSyncServlet</servlet-name>
<url-pattern>/jirarequest/*</url-pattern>
</servlet-mapping>
My Project structure:
My HTML:
I am getting a error in Fire bug saying
"NetworkError: 404 Not Found - http://localhost:8080/jirarequest?OK"
The URL i am using in browser is http://localhost:8080/Jira-Synchronization/index.html. i have double checked all kind of typo. I debugged by putting breaking point but the debugger never hit my Servelt. I am not sure what is wrong? URL is wrong or wiring is wrong or the way it set up something is missing?
Some info:
The 404 error means Tomcat doesn't have any file or servlet or anything exposed for serving at that URL path.
The 500 error means Tomcat has a servlet (or jsp) exposed at that URL path and an unexpected exception was generated while trying to run the java code.
Collection of things we know need to be changed:
$.get('/jirarequest/whatever','OK',function(op){
and
<servlet>
<servlet-name>JiraSyncServlet</servlet-name>
<servlet-class>JiraSyncServlet</servlet-class>
</servlet>
Plus you can test in the browser by typing this URL which should return OK:
http://localhost:8080/Jira-Synchronization/jirarequest/whatever?OK
[In case it is helpful, I am working from the book Core JavaServer Faces (3rd edition) and am on page 12, or thereabouts.]
I am trying to launch a JSF application using GlassFish but am having problems that I can't identify. I can start GlassFish correctly and see the screen that is depicted in the book, so that appears to be fine. I then copy the file login.war that I've created and placed in the directory containing the src and web directories for this project into the domains/domain1/autodeploy directory of GlassFish.
Going to http://localhost:8080 in Chrome shows the correct screen; however http://localhost:8080/login, as described in the book and corresponding to the login.xhtml page that I have created, simply returns an HTTP 404 Error telling me 'The requested resource () is not available'.
My web.xml file is as follows:
<?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/javee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/nx/javaee
http://java.sun.com/xml/ns/javaee/we-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-patter>/faces/*</url-patter>
</servlet-mapping>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
</web-app>
Can anyone offer any help? I am using the Eclipse IDE, my version of GlassFish, as detailed on the error page described above, is 3.1.2.2. If there is any more information that would be helpful, e.g. directory structures, please do ask me for it.
Thanks, Conor.
You should set your application context-root to login if you need to access it with http://localhost:8080/login. There are several ways how to do it, e.g. check this Glassfish tip or in Eclipse IDE, in project Properties click on Web Project Settings and enter new Context root of your application.
This is not a bug, so until you will run more then one application on your server, you can leave it this way if you wish.
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