Google app engine No handlers matched this URL - java

i am going to call firebase Http request from a cron job i setup on google app engine.the cron job is deployed successfully but it did not trigger the firebase url as i think i am missing some setting in the web.xml file or in other files.
In the log viewer i see this type of info "No handlers matched this URL"
Any one have any idea.Any would be appreciated.
Following is my cron.xml setting
<?xml version="1.0" encoding="UTF-8"?>
<cronentries>
<cron>
<url>/cron</url>
<target>beta</target>
<description>Keymitt cron job</description>
<schedule>every 1 minutes</schedule>
</cron>
</cronentries>
this is my web.xml setting
<?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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>HelloAppEngine</servlet-name>
<servlet-class>com.company.HelloAppEngine</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloAppEngine</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>KeymittCron</servlet-name>
<servlet-class>com.company.KeymittCron</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>KeymittCron</servlet-name>
<url-pattern>/cron</url-pattern>
</servlet-mapping>
</web-app>
and this is my associated WebServlet
#WebServlet(name = "KeymittCron",value = "/cron")
public class KeymittCron extends HttpServlet {
private static final Logger _logger = Logger.getLogger(KeymittCron.class.getName());
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// super.doGet(req, resp);
URL url=new URL("httplink");
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.connect();
int requestCode=connection.getResponseCode();
if(requestCode==200){
_logger.info("firebase link triggered successfully");
_logger.info("Executed cron job");
}
else{
_logger.info("Error while triggering firebase link");
}
connection.disconnect();
resp.setStatus(200);
resp.getWriter().println("Done");
}
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//
doGet(req,resp);
}
}
And this is my Logger info

My answer here may be relevant - Google Cloud App Engine cron job - not calling the service
Basically, you may need this in your appengine-web.xml:
<service>beta</service>
The <target> you specify in your cron.xml must match the <service> you define in your appengine-web.xml

Related

context.getInitParameter("") and config.getInitParameter("") always returning null

