Servlet resource is not available HTTP 404 - java

I got a servlet, which I'm trying to get to through a hyperlink.
I checked the Servlet's folder availability by a hyperlink to JSP file on that folder.
I also checked the Servlet itself when asking it on the hyperlink with the .java extension, and I got
the code of that Servlet on the browser.
When I'm trying to connect to that Servlet either to doGet or doPost, I'm getting a 404 HTTP error,
the resource is not available.
Web.xml:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>controllers.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/controllers</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/views/welcomePage.jsp</welcome-file>
</welcome-file-list>
</web-app>
What is wrong?

As you post in your question
I also checked the Servlet itself when asking it on the hyperlink with the .java extension
I think you are putting the servlet class file in a folder under your web folder and that is why you can access trough the url.
project
src
web
controllers
TestServlet.java
page.jsp
WEB-INF
classes
As opposed of the jsp files the servlet classes must be in the WEB-INF/classes folder (the .class file not the .java file), and the source code of your class must be in the src folder of your project under the package controllers so:
project
src
controllers
TestServlet.java
web
page.jsp
WEB-INF
classes
controllers
TestServlet.class
If you're using eclipse, netbeans or other IDE when you put your java class in the source folder it automatically put the .class in the WEB-INF/classes folder.

You map your servlet to the URL pattern /controllers - so your post should go to http://localhostOrWhatever/maybeYourWebAppDirectoryDependingOnSetup/controllers

Related

How to expose custom jar in lib folder of java web application?

I have an existing jetty application that i need to modify. Basically i just need one access point that can handle POST requests. I have access to the WEB-INF directory of the project. There i have web.xml file and a lib folder. As i understand from jetty documentation jars under lib will be loaded automatically. Is it possible to add a custom jar under lib folder and the somehow configure web.xml so that my jar will handle HTTP requests from a certain URL? If so how would i start with that?
You can simply create a jar with your Servlet class inside (a class that extends HttpServlet) and add the servlet declaration inside the web.xml
<servlet>
<servlet-name>A_Name</servlet-name>
<servlet-class>your.new.ServletClass</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>A_Name</servlet-name>
<url-pattern>/the/path/you/want</url-pattern>
</servlet-mapping>

Java webapps - why is index.html displayed when visiting a webapps root directory?

Say you have a webapp named MyWebApp deployed on Tomcat in localhost. If you visit it with your browser like this:
localhost:8080/MyWebApp
then why is the index.html displayed (assuming there is an index.html in the .war file)?
Is there a configuration somewhere that says requests to /MyWebApp should be responded to by sending the index.html? Is it possible to change that configuration? Why isn't there a servlet that handles this request?
The index.html page is specified as welcome page in web.xml file .
Which looks like this:-
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
this is the reason why you see index.html as welcome page.

Deploying a webapp under ROOT - index.html not displaying

