I'm using Java server, and I need the sub-folders in the request to act like parameters.
example:
myhost/p/a/1
and I need the server to "understand" it like that:
myhost/p?a=1
How can I do that?
Thanks,
Koby
1: spring 3 mvc #RequestMapping tag can extract path values from uri
#RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
public String findOwner(#PathVariable String ownerId, Model model) {
Owner owner = ownerService.findOwner(ownerId);
model.addAttribute("owner", owner);
return "displayOwner";
}
2: use UrlRewiter: http://www.tuckey.org/urlrewrite/. This can extract path parameters using regexp.
<rule>
<from>^/image/([A-Za-z0-9-]+).html\??(.*)?$</from>
<to>/image.html?imagecode=$1&$2</to>
</rule>
Create a filter -- in that filter getServletPath() then parse the path and forward the request to appropriate controller/servlet
Create a filter at say path /files/* see here,
In this filter add the logic that gets you the whole path after base URL -- i.e. your servlet path see here
You parse this path by splitting using "/" and then pass the array as the parameter to the servlet that want to use this path. see here for forwarding the request
Related
I currently have the following route defined:
from("servlet:///my-api/v1/{param1}?matchOnUriPrefix=true")
.unmarshal().json(JsonLibrary.Jackson, Map.class)
.bean(myController, "myMethod(${in.headers.param1})")
.setHeader(Exchange.CONTENT_TYPE, simple("application/xml"));
This does not work, when the message reaches myController.class, param1 is null.
Is there a way to capture the suffix on a requested URI and then pass it on as a parameter for a bean method?
I know you can do this if you use the restlet component but I would like to use servlet.
No this is not possible, but it could be a nice addition to support. You are welcome to log a JIRA ticket: http://camel.apache.org/support.html
Today you would have to setup the route as
from("servlet:///my-api/v1/?matchOnUriPrefix=true")
and then grab the Exchange.HTTP_PATH header which should be the relative path, eg in your example {param1}.
I have URL like http://localhost:8080/default/j_spring_security_check?redirect=1
I want to know how to configure the URL pattern for the above URL
I have tried already the below
<url-pattern>/*</url-pattern>
<url-pattern>/j_spring_security_check/<url-pattern>
<url-pattern>/j_spring_security_check/*<url-pattern>
<url-pattern>/<url-pattern>
<url-pattern>/default/*<url-pattern>
could you please anyone suggest me the correct url pattern to handle the above url.
If its not possible is there any other way to filter the url...
Thanks in advance
Nithyn K
You cannot filter an URL based on its parameters in the web.xml file. You can only filter on the URL without parameters.
You'll have to do it in the Java code, in a ServletFilter for example or even directly in a Servlet.
Assuming default is the root of your webapp, you can define the url-pattern to /j_spring_security_check/ and match it to a specific Servlet that will check parameters looking into the HttpServletRequest object.
OR
You can write a ServletFilter that will intercept the request before going to a Servlet and check its parameters as well.
EDIT : I didn't notice at first by it looks like you are using Spring and this URL should already be managed by the framework itself.
suppose that I want to accept the following urls:
http://myserver/myapplication/posts
http://myserver/myapplication/posts/<id>
http://myserver/myapplication/posts/<id>/delete
how can I use the servlet decorator #WebServlet to do so? I'm investigating value and urlPatterns but I don't get how to do so. For example,
#WebServlet(urlPatterns={"/posts", "/posts/*"})
[..]
String param = request.getPathInfo();
gives me some result, but how to use it? Also, request.getPathInfo() seems to return the value of the wildcard, but what if I want more parameters like in http://http://myserver/myapplication/posts/<id>/delete/<force>?
In servlet specification, you have no notion of path variables. Some MVC frameworks do support them, for example Struts or Spring MVC.
For a servlet point of view, an URL is :
scheme://host.domain/context_path/servlet_path/path_info?parameters
where any of the parts (starting from context path may be null)
Spec for servlet 3.0 states :
Context Path: The path prefix associated with the ServletContext that this
servlet is a part of. If this context is the “default” context rooted at the base of the
Web server’s URL name space, this path will be an empty string. Otherwise, if the
context is not rooted at the root of the server’s name space, the path starts with a
/ character but does not end with a / character.
Servlet Path: The path section that directly corresponds to the mapping which
activated this request. This path starts with a ’/’ character except in the case
where the request is matched with the ‘/*’ or ““ pattern, in which case it is an
empty string.
PathInfo: The part of the request path that is not part of the Context Path or the
Servlet Path. It is either null if there is no extra path, or is a string with a leading
‘/’.
The following methods exist in the HttpServletRequest interface to access this
information:
getContextPath
getServletPath
getPathInfo
It is important to note that, except for URL encoding differences between the request
URI and the path parts, the following equation is always true:
requestURI = contextPath + servletPath + pathInfo
That means that you just have to use #WebServlet(urlPatterns={"/posts"}), and then decode by hands the pathInfo part to extract commands and parameters
I think you cannot do so using only the #WebServlet annotation. The urlPatterns only acts as a directive to the Servlet to indicate which url patterns should attend.
And as you can see by this docs https://docs.oracle.com/javaee/6/api/javax/servlet/annotation/WebServlet.html the value is just the case when urlPatterns is one string instead of an array of them.
As brso05 stated, you will need to parse from the request your parameters.
I have a web application written in Java which uses Struts 1.0. Sometimes when a URL is fired, I can see that it one of the request parameters does not have any name and value like the following ...
http://www.aaa.com/test.do?a=1&b=2&=&d=4&e=5
As can be seen, there is a '&=' which is essentially a parameter with no name and value. I'd like to remove this part from the URL before sending the request to the server. How can I achieve this? Should I use a filter or is there an easier way?
I want to allow users to create groups in my application and access them via URL. For example, if you made a group called "sweethatclub," you could access it at http://mysite.com/sweethatclub. Of course, the same code will run for /sweethatclub and /drillteam and even /students/yearbook
I'm running in a Java servlet environment, and can't quite get the paths to align for this. I can write a filter that intercepts all requests and adds information to the request by parsing the URL, but then I want to run the code of an index.jsp. I don't want to map index.jsp to all URLs, because, for example, /images/smiley.jpg still needs to respond the with appropriate file instead of index.jsp.
Is there a way to send all requests to a servlet, unless the request is matched by a plain-old file? Or, is there some other way to accomplish what I want here?
Please let me know if I need to supply more information. I'm new to this environment.
The URL patterns in the web.xml are not supposed to be smart enough to figure out target URL's nature. If you can tolerate it, the easiest way would be to place all the user specified paths under a a well known root... someplace separate from the static files. So you end up with user specified paths like http://mysite.com/sites/sweethatclub.
Alternatively, you can move all your static content under http://mysite.com/static/, and set up the servlet mappings or filters to treat anything starting with 'static' different from the dynamic URL space.
If you are in a Unix invironment, you could just create all the "group sites" as virtual directories that just point to your default one.
Map the servlet on a specific URL pattern
<url-pattern>/groups/*</url-pattern>
Put all static content in a common folder, e.g. /static and fix all URLs in the pages to point to that URL instead.
Create a filter which is mapped on
<url-pattern>/*</url-pattern>
and does the following job in doFilter() method
String uri = ((HttpServletRequest) request).getRequestURI();
if (uri.startsWith("/static/")) {
chain.doFilter(request, response); // Goes to default servlet.
} else {
request.getRequestDispatcher("/groups" + uri).forward(request, response);
}
No, this does not end up with /groups in browser address bar URL. It's fully transparent. You can if necessary make "/static" and/or "/groups" an <init-param> of the filter so that it's externally configureable.