I am a beginner in servlets and JSP and I've tried my best to get the values yet I am getting null, any help is welcomed:
This is basic code on using ServletConfig and ServletContext to get param-value from web.xml
Servlet_Ex_4.java (- servlet):
#WebServlet("/Servlet_Ex_4")
public class Servlet_Ex_4 extends HttpServlet {
ServletContext context;
ServletConfig config;
String appUser ;
String Database ;
#Override
public void init() {
ServletContext context =getServletContext();
appUser = context.getInitParameter("appUser");
ServletConfig config =getServletConfig();
Database = config.getInitParameter("Database");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out = response.getWriter();
//get ServletContext object.
//get context parameter from ServletContext object.
out.print("<h1>Application User: " + appUser + "</h1>");
out.print("<h1>Database: " + Database + "</h1>");
out.close();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<element>
<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_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>Servlet_Exercise_4</display-name>
<servlet>
<servlet-name>Servlet_4</servlet-name>
<servlet-class>Servlet_Ex_4</servlet-class>
<context-param>
<param-name>Database</param-name>
<param-value>Oracle</param-value>
</context-param>
</servlet>
<context-param>
<param-name>appUser</param-name>
<param-value>jai</param-value>
</context-param>
<servlet-mapping>
<servlet-name>Servlet_4</servlet-name>
<url-pattern>/Servlet_Ex_4</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>default.htm</welcome-file>
</welcome-file-list>
</web-app>
</element>
tag <element> must be removed from web.xml because it not part of XML definition in this case
<context-param> must not be used inside the <servlet>, it must be replaced by <init-param> to access parameters from ServletConfig object. This is the reason why the OP is getting null value.
<context-param> will work for parameter "appUser", it can be accessed using ServletContext object.
And mixing annotations with XML for same type of configurations can result in undesirable effects. So, #WebServlet should be removed from the source code as majority of configuration is done using XML.
So final web.xml should like below (with #WebServlet annotation removed from the Servlet_Ex_4 class)
<?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_4_0.xsd"
id="WebApp_ID" version="4.0">
<display-name>ServletParams</display-name>
<servlet>
<servlet-name>Servlet_4</servlet-name>
<servlet-class>Servlet_Ex_4</servlet-class>
<init-param>
<param-name>Database</param-name>
<param-value>Oracle</param-value>
</init-param>
</servlet>
<context-param>
<param-name>appUser</param-name>
<param-value>jai</param-value>
</context-param>
<servlet-mapping>
<servlet-name>Servlet_4</servlet-name>
<url-pattern>/Servlet_Ex_4</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>

Tomcat always run failure

I want to build a web project in Tomcat, but the code always fails to run.
I tried many hints from web,but no method succeeded.
Because I am Java beginner,I supply my IDEA setting.
public class AddServlet extends HttpServlet {
#Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String fname = request.getParameter("fname");
String priceStr = request.getParameter("price");
Integer price = Integer.parseInt(priceStr);
String fcountStr = request.getParameter("fcount");
Integer fcount = Integer.parseInt(fcountStr);
String remark = request.getParameter("remark");
System.out.println("fname = " +fname);
System.out.println("price = " +price);
System.out.println("fcount = " +fcount);
System.out.println("remark = " +remark);
}
}
<?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_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>AddServlet</servlet-name>
<servlet-class>com.atguigu.servlets.AddServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddServlet</servlet-name>
<url-pattern>/add</url-pattern>
</servlet-mapping>
https://imgur.com/a/DS9Z3lF

How to create correctly a Filter in Java EE 8 from Java code?

I want to create programmatically a Filter in Java EE 8 from Java Code, so I did this code in my application.
Gif captures: gif capture one - gif capture two
My filter is LoginFilter.java
package afrominga.filters;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
#WebFilter
public class LoginFilter implements Filter {
#Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("Hello from LoginFilter.");
}
#Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("I am running the LoginFilter tasks.");
}
#Override
public void destroy() {
System.out.println("Goodbie from LoginFilter");
}
}
I have in my web.xml
<?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_4_0.xsd"
version="4.0">
<display-name>Archetype Created Web Application</display-name>
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>afrominga.filters.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<error-page>
<error-code>404</error-code>
<location>/error/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error/500.jsp</location>
</error-page>
</web-app>
So, at the time of to open my browser with the root path context of my application, the filter print in the output the text of my System.out, but my page is left blank and does not show me the home page. I don't understand why this happens, this would must forward to my home page because only print a text in my console.
can would someone help me? please
One problem is that your doFilter() method. Right now, your filter is just printing and not passing the request along to the next filter/your application. You just need to add one line.
#Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("I am running the LoginFilter tasks.");
filterChain.doFilter(request, response)
}

filter that forwards to index page

I want to make a filter that would forward to /WEB-INF/index.html request to application that looks like this
http://localhost:8080/basic-application-web
Here is my filter
public class RootFilter implements Filter {
#Override
public void init(FilterConfig filterConfig) throws ServletException {}
#Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
if (req.getRequestURI().equals("/basic%2Dapplication%2Dweb/")) {
req.getRequestDispatcher("/WEB-INF/index.html").forward(req, resp);
}
chain.doFilter(request, response);
}
#Override
public void destroy() {}
}
My web.xml looks like this
<?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/javaeeweb-app_2_5.xsd'
id='basic_web' version='2.5'>
<display-name>Basic web application</display-name>
<servlet>
<servlet-name>serviceServlet</servlet-name>
<servlet-class>com.pack.ServiceServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>serviceServlet</servlet-name>
<url-pattern>/messaging</url-pattern>
</servlet-mapping>
<filter>
<filter-name>rootFilter</filter-name>
<filter-class>com.pack.RootFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>rootFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
However from time to time I get some weird behaviour where tomcat (I use it to deploy the war) is unable to find basic-application-web when I try to access directly using URL.
Though through tomcat manager it works fine. What is the problem? Maybe due to missing root servlet?
I moved index.html outside WEB-INF so basically the layout started to look like this
webapp
WEB-INF\web.xml
index.html
And adjusted filter to forward to /index.html instead of WEB-INF/index.html
req.getRequestDispatcher("/WEB-INF/index.html").forward(req, resp);
It has helped.

