I'm trying to get freemarker working under Google App Engine. I've gotten simple ftl template files working when requested directly however I'd like to have index.ftl work if available otherwise index.html -- or vice versa.
If I request /index.html, it renders fine as HTML.
If I request /index.ftl, it renders fine as a FreeMarker template. Variables are expanded.
If, however, I request '/' it gives the following 404 message:
Problem accessing /index.html/index.ftl.
I have the following in my web.xml file:
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.ftl</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>freemarker</servlet-name>
<servlet-class>freemarker.ext.servlet.FreemarkerServlet</servlet-class>
<init-param>
<param-name>TemplatePath</param-name>
<param-value>file://ftl</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>freemarker</servlet-name>
<url-pattern>*.ftl</url-pattern>
</servlet-mapping>
</web-app>
I have the following in the war/ directory of my Eclipse GAE application:
/index.html
/ftl/index.ftl
/WEB-INF/...
If I switch the order of the index.ftl and index.html entries, a request for / gives the following 404 message:
Problem accessing /index.ftl/index.ftl.
Thanks for any help.
One additional bit of information is that if I have one <welcome-file> entry of index.html, it works fine. When I add the index.ftl, in any order, is when I get the errors.
Thanks for any help.
I think the problem here is pretty similar the problem of using a struts action as a welcome page.
Quoting Damien B's answer from that question
There isn't ( a better way other than using a jsp redirect). Servlet specifications
(Java Servlet Specification 2.4,
"SRV.9.10 Welcome Files" for instance)
state:
The purpose of this mechanism is to
allow the deployer to specify an
ordered list of partial URIs for the
container to use for appending to URIs
when there is a request for a URI that
corresponds to a directory entry in
the WAR not mapped to a Web component.
Since it is mapped to directory entry and not a mapped web component, the "/" isn't forwarding to the freemarker servlet when index.ftl is the welcome file.
I suggest trying the same approach used to make actions a welcome page. Which is have a jsp fwd to your index.ftl.
I'm still looking for the solution to this (although #Andy Pryor's answer may be ultimately right) but I thought that I'd note what I've done to work around this issue.
I ended up moving all of my html files into the FreeMarker view hierarchy so that all HTML and FreeMarker files are processed by the FreeMarker servlet. I don't have to support *.ftl files since I will never be rendering them directly anyway. So the only files I have in my static hierarchy are images and the like.
This seems to be working well although I had to subclass the FreemarkerServlet to block the getSession() methods made on the request since my app does not have sessions enabled. Here's my web.xml file:
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>freemarker</servlet-name>
<servlet-class>freemarker.ext.servlet.FreemarkerServlet</servlet-class>
<init-param>
<param-name>TemplatePath</param-name>
<param-value>file://views</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>freemarker</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
Related
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.
This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 6 years ago.
I have a servlet register in class p1. I have a JSP jsp1.jsp. I run JSP file and see it, but when I try to apply to the servlet, Tomcat shows an error:
HTTP Status 404
The requested resource (/omgtuk/Register) is not available.
Servlet:
#WebServlet("/register")
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>omgtuk</display-name>
<servlet>
<description></description>
<display-name>register</display-name>
<servlet-name>register</servlet-name>
<servlet-class>p1.register</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>register</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>jsp1.jsp</welcome-file>
</welcome-file-list>
</web-app>
I'm using Eclipse.
The requested resource (/omgtuk/Register) is not available.
This simply means that the servlet isn't listening on an URL pattern of /Register. In other words, you don't have a #WebServlet("/Register").
In your particular case, you made a case mistake in the URL. URLs are case sensitive. You're calling /Register, but your servlet is listening on /register. Fix your form action accordingly.
So, it should not look like this:
<form action="Register">
But it should look like this:
<form action="register">
Or this, which is more robust in case you happen to move around JSPs when you're bored:
<form action="${pageContext.request.contextPath}/register">
Unrelated to the concrete problem, please note that you registered the servlet via both a #WebServlet annotation on the class and a <servlet> entry in web.xml. This is not right. You should use the one or the other. The #WebServlet is the new way of registering servlets since Servlet 3.0 (Java EE 6) and the <servlet> is the old way of registering servlets.
Just get rid of the whole <servlet> and <servlet-mapping> in web.xml. You don't need to specify both. Make sure that you're reading up to date books/tutorials. Servlet 3.0 exist since December 2009 already.
Another detail is that p1 is not a class, it's a package. I'd warmly recommend to invest a bit more time in learning basic Java before diving into Java EE.
See also:
Our servlets wiki page
When using GWT we use this URL forms:
Debug:
http://127.0.0.1:8888/index.html?gwt.codesvr=127.0.0.1:9997#hash
Compiled:
http://127.0.0.1:8888/index.html#hash
I need to be able to access the underlying Rest API.
Which I need to map within the "root"
http://127.0.0.1:8888/{contentId}
However this will sure break the GWT application access, and that it will not be accessible anymore.
How can I make this work together, considering the main requirement to have the Rest API in the root?
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>resteasy.guice.modules</param-name>
<param-value>org.jboss.errai.ui.demo.server.MyModule</param-value>
</context-param>
<listener>
<listener-class>
org.jboss.resteasy.plugins.guice.GuiceResteasyBootstrapServletContextListener
</listener-class>
</listener>
<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
Edit:
The idea is that I should be able to do both:
http://127.0.0.1:8888/index.html#hash
and
http://127.0.0.1:8888/abc123
That is, to co-exist with the "GWT servlet" since both the GWT servlet and the Rest API "servlet" will be deployed in the same server and same webapp path.
Obviously you need to take care of root context in your own servlet. The basic idea is to map YourRoutingServlet to /* path and then handle get/post and other calls by analyzing the request path(gwt or rest) and delegate handling to appropriate servlet.
Depenging on you application specific you can extend e.g. gwt servlet(RemoteServiceServlet) and check request url and if this is a gwt url(with #hash) then just call super method, if not then delegate the req/resp to rest servlet.
This would also mean that you have to have instance of rest servlet in your servlet or you can use servlet redirects(which I would not suggest)
I would like to set the welcome-file of my JSP/JavaBeans project. I have a servlet named 'Controller.java' with the following #WebServlet annotation:
#WebServlet(name="Controller", urlPatterns={"/login", "/show_dbs"})
and I hava a web.xml file with the following content:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<welcome-file-list>
<welcome-file>Controller</welcome-file>
</welcome-file-list>
</web-app>
Almost all things are going well, I can open http://localhost:8080/PROJECT/login and http://localhost:8080/PROJECT/show_dbs and I come to Controller.java. But when I open http://localhost:8080/PROJECT/ I get a 404 error.
I'm using Eclipse with a 'Dynamic Web Project', the Controller.java file is located under /src (default package) and the web.xml file is under /WebContent/WEB-INF.
I hope you have a tip for me.
Thank you for your help. Here comes my solution:
If you want to set your servlet as welcome file you have to do the following:
Define a standard html as welcome-file such as index.html in your web.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
Make sure this file (index.html) doesn't exist.
Define your urlPatterns in #WebServlet like this:
#WebServlet(name="Controller", urlPatterns={"/index.html", "/login", "/show_dbs"})
Now every request to http://.../PROJECT/ (root) will be redirected to http://.../PROJECT/index.html and this calls the servlet.
In the welcome file list you must specify the URIs. But you have specified the name of the servlet.
Quote from the Java™ Servlet Specification version 3.0 (emphasis mine):
10.10 Welcome Files
Web Application developers can define an
ordered list of partial URIs called welcome files in the Web
application deployment descriptor. The deployment descriptor syntax
for the list is described in the Web application deployment descriptor
schema.
The purpose of this mechanism is to allow the deployer to
specify an ordered list of partial URIs for the container to use for
appending to URIs when there is a request for a URI that corresponds
to a directory entry in the WAR not mapped to a Web component. This
kind of request is known as a valid partial request.
The use for this facility is made clear by the following common example: A welcome file
of 'index.html' can be defined so that a request to a URL like
host:port/webapp/directory/, where 'directory' is an entry in the WAR
that is not mapped to a servlet or JSP page, is returned to the client
as 'host:port/webapp/directory/index.html'.
If a Web container receives a valid partial request, the Web container must examine the
welcome file list defined in the deployment descriptor. The welcome
file list is an ordered list of partial URLs with no trailing or
leading /. The Web server must append each welcome file in the order
specified in the deployment descriptor to the partial request and
check whether a static resource in the WAR is mapped to that request
URI. If no match is found, the Web server MUST again append each
welcome file in the order specified in the deployment descriptor to
the partial request and check if a servlet is mapped to that request
URI. The Web container must send the request to the first resource in
the WAR that matches. The container may send the request to the
welcome resource with a forward, a redirect, or a container specific
mechanism that is indistinguishable from a direct request.
If no matching welcome file is found in the manner described, the container
may handle the request in a manner it finds suitable. For some configurations this may
mean returning a directory listing or for others returning a 404 response.
P.S.
Also see the examples in the specification in the chapter 10.10
This is what your web.xml should be. Create an index.jsp. Use the index.jsp as your welcome file. The controller class is your sevlet. So define a servlet in your web.xml as follows. This should cause all requests to be directed to the Controller class. And you should still be able to access localhost:8080/PROJECT/, in which case it will be directed to your welcome file.
If you don't want to create index.jsp, you can use your existing jsp file, may be your login.jsp file as your welcome file. In which case, just replace the index.jsp with login.jsp
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Controller</servlet-name>
<servlet-class>com.company.Controller</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Controller</servlet-name>
<url-pattern>*</url-pattern>
</servlet-mapping>
</web-app>
You can specify the url "/Controller" in urlPatterns in #WebServlet annotation. I think it will work.
The default welcome file is index.html, so just add the URL pattern.
#WebServlet(name="Controller", urlPatterns={"/index.html","/login", "/show_dbs"})
In a JSP page(index.jsp):
${requestContext.requestURL} is the URL
just shows the expression itself. It used to be evaluated to something like "http://.../somerset/"
I created the Maven project with maven-archetype-webapp archetype in Eclipse. The Jetty version is jetty-6.1.14.
My web.xml is simple:
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>SomersetServlet</servlet-name>
<display-name>SomersetServlet</display-name>
<description></description>
<servlet-class>com.foo.somerset.SomersetServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SomersetServlet</servlet-name>
<url-pattern>/som.do</url-pattern>
</servlet-mapping>
</web-app>
See Javascript String.replace(/\$/,str) works weirdly in jsp file for some possible reasons.
Your web.xml should contain reference to web-app_2_4.xsd schema, like
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
This enables servlet 2.4 and jsp 2.0 processing, which includes EL.
Btw requestContext is not valid implicit object.
Incorrectly matched quotes can cause this behavior, where the expression just gets treated as a string. Your IDE would normally highlight this in a different color if this is the case.
Be sure you have directive, and other libraries you use included
<jsp:root .....
More info on definition here
http://java.sun.com/products/jsp/tags/12/syntaxref123.html