Reserve words in web.xml of servlet? - java

I'm fairly new to servlets so I hope this isn't an obvious question. So I have a simple Java servlet I've created in NetBeans using a template. I have a context parameter I created in web.xml that lists allowed hosts (one of my request parameters is a URL which I'll compare against this list):
<context-param>
<param-name>allowedHosts</param-name>
<param-value>
http://opendap.co-ops.nos.noaa.gov/thredds/wms/NOAA/CBOFS/MODELS/201206/nos.cbofs.fields.nowcast.20120612.t00z.nc?service=WMS&version=1.3.0&request=GetCapabilities
http://www.google.com
http://www.facebook.com
</param-value>
</context-param>
When I put only dummy URLs in like Google and Facebook, this works perfectly. However, when I add the first URL, the Tomcat server cannot even deploy. Looking at my logs, I see this at the top of a very long stacktrace:
SEVERE: Parse Fatal Error at line 19 column 146: The reference to entity "version" must end with the ';' delimiter.
org.xml.sax.SAXParseException: The reference to entity "version" must end with the ';' delimiter.
Line 19 column 146 indeed points to the "version" portion of that long URL I have in the list of context parameters. So obviously "version" is some kind of reserve word. If I remove the "version" parameter from this URL, "request" is also a problem.
I can get around this by doing just http://opendap.co-ops.nos.noaa.gov/thredds/wms/ as the URL (because ultimately I want a list of hosts not specific URLs anyway), but I was wondering what one would have to do to get around this otherwise... Is there a way to include URLs that have such "reserve words" in web.xml?
Thanks!

Just put "&" (without the quotes) instead of the ampersands in your URL.
Hope this helps - DF
Edit: Try this:
<context-param>
<param-name>allowedHosts</param-name>
<param-value>
http://opendap.co-ops.nos.noaa.gov/thredds/wms/NOAA/CBOFS/MODELS/201206/nos.cbofs.fields.nowcast.20120612.t00z.nc?service=WMS&version=1.3.0&request=GetCapabilities
</param-value>
Edit2: PS These things are called "entities" in XML - do a Google for more info

Related

how can i use wild card to map url in java servlet

I want to achieve this:
http:\\localhost:8080\mysite\search\cotton\search.html
http:\\localhost:8080\mysite\search\bean\search.html
http:\\localhost:8080\mysite\search\cosmetic\search.html
http:\\localhost:8080\mysite\search\shoe\search.html
<servlet-mapping>
<servlet-name>abcSearch</servlet-name>
<url-pattern>/search/*/search.html</url-pattern>
</servlet-mapping>
mean one pattern for the above all urls
can any one help out for me???
The rules for mappings are as follows
In the Web application deployment descriptor, the following syntax is
used to define mappings:
A string beginning with a ‘/’ character and ending with a ‘/*’ suffix
is used for path mapping.
A string beginning with a ‘*.’ prefix is used as an extension mapping.
The empty string ("") is a special URL pattern that exactly maps to
the application's context root, i.e., requests of the form
http://host:port/<context-root>/. In this case the path info is ’/’
and the servlet path and context path is empty string (““).
A string containing only the ’/’ character indicates the "default"
servlet of the application. In this case the servlet path is the
request URI minus the context path and the path info is null.
All other strings are used for exact matches only.
So this
/search/*/search.html
would match exactly
http://host/context/search/*/search.html
You can't get path matching at the middle of the path with Servlet's url-patterns.
If you only have the 4 paths, I recommend you put 4 <servlet-mapping> element with each exact path match.
Try to change URLs like:
http:\\localhost:8080/mysite/search/cotton
http:\\localhost:8080/mysite/search/bean
http:\\localhost:8080/mysite/search/cosmetic
http:\\localhost:8080/mysite/search/shoe
or like that:
http:\\localhost:8080/mysite/cotton/search.html
http:\\localhost:8080/mysite/bean/search.html
For the first case pattern will be <url-pattern>/search/*</url-pattern> and for the second one <url-pattern>*/search.html</url-pattern>

JSP/Servlet rewrite URL

