How to get my app-engine home page from a Servlet - java

I have created an app-engine connected android project. The way this works is when you open your webapp directory there is an index.html, which serves as the home page of the api. What I want is to create a Servlet that will generate that home page for me instead of serving it from this webapp/index.html. I have created my servlet. But I am not sure how to refer to it inside web.xml
I imagine I have to replace
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

If you want to keep it simple you can remove the static index.html altogether and create a servlet mapping like this (in web.xml).
<servlet>
<servlet-name>MyWelcomeServlet<servlet-name>
<servlet-class>domain.package.subpackage.class</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyWelcomeServlet</servlet-name>
<url-pattern>/index.html</url-pattern>
</servlet-mapping>
Note: removing or renaming the static index.html is important since Google's content delivery network will serve static files before routing requests to app engine instances.

Related

Root "/" mades server load index.html before pass through the servlet

I have a Servlet that is mapped to the root directory "/":
<servlet>
<servlet-name>Main</servlet-name>
<servlet-class>com.motorola.triage.MainServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Main</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
In this servet there is a couple of small things that are done there, like authentication and retrieve of Google Plus information. After that, I'm doing a forward to a JSP file called "index.jsp"
req.getRequestDispatcher("index.jsp").forward(req,resp);
When I'm accessing "localhost:8080/" the static file "index.jsp" is loaded without passing through the servlet. For architecture reasons I can not change the name of index.jsp. I would like to ask if is there any way to change this behavior of the server and make it look to the servlet before look the index.jsp file.
This is occurring specifically because you used the name index.jsp.
This has been covered elsewhere, such as here and here and here.

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.

Change landing page of web app from login page to some other page [duplicate]

I am just getting started to learn about Web Apps and deploying them to Tomcat. So I started with a sample web app project - made up of struts, hibernate, etc., etc.
The ANT build was successful. Also, was able to deploy the web app through an xml under Catalina/host. I am able to open the web site with no issues.
This is the structure of my web app
-exploded
-WEB-INF
-classes
-lib
-web.xml
-index.jsp
-welcome.html
My question is
How does Tomcat know which is the first page / starting page / home page that it is supposed to open? Which file is this specified in?
In any web application, there will be a web.xml in the WEB-INF/ folder.
If you dont have one in your web app, as it seems to be the case in your folder structure, the default Tomcat web.xml is under TOMCAT_HOME/conf/web.xml
Either way, the relevant lines of the web.xml are
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
so any file matching this pattern when found will be shown as the home page.
In Tomcat, a web.xml setting within your web app will override the default, if present.
Further Reading
How do I override the default home page loaded by Tomcat?
I already had index.html in the WebContent folder but it was not showing up , finally i added the following piece of code in my projects web.xml and it started showing up
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

app engine simple java webservice

I like to expose one JAVA method as a Web service that will accept POST ,strip the parameters out of it and reply accordingly. I read I have to use doPost(req,resp) , but How can I get to the servlet code? what should be in web.xml? there will not be a welcome-file ? After mapping the servlet, can I read it without the need for a index.html as start point?
create the doPost(req,resp) method in your servlet and map it to a url in web.xml
<servlet>
<servlet-name>HelloPost</servlet-name>
<servlet-class>packageName.HelloPost</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloPost</servlet-name>
<url-pattern>/post-url</url-pattern>
</servlet-mapping>
then you can post your request to /post-url .You don't need to use index.html.Any url can be put in welcome file to load for the url /
To set /post-url as landing page , use
<welcome-file-list>
<welcome-file>/post-url</welcome-file>
</welcome-file-list>
you can get started here https://developers.google.com/appengine/docs/java/gettingstarted/creating
If you want to know how to set the web.xml to start the servlet then may be this will help you.

redirecting requests based on the body tomcat

I have a tomcat 7 application which I can get requests from external sources.
Most of them call my request like this:
http://localhost:8080/MyWeb/exRequest
and I build servlet with URL pattern inside MyWeb app.
However, one external source must send the request like this:
http://localhost:8080/
and in the body:
<xml name="test" />
Since I don't want to declare a general servlel (like tomcat default) since it means that any request will need to go through my servlet, I thought to change index.jsp of ROOT to redirect to my servlet.
Is it the best option?
Is there an option to create a default servlet that will be invoked only if there is a special parameter in the body?
EDITED
Please note that I get the requests to localhost:8080 and not localhost:8080/MyWeb - it's general to tomcat and not to a specific web app
You can't choose a servlet to invoke based on the request body, but you can set a servlet as the "welcome-file" in your web.xml.
<servlet>
<servlet-name>index</servlet-name>
<servlet-class>com.y.MyWelcomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>index</servlet-name>
<url-pattern>/index</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index</welcome-file>
</welcome-file-list>
If you want to preserve the "welcome" function of some existing index.jsp, your servlet could forward requests without the correct XML in the body to an index.jsp file located under the WEB-INF directory.
No, but you can create a Filter and forward/redirect to a specific servlet whenever the request meets certain conditions.
If using servlet 3.0 map it with #WebFilter, otherwise use web.xml and <filter> + <filter-mapping>. You should map it be executed before the default servlet.

Categories