Google App Engine JSP - java

I have created a Google App Engine project, but because of some SEO concerns I want to change one of my pages from HTML (+ JQuery) to a JSP that gets rendered on the server
This page is the index.html file, how can I make it work as a JSP without renaming it (I don't want the user to go to index.jsp, but instead treat index.html as a JSP page)
I've tried adding this to my web.xml, but it doesn't seem to work
<servlet>
<servlet-name>main</servlet-name>
<jsp-file>/index.html</jsp-file> (or index.html, same result)
</servlet>
Any ideas on how to solve this ?
If I rename the index.html to index.jsp file, everything works fine

You can definitely do this in a Servlet filter.
Set up your filter to catch requests to /index.html
Then in the filter return index.jsp so it is seen by the client as /index.html
ex:
private ServletContext context;
#Override public void init(FilterConfig arg0) throws ServletException {
context = arg0.getServletContext();
}
#Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
context.getRequestDispatcher("/index.jsp").include(request, response);
}
What this does is include /index.jsp in the response. Of course, since you don't have a /index.html file then that ends up being the whole response.

Related

access WEB-INF/jsp from html

I have a simple application to test the communication between html and jsp. My jsp is located in
WEB-INF/test.jsp
Here is the structure of my files:
ProjectA
src
irstServlet.java
Web-Content
test1.html
WEB-INF
test.jsp
Here is the code from servlet
protected void doPost(HttpServletRequest request, response) throws ServletException, IOException {
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/test.jsp");
request.setAttribute("userName", request.getParameter("userName"););
dispatcher.forward(request, response);
}
First I have deploy in tomcat start my test1.html: It take me to the servlet: FirstServlet.java and I can enter userName there.
But after i enter the values in and press enter I expect it to forward me to test.jsp which is not working. I get the error:
The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Edited:
In my html I am trying to use it like:
<form method="POST" name="XX" action="/HelloWorldServlet">
Still not working.
Please can someone help me?
Your code does not look like it would compile at all.
Parameter response has no type - should be HttpServletResponse
There is a semicolon (;) after request.getParameter("userName")
Also I'm not sure why you're getting RequestDispatcher from servlet context rather than from the request - then again I've never checked if it makes any difference.
Anyway, I would rewrite doPost method like this:
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setAttribute("userName", request.getParameter("userName"));
req.getRequestDispatcher("/WEB-INF/test.jsp").forward(req, resp);
}
EDIT:
I'm assuimng you have either a correct servlet mapping in your web.xml:
<servlet>
<servlet-name>HelloWorldServlet</servlet-name>
<servlet-class>FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorldServlet</servlet-name>
<url-pattern>/HelloWorldServlet</url-pattern>
</servlet-mapping>
or your servlet is annotated with #WebServlet annotation:
#WebServlet("/HelloWorldServlet")
public class FirstServlet extends HttpServlet {
//your code
}
. If neither of those is true, that's your problem right there.

Skip request filter for specific resource in jersey

