cloud foundry 404 but local tomcat is fine - eclipse

First lets get the code out of the way.
index.html in WebContent
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Child Tickets</title>
</head>
<body>
<h1>Find all child tickets affected user and their info</h1>
<hr>
<form name="f1" method="GET" action="/FindChildTicket4/FindChildTickets">
<input type="text" name="masterticket">
<button type="submit" value="main" name="btnSubmit">Hello</button>
<br>
<br>
<div id="results">
results html
</div>
</form>
</body>
</html>
web.xml in WebContent/WEB-INF
<?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>HelloWorld</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>
</web-app>
HelloAgain.java in Java Resources/src/hlo.hello.net
package hlo.hello.net;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class HelloAgain
*/
#WebServlet("/HelloAgain")
public class HelloAgain extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public HelloAgain() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
when I deploy this in Eclipse to the local Apache7 server the html fires off and when I click the Hello button I get back the "Served at: /HelloWorld" in the browser.
Now when I push this to our internal Cloud Foundry setup VIA Eclipse I get this when I click on the Hello button
HTTP Status 404 - /HelloWorld/HelloAgain
type Status report
message /HelloWorld/HelloAgain
description The requested resource is not available.
Apache Tomcat/7.0.62
I have tried these sites...
HTTP Status 404 - on Eclipse with Tomcat
Java - Servlet 404 error
Getting HTTP Status 404 error when trying to run servlet
java servlet not found error 404 in eclipse
as well as a few others and non of the recommendations fixes the cloud foundry side. The HTML will work every singe time but it never finds the servlet. I also have looked at the cloud foundry server files I deployed and the class is there under WEB-INF/classes/hlo/hello/net/
Very odd. Also some of the recommendations in the linked sites broke the local apache deployemtn to where the server would shutdown. Just seeing if anyone has any insight since I cannot find any good data when searching for cloud foundry 404.

It appears that you're trying to access /HelloWorld/HelloAgain.
HTTP Status 404 - /HelloWorld/HelloAgain
Your Servlet is configured for /HelloAgain and /HelloWorld is the context (you can see this in the output from when you run locally, Served at: <context>).
When you deploy an application to a local Tomcat, it is typically done under a context and not the ROOT context. The difference is when you deploy to a context, everything is prefixed with that context name. When you deploy to Cloud Foundry, the Java build pack always deploys your application as the ROOT context. This means there is no prefix.
If you have hard coded URLs or links in your application that include the context name they will be incorrect when you deploy to CF and that would generate 404s.
One quick test would be to go to /HelloAgain when you deploy on CF. Since the app is deployed under the ROOT context, it should be accessible at that URL. I suspect you'll see "Served at: /" in the output.
One way to make sure URL's are generated correctly is with JSTL Core's <c:url> tag. Another way would be to build your URL's from parameters on the HttpServletRequest object. I'm sure there are many other ways. You just need to make sure the URL being used takes into account the context of where the application is being deployed.
Hope that helps!

Related

Can't access a Java servlet through its URL

