My customer sent one HTTP request with URL http://example.com/callback#id_token=data
Now I need handle this request and get data of "id_token".
Please help me handle this request using Java web project.
I tried with servlet but not get data of "id_token"
My servlet is below
<servlet>
<servlet-name>callback</servlet-name>
<servlet-class>com.admin.controller.CallbackServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>callback</servlet-name>
<url-pattern>/callback</url-pattern>
</servlet-mapping>
Thanks a lot.
Unless url-encoded, the hash #id_token=data usually ends up in document.location.hash on the client side.
console.log(document.location.hash);//#id_token=data
var hash = document.location.hash;
var split = hash.split('=');
console.log(split[0]);//#id_token
console.log(split[1]);//data
You can usually access the console with F12, tab "console".
Related
I would like to filter (modify) HttpRequest, which is coming to target Liferay Servlet, intercept it and add some attributes. (for example User) and then redirect (forward) the request to the target servlet. I create new servlet that should work like interceptor, add the attribute (request.setAttribute("user", User)) and then either dispatcher.forward or response.redirect... This is only for testing purposes to be able to call some liferay functionality from tests.
The problem is that Liferay is filtering the request and add there its 7 attributes. I tried also create SimpleFilter, but its not called when target servlet is called, only when I create my own Servlet which extends HttpServlet.
So the question is, is it possible to tell liferay not to filter the requests or more important not to remove the attributes from the request? I have debugged the Liferay source code, catch the Filter Chain and on the 1. filter I see there original request, and at the end the requests contains those Liferay attributes.
Many thanks.
The target servlet is defined like this:
<servlet>
<servlet-name>targetServlet</servlet-name>
<servlet-class>com.liferay.portal.kernel.servlet.PortalDelegateServlet</servlet-class>
<init-param>
<param-name>servlet-class</param-name>
<param-value>org.springframework.web.context.support.HttpRequestHandlerServlet</param-value>
</init-param>
<init-param>
<param-name>sub-context</param-name>
<param-value>targetServlet</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
elasticsearch-transport-wares
I don't understand, how to use this plugin...
I want use the servlet go to connect elasticsearch and operate...
My environment only apache tomcat servlet, please help me, Thanks.
The idea is that you modify your Maven pom.xmlby adding the following dependency (make sure to use the right version depending on the version of ES you're running):
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch-transport-wares</artifactId>
<version>2.7.0</version>
</dependency>
The JAR file of elasticsearch-transport-wares will ultimately end up in your WEB-INF/lib folder.
Then in your web.xml, you add a new <servlet> and <servlet-mapping> like this:
<servlet>
<servlet-name>esnode</servlet-name>
<servlet-class>org.elasticsearch.wares.NodeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>esnode</servlet-name>
<url-pattern>/es/*</url-pattern> <---- you can change this pattern
</servlet-mapping>
This will instantiate a new servlet serving requests on the http://server:8080/es/ path and proxying them to your local embedded Elasticsearch node, which means you can query ES through your web app, .e.g.:
curl -XGET http://server:8080/es/your_index/your_type/_search?q=*
The above query will be equivalent to querying an external ES cluster directly using
curl -XGET http://localhost:9200/your_index/your_type/_search?q=*
The main difference is that you can also query ES internally from anywhere within your web app (where you have access to the ServletContext) by retrieving the Elasticsearch node that has been created in the servlet context, using:
Node node = getServletContext().getAttribute("elasticsearchNode")
Finally, you can store the Elasticsearch configuration for your node either /WEB-INF/elasticsearch.json or in /WEB-INF/elasticsearch.yml.
We have an servlet which sends messages to a queue.
It gets the message from a request object it got from Paypal.
The servlet is called IPNServlet.
Note that this is a standalone servlet which is not a part of any MDB.
For some reason the following mapping in my web.xml does not work (requests to this servlet bundled in the WAR file on JBoss EAP 5.1 are not handled)
<servlet-name>IPNServlet</servlet-name>
<servlet-class>com.temenos.paypal.servlet.IPNServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<servlet-mapping>IPNServlet</servlet-mapping>
<url-pattern>/IPNServlet</url-pattern>
By the way of this web.xml, shouldnt a request made to https:// HOSTNM:8443/IPNServlet be processed as expected ?
Where HOSTNM is the name of the IP.
Is there a separate HTTP Listener that is needed ?
I would like to map my servlet to /*, but it failed with an infinite loop.
<servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>my.HelloServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
The java code is:
public class HelloServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response){
request.getRequestDispatcher("/WEB-INF/jsps/hello.jsp").forward(request, response);
}
}
If I map to /hello, everything works fine.
As the HelloServlet is mapped to /*, it will also be invoked on RequestDispatcher#forward() and cause the infinite loop.
How is this caused and how can I solve it?
This is not possible. The JSP should actually invoke container's builtin JspServlet. But the /* mapping definied by the webapp has a higher precedence.
You need to map the servlet on a more specific URL pattern like /pages/* and create a servlet filter which forwards non-static requests to that servlet. Yes, non-static requests (image/CSS/JS files) are also covered by /*, but they should not be processed by the servlet at all.
Assuming that you've all static resources in /resources folder, the following should do:
<filter>
<filter-name>filter</filter-name>
<filter-class>com.example.Filter</filter-class>
</filter>
<filter-mapping>
<filter-name>filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>controller</servlet-name>
<servlet-class>com.example.Controller</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>controller</servlet-name>
<url-pattern>/pages/*</url-pattern>
</servlet-mapping>
With the following in filter's doFilter():
HttpServletRequest req = (HttpServletRequest) request;
String path = req.getRequestURI().substring(req.getContextPath().length());
if (path.startsWith("/resources")) {
chain.doFilter(request, response); // Goes to container's own default servlet.
} else {
request.getRequestDispatcher("/pages" + uri).forward(request, response); // Goes to controller servlet.
}
This takes place fully transparently without any change to /pages in the URL. The forward to the JSP will not trigger the filter or the servlet. Filters do by default not kick in on forwards and the JSP forward path does not match the controller servlet's URL pattern any more.
Alternatively, if you have your own default servlet implementation, then you could map the servlet to / and let it delegate to the default servlet if the request isn't applicable as a front controller request. This is what Spring MVC is doing under the covers. Creating a default servlet is however not a trivial task as it should be capable of responding to conditional requests, caching requests, streaming requests, resume requests, directory listing requests, etecetera.
See also:
Difference between / and /* in servlet mapping url pattern
How to access static resources when mapping a global front controller servlet on /*
Design Patterns web based applications
This is possible duplicate to Servlet Filter going in infinite loop when FORWARD used in mapping in JSF
Otherwise should check what your JSP contains, it could be that it makes request for css or image files that can result in this behaviour. Also would recommend to try without a wildcard.
In struts2 why is a Filter is used as a controller instead of ActionServlet?
What is the advantage of using a Filter over ActionServlet?
As per Struts2 Budi Karnival struts2 book, There is one distinct advantage of using a filter over a servlet as a controller. With a filter you can conveniently choose to serve all the resources in your application, including static ones.
With a servlet, your controller only handles access to the dynamic part of the application. Note that the url-pattern element in the web.xml file in the previous application is
<servlet>
<servlet-name>Controller</servlet-name>
<servlet-class>...</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Controller</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
With such a setting, requests for static resources are not handled by the servlet controller, but by the container. You wouldn't want to handle static resources in your servlet controller because that would mean extra work.
A filter is different. A filter can opt to let through requests for static contents. To pass on a request, call the filterChain.doFilter method in the filter's doFilter method.
Consequently, employing a filter as the controller allows you to block all requests to the application, including request for static contents. You will then have the following setting in your deployment descriptor:
<filter>
<filter-name>filterDispatcher</filter-name>
<filter-class>...</filter-class>
</filter>
<filter-mapping>
<filter-name>filterDispatcher</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Advantage of this filter : One thing for sure, you can easily protect your static files from curious eyes.
The following code will send an error message if a user tries to view a JavaScript file:
public void doFilter(ServletRequest request, ServletResponse response,FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
String uri = req.getRequestURI();
if (uri.indexOf("/css/") != -1 && req.getHeader("referer") == null) {
res.sendError(HttpServletResponse.SC_FORBIDDEN);
} else {
// handle this request
}
}
It will not protect your code from the most determined people, but users can no longer type in the URL of your static file to view it. By the same token, you can protect your images so that no one can link to them at your expense.
Another advantage :
The introduction of Interceptors in Struts2 framework.It not just reduce our coding effort,but helps us write any code which we would have used filters for coding and necessary change in the web.xml as opposed to Struts1.So now any code that fits better in Filter can now moved to interceptors( which is more controllable than filters), all configuration can be controlled in struts.xml file, no need to touch the web.xml file
We generally Use a Filter when we want to filter and/or modify requests based on specific conditions.
For S2 to work it needs to perform certain reprocessing and modification work in order for a successful execution of your request while on other hands we use Servlet when we want to control, preprocess and/or post-process requests.
For controlling request S2 use Servlet under the hood but being hidden away to make the overall application structure more clean and easy to use.
This is what we have for Filters in The Java EE 6 Tutorial.
A filter is an object that can transform the header and content (or both) of a request or response. Filters differ from web components in that filters usually do not themselves create a response. Instead, a filter provides functionality that can be “attached” to any kind of web resource. Consequently, a filter should not have any dependencies on a web resource for which it is acting as a filter; this way, it can be composed with more than one type of web resource.