Iam working on a jersey project and Iam using token authentication and am using ContainerRequestFilter for filtering all the request and checking whether it has a token with it, but the requests includes Login and registration request, but we need to skip these request.. How i can skip the filtering for login and registration requests? Is there any mechanism in jersey for achieving this?
thank you
As far as I know, there is no facility fo such a behavior using a raw deployment descriptor (web.xml).
However, if that was a custom filter, you can get it skip the excluded paths using a simple check on request url in your doFilter() method. But since you are using a third party filter, that won't be the way to go with but still can have this functionality achieved:
Change your third party filter (ContainerRequestFilter) mapping to another path rather than a wildcard one:
<filter-mapping>
<filter-name>containerRequestFilter</filter-name>
<url-pattern>/tokenizedpaths/*</url-pattern>
</filter-mapping>
Declare a new filter (you will see in a moment what it looks like), that will be mapped to a wildcard path to filter all requests and be delegated to dispatch requests to your containerRequestFilter only if the request path does not match the excluded path(s) (I've choosen register as a sample):
<filter>
<filter-name>filteringFilter</filter-name>
<filter-class>com.sample.FilteringServletFilter</filter-class>
<init-param>
<param-name>excludedPaths</param-name>
<param-value>/register</param-value>
</init-param>
</filter>
The FilteringServletFilter will look like somthing like the following:
public class FilteringServletFilter implements Filter {
private List<String> excludedPaths = new ArrayList<String>();
public void init(FilterConfig config) throws ServletException {
// You can declare a comma separated list to hold your excluded paths
this.excludedPaths = Arrays.asList(config.getInitParameter("excludedPaths").split(","));
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
String path = ((HttpServletRequest) request).getRequestURI();
// If the url is one of excluded paths, then just continue with next filter
if (this.excludedPaths.contains(path)) {
chain.doFilter(request, response);
return;
}
// Otherwilse, forward the request to the needed filter
else {
request.getRequestDispatcher("/tokenizedpaths" + path).forward(request, response);
}
}
}

Filter on static content outside tomcat webapp directory

Im using tomcat 7.0 and in my server.xml I have this:
<Context docBase="C:/xampp/tomcat/temp" path="/testapp/files" />
to allow the user to download files that are placed in the /temp directory. However, I would also like to limit some users from downloading files depending on their user-role. I describe my filter in webapp/WEB-INF/web.xml like this:
<filter>
<filter-name>DownloadFilter</filter-name>
<filter-class>Main.downloadFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>DownloadFilter</filter-name>
<url-pattern>/files</url-pattern>
</filter-mapping>
The filter is clearly available for use, as I get no errors when I start the server, however, it is not applied upon downloading a file as I've placed a System.out.println statement inside the filter, and this statement is never invoked.
Here is the filter doFilter() code
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
System.out.println(req.getRemoteUser());
if (req.isUserInRole("freeuser")==true){
//check in DB if downloaded 2 today. CLEAR PAST DAYS. IF NOT DOWNLOADED - FILTER, ELSE NOT.
String user = req.getRemoteUser();
Database db = new Database();
if (db.checkDownloadLimit(user)==false){
chain.doFilter(request, response);
}
}
}

How to define default welcome page in an equinox OSGI server?

We are using OSGI Equinox "org.eclipse.equinox.http.registry.resources" extension to define resources accessible in our different JAR in our OSGI Equinox server. Most of them are just to point to static HTML content so there's no Servlet implementation. I was wondering what was the easiest way to define the default page for a sub folder (defining the "Welcome" file usually defined in a web.xml in standard Servlet packaging). Basically, I define a resource at /mynewresource and would link the user to be directed to index.html when he enters instead of getting a server error.
If you just want to have a default behavior of going to index.html on your resource, you can create that simple filter:
public class WelcomFilter implements javax.servlet.Filter {
/** {#inheritDoc} */
#Override
public void init(FilterConfig filterConfig) throws ServletException {
}
/** {#inheritDoc} */
#Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if (request instanceof HttpServletRequest) {
HttpServletRequest r = (HttpServletRequest) request;
if ("/".equals(r.getPathInfo())) {
r.getRequestDispatcher("index.html").forward(request, response);
} else {
chain.doFilter(request, response);
}
} else {
chain.doFilter(request, response);
}
}
/** {#inheritDoc} */
#Override
public void destroy() {
}
}
You have two choices: you can register this filter once at the root (/) but keep in mind that any request with no path info will get redirected to index.html or you can register it for the sub-domain where you want it. In any case, you need to use the equinox http filter extension.
<extension
point="org.eclipse.equinox.http.registry.filters">
<filter
alias="/mydomain"
class="com.abc.filters.WelcomeFilter">
</filter>
</extension>
There is no standardised way yet to define a default (or welcome) page in an OSGi server.
Coincidentally, I faced the same and decided to add this functionality to the Amdatu-Web project. Aside allowing non-Java resources to be served through the web, it now also allows you to define a default page like:
X-Web-Resource-Default-Page: index.html
or a default page for a specific directory:
X-Web-Resource-Default-Page: /path=index.html
The default page(s) will be served in case no file is requested.
It is not entirely done yet, as it needs some reviewing and I need to update the documentation and examples a bit on the Amdatu website. But, you can already take a look at the code (especially the demo project in the BitBucket project) to get an idea of how it should work.
Filters didn't work for me (using Kura/Equinox) however, using a custom HttpContext implementation I was able to add the required logic in getResources.

how to trace css element errors

I have a JSP file in which I have used many CSS classes.
I have defined those CSS in .css file. If I don't include .css file in the path then JSP will not work as per expectation. In bigger project how can I trace absence of .css file?
Do we have any browser where I can see/trace CSS errors?
No features of Firebug give any error if we miss to include .css file in the project. I am using Firebug and could not find .css error file by using firebug. We should have prior knowledge that we must have missed to include .css file that is why JSP file is not being render properly. But what if somebody else who doesn't have knowledge which .css(or any other html, jsp, javascript) file to be included then such features like FIREBUG is useful. But in case of .css firebug doesn't give error trace
You can write a unit test which loads all your JSPs as plain text files and parses their contents for the required path to the CSS file. Something like this perhaps
#Test
public void hasCssFile() {
for (File file : new File(yourJspFolder).listFiles()) {
String contents = Files.toString(file, Charset.defaultCharset());
if(!contents.contains("path/to/css"){
fail("add Css file to " + file.getName());
}
}
}
Firebug, a Firefox add-on (downloadable from here), can be used to trace CSS related issues.
You may create a servlet filter which checks whether the .css file is refered from the JSP output and only enable it for development cycle.
#WebFilter("/*")
public class CssCheckFilter implements Filter {
#Override public void init(FilterConfig config) throws ServletException { }
#Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
HttpSession session = request.getSession(false);
res. ... // Check the response content here
chain.doFilter(req, res);
}
}

Categories

Resources