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);
}
}
Related
I have a running web application with a functional header with js and css of it's own. Now I m trying to use the same header with all the features in my another application.
Instead of duplicating the code, I need to have some kind of a controller or servlet that can provide me this JSP in proper functional format.
Currently I have tried this approach mentioned below code in a servlet :
#Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
CharArrayWriterResponse customResponse = new CharArrayWriterResponse(resp);
req.getRequestDispatcher("/WEB-INF/jsp/layout/header_new.jsp").forward(req, customResponse);
resp.getWriter().print(customResponse.getOutput());
}
I m getting the response as html but stil not able to use it in another application using tag.
Any suggestions would be helpful.
I'm trying some URL on browser, it works well on all browsers including IE 10 but when on Microsoft Edge, it fails at a point while doing HttpServletResponse sendRedirect, which expires HttpServletRequest session and the expected page does not appear.
Please help for how we can resolve this browser specific redirect-session issue.
Basic code:
public class MyServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// some code where values are set in session
session.setAttribute(myAttribute, value);
response.sendRedirect("https://qa.sys.com/MainPage.jsf");
}
public class MyFilter implements Filter {
#Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)servletRequest;
// myAttribute below comes null as request.getSession() is null
Boolean myAttribute = request.getSession().getAttribute(myAttribute);
}
In logs, I got this Exception:-
java.lang.IllegalStateException: Response already committed
at weblogic.servlet.internal.ServletResponseImpl.objectIfCommitted(ServletResponseImpl.java:1861)
at weblogic.servlet.internal.ServletResponseImpl.sendRedirect(ServletResponseImpl.java:961)
at weblogic.servlet.internal.ServletResponseImpl.sendRedirect(ServletResponseImpl.java:956)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:78)
Truncated. see log file for complete stacktrace
You could also use F12 dev tools to check the Network tab in Edge and see if there's any error in console. This error usually occurs when a java servlet is attempting to write to the output stream (response) after the response has been committed. You could refer to this thread to find out why the response will get committed.
In this blog, it gives the solution and you could also refer to the sample code in the post:
It is always better to ensure that no content is added to the response after the forward or redirect is done to avoid IllegalStateException. It can be done by including a ‘return’ statement immediately next to the forward or redirect statement.
I also find an answer with detailed information about this issue and you could check it.
I'm developing a JWS application as an applet replacement. So far I've been able to launch my app from its parent application via a link in an HTML page to static JNLP. But my app really needs to be launched by dynamic JNLP since the argument values will be different with each execution. So I decided to extend the JnlpDownloadServlet in the manner shown [here][1]. My download servlet is called download, which is mapped to a URL pattern \download and which references a JSP called myJnlp.jsp, which currently contains some static JNLP, in the overriden method service:
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
HttpServletRequest request = (HttpServletRequest) req;
resp.setContentType("application/x-java-jnlp-file");
request.getRequestDispatcher("/myJnlp.jsp").include(request, resp);
}
When I try to invoke my download servlet, I get the following error:
javax.servlet.ServletException: File "/myJnlp.jsp" not found
org.apache.jasper.servlet.JspServlet.handleMissingResource(JspServlet.java:417)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:384)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
com.myCompany.parentApp.myDownloadServlet.service
I placed myJnlp.jsp in the directory jsp which is the directory that contains the other JSPs used by the parent application. Did I put the JNLP page in the wrong location or did I specify it incorrectly in my servlet code?
I solved this by creating a subdirectory called myJWSApp, then I placed the JNLP JSP in that directory then referenced it as
request.getRequestDispatcher("myJWSApp/myJnlp.jsp").include(request, resp);
Thanks to #rickz whose comment pointed me in the right direction...
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.
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.