EL in a JSP stopped evaluating - java

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

Related

welcome-file-list not working in jetty + spring

I'm using Jetty 8.1.4 with Spring 3.2.4. Following is my web.xml file. I have an index.html file under WEB-INF and I want that page to be hit when I do http://myapp.com/ or simple http://myapp.com but I'm getting 404. If I do http://myapp.com/index.html it works. I'm not sure what I'm missing. Also, I'm bit confused if I must use / or /* in the url-pattern below, I tried both.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
id="DOMAINAPPROVALGUI" version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>myapp-ui</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/myapp-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<session-config>
<session-timeout>10</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>myappname</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myappname</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
Jetty 8 is EOL (End of Life), upgrade to Jetty 9. (The answer here is provided based on how Jetty 9 operates.)
The <welcome-file-list> is part of the DefaultServlet handling (per servlet spec).
Your declaration of myappname at <url-pattern>/*</url-pattern> is effectively preventing the DefaultServlet from doing anything.
Your configuration has basically said "send all requests to my DispatcherServlet".
This even includes static file serving, welcome-file handling, default handling, error handling, dispatching, and much much more.
As for what url pattern to choose, that's up to you.
There are many ways to use Spring, your current configuration at /* is just as valid as others that have it at *.do or *.dispatch or /dispatch/*
You have to decide what is best for your webapp, and adjust your internal use of Spring to satisfy your needs (such as how you declare your RequestMapping's)
Now that you know why <welcome-file-list> isn't working, you can make adjustments to either not use the standard servlet <welcome-file-list> (using something internally in Spring), or adjust your dispatcher servlet url pattern to allow the servlet container (Jetty) to serve your static files and handle your declared <welcome-file-list>.
I added below to my servlet.xml to make this work. Agree with #Joakim Erdfelt answer that request went to spring. But not sure how directly entering index.html in url worked.
<mvc:view-controller path=”/” view-name=”index” />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".html" />
</bean>
Firstly, Your servlet mapping config of spring is not right , it will mapping all thing and link to spring , this means that *.jsp would be handled by spring. So You Should fix this flaw config. But after doing this, it still would not be right to access the welcome page. this is a character of Jetty.
I Must Say: there is some difference between tomcat and jetty. Tomcat can handle this with right behaviour but jetty not.
You can try to verify by below step.
delete the mapping with "/"
and run the app with tomcat and jetty. you can find out tomcat and jetty both works.
if you add a servlet mapping with mapping pattern : "/" ,either with a customer servlet or spring dispatch servlet. it will work right in tomcat but jetty not.
i think placing you welcome file at the start will help you to load the file.To be precise place it after the display tag

Spring MVC url pattern - keeps appending the controller

I have a Spring MVC app.
This is the web.xml
<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
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring Web MVC Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
I have a sample page Controller (TestController). with request mapping
#RequestMapping("/Test")
class TestController{
}
Am calling the controller using Test
When I click the link first time, its working fine
http://localhost:8008/App/Test
If i click the link, once again, it is appending Test once again
http://localhost:8008/App/Test/Test
and it keeps adding.
What could be the issue!
Instead of having
link
in your JSP, you should have
link
(and of course add the JSTL core taglib definition to the head of the JSP).
This will make use of an absolute URL (/App/Test) rather than a relative one (Test), and will automatically prepend the context path of the application (/App in your case) to the URL. This link can be used from anywhere in the application, and will always poit to your controller, whatever the URL of the current page is.
Another way is to use
link
but it's longer, less clean, and doesn't allow adding parameters to the URL like c:url does. Note that Spring also has an s:url tag that does the same thing, and more.
An addition to #JB Nizet's answer, since it is not clear which viewResolver you are using and there might be other people looking for an answer to that question:
if you are using freemarker as a template engine, you can do that:
link
in your templates. This will let spring create the correct context path for you.
url is the name of the macro here, and spring the reference name of the template.
Note: you have to import the spring.ftl-template for that beforehand, like so
<#import "spring.ftl" as spring/>
before you can use the macro.

Make GWT app run side by side with a Rest API (Deploy Resteasy in Root)

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)

#WebServlet annotation web.xml welcome-file

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"})

freemarker cannot handle request for '/' on google app engine

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>

Categories