Java url-pattern all subdomains redirect - java

is there any possibility in Java's web.xml of redirect all subdomains to one servlet?
For example:
<url-pattern>*.</url-pattern>

You can definitely do that but I think you would like to define your URL pattern using *.ext or *.* as below:
<url-pattern>*.*</url-pattern>
This is also used by some populare MVC frameworks such as struts where UL pattern
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
is mapped to Struts controller servlet.

Related

How to intercept a url and redirect it to a jsp page?

I want to develop a application and deploy in WebSphere where the requirement is:
if there are any request like http://appserver1:9080/ - it will reach to a landing jsp page
for example http://appserver1:9080/index.jsp
Is it at all possible to redirect to a page even if I don't mention
the resource name?
If you want to redirect from server's root, this is not about your java code or project configuration, it is about server configuration.
Look here for WebSphere configuration.
For JEE projects, on web.xml, you can define as;
<web-app>
....
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
</welcome-file-list>
</web-app>
So
http://localhost:8080/myproject
will load index.jsp
Source for details
From what you are describing, the redirection can be pretty much handled by servlets mapping. Read here:
https://docs.oracle.com/cd/E13222_01/wls/docs92/webapp/configureservlet.html
You can be able to intercept URL requests and process:
// Servlet definition on your web.xml
<servlet>
<servlet-name>ServletHandler</servlet-name>
<servlet-class>com.servlets.ServletHandler</servlet-class>
</servlet>
// This maps all requests to the above defined servlet for processing:
<servlet-mapping>
<servlet-name>ServletHandler</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
I hope this helps
You can define welcome files in web.xml
<web-app>
...
<welcome-file-list>
<welcome-file>index.jsp/welcome-file>
</welcome-file-list>
...
</web-app>
But according to the spec index.html, index.htm and index.jsp are welcome files by default. So you probably don't need to configure anything when the file is called index.jsp.

How to use DispatcherServlet in tomcat with multiple servlets

I am working with a tomcat app with multiple servlets.
I want to be able to initialize these and do dependency injection on server start up.
I understand that I will have to declare a org.springframework.web.servlet.DispatcherServlet .
But I am not sure how should my web.xml look like. Currently it looks like following:
<servlet>
<servlet-name>AddAccount</servlet-name>
<servlet-class>com.addressbook.servlets.AddAccount</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AddAccount</servlet-name>
<url-pattern>/AddAccount</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>com.addressbook.servlets.Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/Login</url-pattern>
</servlet-mapping>
Currently, any request to add an account is sent directly to /AddAccount and for login the request is sent to /Login.
With DispatcherServler, how should my new web.xml and request structure look like? Do I have to make a new servlet that implements DispatcherServlet and forward each request to this new servlet which then forwards to correct Servlet?
You dont need multiple servlets for several kind of actions in your app. The DispatcherServlet is a front controller that handles all requests and dispatches them to your controllers. As Sotirios suggested, have a look at the Spring MVC manual first. Only the Dispatcher servlet is neccessary.

App engine default page to be servlet

I have App engine application, with that servlet:
<servlet>
<servlet-name>Authorization</servlet-name>
<servlet-class>x.y.z.Authorization</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Authorization</servlet-name>
<url-pattern>/pattern</url-pattern>
</servlet-mapping>
everything works.
servlet invokes with this pattern http ://localhost/pattern
now I want to create AutorizationServlet default page. I need to invoke that servlet with this pattern:
http ://localhost
if I write <url-pattern></url-pattern> I have AppEngineConfigException:
Try this :
<url-pattern>/</url-pattern>

Why use <servlet-mapping> in web.xml(Deployment Descriptor) in jsp/servlet?

<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/HelloWorld.do</url-pattern>
</servlet-mapping>
Why do we use url-pattern inside servlet-mapping tag. Why not in servlet tag itself.
It's seems just an extra tag to write.
Is it just because servlet/jsp spec writers decided to do so or has it some logical reason behind its existance ?
This is more likely due to the fact that servlets were intended to support multiple protocols, and not just HTTP. URL patterns are specific to HTTP alone, and therefore the mapping of the servlet to HTTP URL patterns is done in a servlet-mapping tag, instead of the servlet tag which is used for declaring the more generic properties of the servlet.
u may try to write another that also linked to the same servlet,and then u will know that the servlet can have more than one servlet-mapping.
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/HelloWorld.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/HelloWorld2.do</url-pattern>
</servlet-mapping>
I agree with Vineet Reynold that servlet-mapping is used to provide support for different protocols available for communication in network. therfore, url-pattern tag let know the servlet the type of protocol ie. HTTP requesting the service.
The limitations and weaknesses of the standard servlet url pattern was a driving factor behind many of the early MVC frameworks (Struts etc). No support for regular expression matching, or even ant-style patterns. No support for url exclusion patterns etc etc.
As to why the specification is the way it is (and still continue to be), I have no idea. The only people who know for sure are the ones who wrote the specs.

Hide .jsp extension in JSP project using servlet mapping

I am writing an application in JSP, and I need to remove the ".jsp" extension from the URL. For example, I need:
http://example.com/search.jsp?q=stackoverflow
To be:
http://example.com/search?q=stackoverflow
I know that this can be done using the ".htaccess" file, but I need some other way. I have tried the following code:
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.</url-pattern>
</servlet-mapping>
However, this did not work. Does anyone have some suggestions for ways to accomplish this? Thanks in advance for any help.
With a servlet mapping you need to specify each JSP individually like follows:
<servlet>
<servlet-name>search</servlet-name>
<jsp-file>/search.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>search</servlet-name>
<url-pattern>/search</url-pattern>
</servlet-mapping>
It's easier if all those JSPs are in a common path. E.g. /app/*.
<servlet>
<servlet-name>app</servlet-name>
<servlet-class>com.example.FriendlyURLServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
with
request.getRequestDispatcher("/WEB-INF" + request.getPathInfo() + ".jsp").forward(request, response);
This assumes the JSPs to be in /WEB-INF folder so that they cannot be requested directly. This will show /WEB-INF/search.jsp on http://example.com/app/search.
Alternatively, you can use Tuckey's URLRewriteFilter. It's much similar to Apache HTTPD's mod_rewrite.

Categories