I am using tomcat7 and have a small Java application that needs to be deployed under ROOT. The reason for this has to do with the client's inability to specify the webapp's proper context path at this time.
I have an index.html in this webapp and my issue is that when I deploy the application under the webapp name - ${catalina_home}/mywebapp - the index.html renders without a problem when I navigate to http://localhost:8080/mywebapp.
However, when I deploy it under ROOT - ${catalina_home}/ROOT - the index.html inside does not render when I navigate to http://localhost:8080/. The error is 404 not found. Does this have anything to do with overriding tomcat's default page?
My web.xml:
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/</param-value>
</context-param>
...
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
...
Can someone explain why this is so?
Thanks in advance.
UPDATE: I see in this stackoverflow Display html page in tomcat from maven RESTEasy webapp that the issue may be that my filter says anything under /* should go to the servlet. I tried to set up a default servlet for .html pages, but that does not see to help yet...
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
How do you deploy the app as ROOT? If you package as war file, to deploy as ROOT, change the war file to ROOT.war and have the index.html under the ROOT folder. Also check the web.xml under WEB-INF to have the welcom page set to index.html as below:
<welcome-file-list>
<welcome-file>
index.html
</welcome-file>
</welcome-file-list>
I found the answer here - http://docs.jboss.org/resteasy/docs/3.0.2.Final/userguide/html_single/:
"The downside of running Resteasy as a Servlet is that you cannot have static resources like .html and .jpeg files in the same path as your JAX-RS services. Resteasy allows you to run as a Filter instead. If a JAX-RS resource is not found under the URL requested, Resteasy will delegate back to the base servlet container to resolve URLs."
I created a filter instead and I was able to serve both the static page and my regular REST resources.

how to reference a file of higher hierarchy in a java servlet project

i'm trying to implement a basic mvc pattern for a java web app project in netbeans. i have the deployment descriptor (web.xml) correct but i can't seem to redirect to the correct jsp from the java servlet itself. here's the directory of my project folder:
/project
/src
/conf
MANIFEST.MF
/java
/ph
/com
/client
/esurvey
/objects
/* other .java files */
/servlets
ManageSurveysServlet.java
/* other .java files */
/build
/empty
/web
index.jsp
manage_surveys.jsp
script.js
style.css
/META-INF
context.xml
MANIFEST.MF
/WEB-INF
web.xml
/classes
.netbeans_update_resources
.netbeans_automatic_build
/ph
/com
/client
/esurvey
/objects
/* .class files found here */
/servlets
ManageSurveysServlet.class
/* other .class files found here */
a link from index.jsp calls the ManageSurveysServlet which in turn forwards a request object and redirects to manage_surveys.jsp, but given the above directories, i don't know what path/filename to use to reference manage_surveys.jsp with from the servlet. here's the code in the servlet that forwards the request object:
request.setAttribute("surveys", surveys); // surveys is an arraylist
RequestDispatcher dispatcher = request.getRequestDispatcher("manage_surveys.jsp"); // i'm guessing it can't find the jsp
dispatcher.forward(request, response);
UPDATE: web.xml
<?xml version="1.0" encoding="utf-8"?>
<web-app version="2.5" 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_2_5.xsd">
<servlet>
<servlet-name>ManageSurveysServlet</servlet-name>
<servlet-class>ph.com.client.esurvey.servlets.ManageSurveysServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ManageSurveysServlet</servlet-name>
<url-pattern>/ManageSurveys</url-pattern>
</servlet-mapping>
<session-config><session-timeout>30</session-timeout></session-config>
<welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list>
</web-app>
Move manage_surveys.jsp into WEB-INF folder.I think that will be solution.
The Javadoc says the given resource is a relative path. So executed in the classes dir means, you have to either put your jsp in the "classes" dir, but thats not a good idea, but for testing reasons u may try or
change your code to context root relative:
RequestDispatcher dispatcher = request.getRequestDispatcher("/manage_surveys.jsp");
or relative:
RequestDispatcher dispatcher = request.getRequestDispatcher("../../manage_surveys.jsp");
The Javadoc for reference:
http://docs.oracle.com/javaee/5/api/javax/servlet/ServletRequest.html#getRequestDispatcher(java.lang.String)

Remote application debug in eclipse

I m trying to debug some jsp pages in eclipse. These pages depends on some other projects, that are located in workspace. The physical files are located in webapps directory. I'm using tomcat7 as server. I have started Tomcat with standard configuration(debug at port 8000) and left also port 8000 in remote debug configuration. In my workspace there is a ic-config with WEB-INF and web.xml. It maps com.quartal.irtoolbox.ic.ControllerServlet to Controller like the following:
<servlet>
<servlet-name>Controller</servlet-name>
<servlet-class>com.quartal.irtoolbox.ic.ControllerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Controller</servlet-name>
<url-pattern>/Controller</url-pattern>
</servlet-mapping>
But when i try to access Controller , it is not accessible at all.I'm getting the error: "The requested resource (/Controller) is not available". What should i change and how should i call the servlet to be able to get and debug it?
Thanks
Magda

Categories