Forwarding requests in servlets

so I'm trying to create a servlet which will handle all requests for the WSDL at the endpoint, and then send the WSDL (Right now its just a file on the disk, later, I'm planning to change that file somewhat based on the permissions of the client). The way I'm trying to implement this is by creating a WSDLHandlerServlet class that's mapped to /* in web.xml. The way I understand it - all requests should be intercepted by this.
Once passed to the servlet it checks basically if the URI with query string is - "/TestProject/pace/soap?wsdl" and if so sends back the WSDL. This part works fine when I open "localhost:8180/TestProject/pace/soap?wsdl" in the browser and it displays the correct WSDL.
If the request has no query string, then I forward it to the cxf servlet which is linked to my web service implementor.
However when I send a request from a client (SOAP request), I get -
Exception in thread "main" com.sun.xml.internal.ws.client.ClientTransportException: The server sent HTTP status code 200: OK
Also it is clearly not reaching the implementing class since if it did, there would be a log. I suspect I have most probably messed up the servlet mappings.
This is the WSDLHandler Servlet-
package servlets;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class WSDLHandlerServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
#Override
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// This is just a test.. anything without a query string will be sent to
// cxf servlet... and any long request ending with ?wsdl will yield the
// wsdl.. need to work on the design...never mind now it is time to
// begin!!
String query = new String();
query = request.getQueryString();
/*
* File abc = new File("/home/aneeshb/testing.txt"); FileWriter fw = new
* FileWriter(abc);
*
*
* if(!query.isEmpty()){ fw.write(query); fw.close();}
*/String requestURI = request.getRequestURI();
File abc = new File("/home/aneeshb/testing.txt");
FileWriter fw = new FileWriter(abc);
fw.write(requestURI);
fw.append("1234");
fw.append(query);
fw.flush();
if (query==null) {
fw.append("2");
fw.flush();
RequestDispatcher dispatch = request.getRequestDispatcher("/pace");
fw.append("2");
fw.flush();
dispatch.forward(request, response);
fw.append("2");
fw.flush();
}
else if (requestURI.equals("/TestProject/pace/soap")
&& query.equals("wsdl")) {
fw.append("This is in part1 ");
fw.flush();
response.setCharacterEncoding("UTF-8");
response.setContentType("text/xml");
PrintWriter respWrite = response.getWriter();
File wsdlFile = new File(
"/home/aneeshb/Workspaces/Project1/TestProject/WebContent/WEB-INF/wsdl/stock.wsdl");
FileReader wsdlRead = new FileReader(wsdlFile);
BufferedReader wsdlBuffRead = new BufferedReader(wsdlRead);
int temp = 0;
while ((temp = wsdlBuffRead.read()) != -1) {
respWrite.write(temp);
}
respWrite.flush();
wsdlBuffRead.close();
}
else {
throw new IndexOutOfBoundsException();
}
}
}
My web.xml -
<?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-insance"
version="2.5"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>cxf</display-name>
<servlet>
<description>Apache CXF Endpoint</description>
<display-name>cxf</display-name>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<description>WSDL Handler</description>
<display-name>wsdlh</display-name>
<servlet-name>wsdlh</servlet-name>
<servlet-class>servlets.WSDLHandlerServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/pace</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>wsdlh</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
</web-app>
And just for good measure-the cxf servlet config file-
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:soap="http://cxf.apache.org/bindings/soap" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd" xmlns:tns="http://impl/">
<jaxws:server id="jaxwsService" serviceClass="impl.GatewayImplementor" address="/soap" >
<jaxws:serviceBean>
<bean class="impl.GatewayImplementor"/>
</jaxws:serviceBean>
</jaxws:server>
</beans>
Thanks, any help with what I am doing wrong or ideas on alternate solutions would be great!

Categories

Resources