I'm trying to set up a very basic JEE project with Eclipse+Tomcat using a single servlet and JSP. However I'm getting a HTTP 500 Internal Server Error when trying to access its URL http://localhost:8080/test/toto. The exception message being displayed is:
Cannot invoke "javax.servlet.RequestDispatcher.forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse)" because the return value of "javax.servlet.ServletContext.getRequestDispatcher(String)" is null.
Here is my Test servlet code:
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Test extends HttpServlet {
public void doGet( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException{
this.getServletContext().getRequestDispatcher( "/WEB-INF/test.jsp" ).forward( request, response );
}
}
The test.jsp file should just display a message : "Generated by a JSP"
Here's the content of web.xml for reference :
<?xml version="1.0" encoding="UTF-8"?>
<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">
<servlet>
<servlet-name>Test</servlet-name>
<servlet-class>com.sdzee.servlets.Test</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/toto</url-pattern>
</servlet-mapping>
</web-app>
The corresponding Tomcat log reads :
0:0:0:0:0:0:0:1 - - [10/Dec/2020:14:29:47 +0100] "GET /test/toto HTTP/1.1" 500 1606
This is the project arborescence
Edit: Other things I recently tried: Reinstall Eclipse+Tomcat on another pc and start again, move test.jsp directly to WebContent and access http://localhost:8080/test/test.jsp: results in a HTTP 404 ressource not found error.
I've just noticed that before I create my servlet I can access whatever file I create under WebContent at http://localhost:8080/test/whatever.jsp but I can't do that anymore once I add the servlet and establish the mapping in web.xml
I was editing the web.xml file of the Tomcat server and not the one of the project, that's why nothing was working as expected.

How to set http headers in JBoss EAP 6.1

I want to set the http headers for x-frame options and Strict-Transport-Security in jboss 6.1.0.
I have been searching for the proper configuration file to add these headers, am able to see some procedures for jboss 6.4, jboss 7 but I didn't get anything for jboss 6.1
Configure Http Headers in JBoss EAP 7
This is in jboss 7, I need to do the same for jboss 6.1
I have tried a lot in identifyiing the proper confiurtion changes needed for this in jboss 6.1, but am helpless.
please let me knoe if someone is aware of doing this in jboss 6.1
Thanks in advance.
If you are using Apache HTTPD as a proxy to JBoss, it is very easy to add all these headers using the Header directive. Otherwise you can set all these headers in a custom filter and place in the corresponding web application’s lib folder.
This answer is present in RedHat Knowledgebase. As it requires RedHat credentials, I'm posting the same answer here.
Solution:
A servlet filter can be used to add the additional HTTP header to the response. Below is an example filter which uses Servlet 3.0 #WebFilter. Using annotation does not require to configure web.xml to enable the filter.
/*
* This is a sample servlet filter to set "X-Frame-Options" http header to
* http response.
*/
package com.redhat.jboss.support;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebFilter;
#WebFilter("/*")
public class AddCustomHeaderFilter implements Filter {
/**
* Take this filter out of service.
*/
public void destroy() {
}
/**
* #param request The servlet request we are processing
* #param result The servlet response we are creating
* #param chain The filter chain we are processing
*
* #exception IOException if an input/output error occurs
* #exception ServletException if a servlet error occurs
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
((HttpServletResponse)response).setHeader("X-Frame-Options", "SAMEORIGIN");
chain.doFilter(request, response);
}
/**
* Place this filter into service.
*
* #param filterConfig The filter configuration object
*/
public void init(FilterConfig filterConfig) throws ServletException {
}
}
After compiling the AddCustomHeaderFilter.java, one package will be
creating named com.redhat.jboss.support with AddCustomHeaderFilter.class.
Create a jar for the AddCustomHeaderFilter.class using following
command. It will generate a jar AddCustomHeaderFilter.jar :
jar -cvf AddCustomHeaderFilter.jar com
Put this jar in your Web application's WEB-INF/lib folder. It will enable the Servlet filter in the web application.
NOTE:
The example given in AddCustomHeaderFilter.java class is for "SAMEORIGIN". There are below possible values for X-Frame-Options:
DENY: The page cannot be displayed in a frame, regardless of the
site attempting to do so.
SAMEORIGIN: The page can only be displayed in a frame on the same
origin as the page itself.

How can i integrate rest services in existing JSF application using jersey?

I'm working on existing JSF application i need to implement rest service in this application so user can also consume data using rest api from existing jsf application.
I have tried
How can I integrate JSF with REST? but no response made on this question.
Calling REST service in my JSF application here requirement is opposite from my case.
How to redirect to JSF page from JAX-RS method? with this i have same issue.
I have follow this https://www.mkyong.com/webservices/jax-rs/restful-java-client-with-jersey-client/
downloaded jar of jersey-client-1.19.4.jar, added to library creating class
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("/rest")
public class JSONService {
#GET
#Path("/get")
#Produces(MediaType.APPLICATION_JSON)
public void test(#Context HttpServletRequest request, #Context HttpServletResponse response)
throws IOException {
String myJsfPage = "i am here";
response.getWriter().write(myJsfPage);
}
}
but it gives me 404 page not found, please help me.

GWT Client files in one Server & GWT Servlets in other Server

I'm trying to separate GWT Client & Server. If i'm not wrong, GWT client code is getting server reponses by connecting to the servlet we mentioned in GWT Project's web.xml. If So, can i host my GWT Servlets in one Tomcat Server & GWT Client code in other tomcat server ..?
Will it work ..? If so how to do that, i have already tried something working with hosted.html in GWT Client files. But it didn't worked
Yes, you can host client files in any web-server since they are static stuff, actually what you need is to pick your index.html, the .nocache.js and all the *.cache.(js|html) files and put them in any web server (apache, nginx, iis, jetty, etc).
You could even replace the index.html by any other html generator like php, jsp etc.
But of course the server side should be hosted in a servlet container.
What you have to be aware about, is that when the server with your static files are in a different domain than the servlet server, ajax requests will fail because of security constrains (see CORS).
To avoid that restriction there are many ways in gwt (gwtquery-jsonp, gwt-xdm, etc).
What I'm using is a filter (see code above) able to enable CORS when an options request is received.
You have to modify your client code as well to configure correctly the url of the servlet-container. Here is an example for changing the url with RequestFactory.
Client side code for RF:
myFactory = GWT.create(MyRFFactory.class);
DefaultRequestTransport transport = new DefaultRequestTransport();
transport.setRequestUrl("http://my.servletcontainer.com/gwtRequest");
myFactory.initialize(eventBus, transport);
web.xml configuration
<filter>
<filter-name>CORSFilter</filter-name>
<filter-class>my.namespace.CORSFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CORSFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Server filter
public class CORSFilter implements Filter {
public void doFilter(ServletRequest servletRequest,
ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) servletRequest;
HttpServletResponse resp = (HttpServletResponse) servletResponse;
String o = req.getHeader("Origin");
if ("options".equalsIgnoreCase(req.getMethod())) {
resp.setHeader("Allow", "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS");
if (o != null) {
resp.addHeader("Access-Control-Allow-Origin", o);
resp.addHeader("Access-Control-Allow-Methods",
"POST, GET, OPTIONS");
resp.addHeader("Access-Control-Allow-Headers",
"content-type,pageurl,x-gwt-permutation");
resp.setContentType("text/plain");
}
resp.getWriter().flush();
return;
}
if (o != null) {
resp.addHeader("Access-Control-Allow-Origin", o);
}
if (filterChain != null) {
filterChain.doFilter(req, resp);
}
}
#Override public void destroy() {
}
#Override public void init(FilterConfig arg0) throws ServletException {
}
}
The same question has been asked in the past.
gwt-split-client-and-server
What you can do is use the servlet as a proxy to another server, where you have implemented your model logic.