I'd like to change the URL of some pages in my website in the same way as foursquare is doing:
from www.foursquare.com/v/anystring/venueid
to www.foursquare.com/v/venue-name/venueid
For example central park in new york:
https://foursquare.com/v/writeherewhatyouwant/412d2800f964a520df0c1fe3
becomes
https://foursquare.com/v/central-park/412d2800f964a520df0c1fe3
I'm developing a pure JSP/Servlet app, no frameworks, in a Tomcat container.
I'm thought of using tuckey's urlrewritefilter, but I don't see how can I use dynamic values coming from the servlet itself there (the venue name)
How can I accomplish this?
Off the top of my head, here's something you could try:
1) Create a servlet with a servlet-mapping matching the common (prefix) part of the URL (e.g. for foursquare the pattern would be /v/*).
2) In your servlet, retrieve the remaining part of the URL path using request.getPathInfo(). You can then parse it using regular string utilities and convert it to the new path you'd like.
3) Assuming your updated path is in a variable called newUrl, call response.sendRedirect(newUrl) to tell the browser to update its URL. This will also call your servlet again with the new path, so it needs to handle both cases.
See the javadoc for HttpServletResponse.sendRedirect() for more info about how it handles relative vs absolute paths, etc.

Dispatcher servlet spring and url pattern

I'm new to spring framework I today I ran into dispatcher servlet configuration in web.xml file and i came up with a question concerning url pattern like this syntax /. So what does actually the "/" symbol apply in case I deploy web application in tomcat server as following: host:port/ or host:port/myWeb/
The pattern / will make your servlet the default servlet for the app, meaning it will pick up every pattern that doesn't have another exact match.
URL pattern mapping :
A string beginning with a / character and ending with a /* suffix is used for path mapping.
A string beginning with a *. prefix is used as an extension mapping.
A string containing only the / character indicates the default servlet of the application. In this case the servlet path is the request URI minus the context path and the path info is null.
All other strings are used for exact matches only.
Rules for path mapping :
The container will try to find an exact match of the path of the request to the path of the servlet. A successful match selects the servlet.
The container will recursively try to match the longest path-prefix. This is done by stepping down the path tree a directory at a time, using the / character as a path separator. The longest match determines the servlet selected.
If the last segment in the URL path contains an extension (e.g. .jsp), the servlet container will try to match a servlet that handles requests for the extension. An extension is defined as the part of the last segment after the last . character.
If neither of the previous three rules result in a servlet match, the container will attempt to serve content appropriate for the resource requested. If a default servlet is defined for the application, it will be used.

GlassFish 3.1 and the tag <url-pattern> of web.xml file

I read that into the tag <url-pattern> I can write a prefix or a suffix pattern url.
But if I try to write something like:
<url-pattern>*sde</url-pattern>
or
<url-pattern>/sde*</url-pattern>
and try to get the url as polsde alsde or sdepp sdelop I have a 404 error and a deploy failed
into server log.
What's wrong?
URL-pattern is wrong.
The pattern you have supplied is invalid
The url-pattern specification:
A string beginning with a ‘/’ character and ending with a ‘/*’
suffix is used for path mapping.
A string beginning with a ‘*.’ prefix is used as an extension
mapping.
A string containing only the ’/’ character indicates the "default"
servlet of the application. In this
case the servlet path is the request
URI minus the context path and the
path info is null.
All other strings are used for exact matches only.
Please refer Java Servlet Specifications
Interesting question! From reading the 3.0 servlet spec, it doesn't look like the wildcard in servlet mappings works the same way as a regex wildcard; there are boundaries to the mapping.
*.sde would be a valid mapping.
So would "/sde/*", but I don't see any mention of embedded wildcards as you're using them. For the Servlet 3.0 specification, see section 12.2.

Java and SEO URLS

I'm building a webapp using spring MVC and am curious as to whether there is any clean way to make SEO urls.
For example, instead of http://mysite.com/articles/articleId and the such, have:
http://mysite.com/articles/my-article-subject
If you're using the new Spring-MVC annotations, you can use the #RequestMapping and #PathVariable annotations:
#RequestMapping("/articles/{subject}")
public ModelAndView findArticleBySubject(#PathVariable("subject") String subject)
{
// strip out the '-' character from the subject
// then the usual logic returning a ModelAndView or similar
}
I think it is still necessary to strip out the - character.
This might be of interest to you:
http://tuckey.org/urlrewrite/
If you are familiar with mod_rewrite on Apache servers, this is a similar concept.
http://mysite.com/articles/my-article-subject is a much stronger URL than http://mysite.com/articles/articleId - especially if the title and header tags match "my-article-subject" too and you have "my", "article" and "subject" in the content of the page.
For example if you want the url
http:///blog/11/12/2009/my-hello-world-post/
then configure the servlet mapping
<servlet>
<servlet-class>com.blog.Blog</servlet-class>
<servlet-name>blog</servlet-name>
<servlet-class>com.blog.Blog</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>blog</servlet-name>
<url-pattern>/blog/*</url-pattern>
</servlet-mapping>
and in the servlet code
String url = request.getPathInfo();
StringTokenizer tokens = new StringTokenizer(url,"/");
while(tokens.hasMoreTokens()){
out.println(""+tokens.nextToken());
}
Use these params to get the data from database and display to user
The standard Java web frameworks are not ready for those kind of URL.
AFAIK, SpringMVC does not support this kind of URL.
There are two frameworks I'm sure that support this kind of URL: Mentawai and VRaptor.
If you're only looking for a SEO optimization you could design your URLs this way:
http://mysite.com/articles/my-article-subject/articleId
or
http://mysite.com/articles/articleId/my-article-subject
and just ignore the part my-article-subject when evaluating the urls.
Amazon does something like that with their URLs:
http://www.amazon.com/Dark-Crystal-Jean-Pierre-Amiel/dp/B00000JPH6/ref=sr_1_1?ie=UTF8&s=dvd&qid=1240561659&sr=8-1
Here the Text "Dark-Crystal-Jean-Pierre-Amiel" is totally irrelevant because the article is identified by the id B00000JPH6.
Edit: In fact I just noticed that right here on SO this exact technique is used to generate SEO-friendly URLs...
I have used pretty faces http://ocpsoft.org/prettyfaces/ for our application because it was JSF based. This is a very neat solution.
Not sure if it will work for Spring MVC
Have a look at our URL
http://www.skill-guru.com/cat/certification-mock-test
http://www.skill-guru.com/test/81/core-spring-3.0-certification-mock
http://www.skill-guru.com/tutor/Pro+ESL
Earlier we had non SEO friendly URL's with jsession Id's appended to it. Now it is all neat and clean with the help of pretty filter.
This is in very in line with wordpress url.
generate url containing both id and description like this url http://stackoverflow.com/questions/784891/java-and-seo-urls . in the servlet parse the url and then use id for fetching data from database. Same Technique is applies on this stackoverflow page too. look at the url. its http://stackoverflow.com/questions/784891/java-and-seo-urls
however only questionId is considered and description is ignored. try http://stackoverflow.com/questions/784891 or http://stackoverflow.com/questions/784891/abcdxyz . you will get same page. this is very good technique to generate seo urls

Categories