ServletException (JNLP JSP not found) - java

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...

Related

Same header.jsp for different web application

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.

How does the Tomcat container calls service() method?

How does the Tomcat container calls service(ServletRequest req, ServletResponse res) method?
Where can I look for implementation of this call?
How can I see how this req and res objects (that passed to service() method) are created?
Although the Tomcat Architecture page is in TODO status, there is at least a link to the UML sequence diagram of the request processing. Combining with Tomcat's source code, it is a good starting point.
If you want to know it, first clone apache tomcat source code
git clone https://github.com/apache/tomcat.git
Then inside the cloned repository, launch this command to search where it is invoked service method:
grep -H -n -r "\.service(" --include=*.java
You will find a short file list:
java/javax/servlet/jsp/PageContext.java:107: * in this PageContext until the return from the current Servlet.service()
java/org/apache/catalina/connector/Request.java:3128: // that set towards the start of CoyoyeAdapter.service()
java/org/apache/catalina/core/ApplicationFilterChain.java:231: servlet.service(request, response);
java/org/apache/catalina/servlets/DefaultServlet.java:411: super.service(req, resp);
java/org/apache/catalina/servlets/WebdavServlet.java:349: super.service(req, resp);
java/org/apache/coyote/ajp/AjpProcessor.java:403: getAdapter().service(request, response);
java/org/apache/coyote/AsyncStateMachine.java:41: * been called during a single Servlet.service() method. The
java/org/apache/coyote/AsyncStateMachine.java:58: * been called during a single Servlet.service() method. The
java/org/apache/coyote/http11/Http11Processor.java:498: getAdapter().service(request, response);
java/org/apache/coyote/http2/StreamProcessor.java:257: adapter.service(request, response);
java/org/apache/jasper/Constants.java:41: * HttpJspBase.service(). This is where most of the code generated
java/org/apache/jasper/servlet/JspServlet.java:385: wrapper.service(request, response, precompile);
java/org/apache/jasper/servlet/JspServletWrapper.java:440: servlet.service(request, response);
java/org/apache/jasper/servlet/JspServletWrapper.java:443: servlet.service(request, response);
The most intresting one is java/org/apache/catalina/core/ApplicationFilterChain.java. You wil find more coincidences, but much of them are because there is another interface into Tomcat source code that has a very similar method java/org/apache/coyote/Adapter.java ignore it.
Once you get java/org/apache/catalina/core/ApplicationFilterChain.java, you can edit, got to line 231 and see where the service method is called.
However, both req and res objects are not created in that place. Finding how those are created seems to be a bit more complex and requires more time.
Servlet lifecycle is controlled by the underlying container. Once the servlet has been initialized and there is a request, Tomcat will call the servlet's service method to process the request.
Service method will delegate request to your Servlet class where you can get access to req and res objects in doGet or doPost methods.
public void doGet(HttpServletRequest req, HttpServletResponse res){
}
Update :
1. Upon request from the client, Container creates two objects : HttpServletRequest and HttpServletResponse.
2. Based on the request, Container will find correct Servlet (as per URL mapping), creates new thread for that particular request(one-to-one mapping - new thread for each request) and calls Servlet's service method, passing in created HttpServletRequest and HttpServletResponse objects as arguments.
3. Based on request method (GET or POST) service() method will call doGet() or doPost() method in Servlet, again passing the same HttpServletRequest and HttpServletResponse objects as arguments.
Those are Servlet specifications in a nutshell. How does Tomcat act exactly is implementation specific, it is not controlled by specification. If you need to know how exactly it is implemented in Tomcat, you might check it's source code.

How can I call from one servlet file to another servlet file? [duplicate]

This question already has answers here:
How do I execute multiple servlets in sequence?
(2 answers)
Closed 6 years ago.
I am using net beans 7.1 and I create one JSP file with two servlet files.
like:
index.jsp --->servlet1.java --->servlet2.java
I give some value from index.jsp file and send to servlet1.java.
In this servlet1.java file I call servlet2.java file.
Then it throws NullPointerException. How can I solve this?
My code like this:
index.jsp
<form action="servlet1" method="post">
servlet1.java
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
..................
..................
..................
servlet2 ob=new servlet2();
ob.doPost(request, response);
..................
..................
..................
}
Then it throws NullPointerException.
Use RequestDispatcher
RequestDispatcher rd = request.getRequestDispatcher("servlet2");
rd.forward(request,response);
RequestDispatcher
Defines an object that receives requests from the client and sends them to any resource (such as a servlet, HTML file, or JSP file) on the server.
Update
No need to create an object of servlet manually, just simply use RequestDispatcher to call servlet because web container controls the lifecycle of servlet.
From Oracle JavaEE docs Servlet Lifecycle
The lifecycle of a servlet is controlled by the container in which the servlet has been deployed.
When a request is mapped to a servlet, the container performs the following steps.
If an instance of the servlet does not exist, the web container
Loads the servlet class.
Creates an instance of the servlet class.
Initializes the servlet instance by calling the init method. Initialization is covered in Creating and Initializing a Servlet.
Invokes the service method, passing request and response objects. Service methods are discussed in Writing Service Methods.
What are you trying here,
servlet2 ob=new servlet2();
ob.doPost(request, response);
Its not necessary to create an object explicitly for a servlet, Web container creates an instance for a servlet and shares it during the app's lifetime . Though you have created an object here, it will return the existing object only.
You can is instead go for Request Dispatcher or page Redirect.

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.

RequestDispatcher in Java

What exactly the main purpose of using RequestDispatcher, for example when it's executed in Filter like the following example:
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws ServletException, IOException {
HttpServletRequest aHttpServletRequest = (HttpServletRequest) request;
aHttpServletRequest.getRequestDispatcher("/init.jsp").include(request, response);
chain.doFilter(request, response);
}
In your case, the output of JSP is prepended to every page that filter is attached. This is a technique to easily add a common header to all your pages.
It's a little bit confusing when RequestDispatcher is used int his context. Normally, when you want to dispatch your request to another servlet or JSP for process, you use RequestDispatcher to forward to another resource. In this case, your request is not dispatched anywhere else, instead you include output generated by another resource to your current response.
The javadoc says is better than I can:
RequestDispatcher:
Defines an object that receives
requests from the client and sends
them to any resource (such as a
servlet, HTML file, or JSP file) on
the server. The servlet container
creates the RequestDispatcher object,
which is used as a wrapper around a
server resource located at a
particular path or given by a
particular name.
In other words, you obtain a RequestDispstcher when you want to include from, or forward to, another resource on the server.

Categories

Resources