App engine default page to be servlet - java

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>

Related

Serving single page app next to other war file in Tomcat

I have a Tomcat server, serving two .war files. The first one is mapped to context /api, the second one to root /. The latter contains a single-page AngularJS app. It has the following web.xml config:
<servlet>
<servlet-name>webapp</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>webapp</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/</location>
</error-page>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
When I go to https://my.url/, the index page of the single page app is properly served. So far, so good.
The problem is, when I deeplink to a page in my single page app, for example https://my.url/some/resource, Tomcat will give a 404. Because of the error-page config, it will still return the index page, but still with status 404. So, it kind of works, but not nicely.
Can I get Tomcat to return the index page with a proper 200 status code for all deep links? Of course, calls to /api should still resolve to the other deployed .war. I want to avoid duplicating the AngularJS url definitions in Tomcat, so it should just return the index page for any request that doesn't start with /api/.
For deeplinks you need to use wildcards on your webapp url pattern. Something like:
<url-pattern>/*</url-pattern>
Not sure would it not catch the /api/ calls then. Some more advanced url pattern might be required.

doGet Method of the Faces Servlet

I'm using Apache Tomcat 7.0 Servlet Container. I've been trying to look at the request handling in JavaServer Faces. I can see the following chunk of config in the web.xml :
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
I've looked at the source of the Faces Servlet, but I haven't found doGet method inside. I thought doGet method is one of the principal method to handle HTTP GET request in the Java Servlets. So who exact handles incoming GET request in JSF? I would like to look at the method, which do that.
FacesServlet doesn't extend from HttpServlet class containing a.o. doGet(). It just implements Servlet interface which offers the base service() method. Look here.
JSF is designed to be compatible with both servlets and portlets. Portlets doesn't use HttpServlet, but PortletServlet which shares the common Servlet interface.

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.

Hide servlet name in the url

In a website I have some links in a navbar like this
<li>PAGE</li>
and I load the linked page with JAVA in JSP page, then I submit data through a form by GET, after this my URL become
www.sitename.com/Servlet
then I'm not able to load the page by
?page=pagename
because I have the Servlet name in the URL.
How I can hide or delete the servlet name?
Use a servlet-mapping in your web.xml:
<servlet-mapping>
<servlet-name>Servlet</servlet-name>
<url-pattern>/*</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.

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