Protecting Jersey web service with SSL

I have pretty simple web-service, which runs on the Embedded Glassfish server:
#Stateless
#Path("/id")
#Produces(MediaType.TEXT_HTML)
public class SimpleFacadeBean {
#GET
public String sayHello(#Context HttpServletRequest request, #Context HttpServletResponse response) {
return "Hello world!";
}
}
And corresponding class, that set root-element for all web-service calls:
#ApplicationPath("root")
public class ApplicationConfig extends Application {
}
Everything works fine, but I want to protect this GET method through SSL.
I have read such manuals as: this, and this. I am not interested in configuring Glassfish part of SSL connection (I know how to do it), I just want not to see my GET method in localhost:8080/root/application.wadl link (this will mean that my web-service is connected to 8181 secure glassfish port, not to default unsecure 8080 port).
For this purpose I wrote web.xml descriptor and put it in webapp/WEB-INF directory. The most interesting part of it looks like:
<security-constraint>
<display-name>SecurityConstraint</display-name>
<web-resource-collection>
<web-resource-name>Secure Area</web-resource-name>
<url-pattern>/root/id</url-pattern>
<http-method>GET</http-method>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
But after executing I saw (in application.wadl) again my web-service URL connected to unsecured port (8080 instead of 8181). I have tried a lot of other ways, trying to make my code work correctly. I was not successful at all.
I package this web-services into .war. After that this .war goes to .ear. Any ideas why I am not successful in NOT seeing my "secure" web-service in application.wadl?