JETTY: Embedded JSP inside its jar - java

I'm using Jetty 8.0. to create a simple web server (html/json/png) inside my java application.
I added an handler to use jsp pages positioned inside a package (as mentioned here: http://docs.codehaus.org/display/JETTY/Embedding+Jetty):
String WEBAPPDIR = "com/econorma/jsp/resources";
String CONTEXTPATH = "/jsp";
URL warUrl = WebApplication.class.getClassLoader().getResource(WEBAPPDIR);
final String warUrlString = warUrl.toExternalForm();
WebAppContext webapp = new WebAppContext(warUrlString, CONTEXTPATH);
I tested this code in Eclipse without any problems: html and jsp both work.
My problem come when I run my jar deployed. I get NULL POINTER exception in the getClassLoader line.
I try to put the slash at the end of WEBAPPDIR as mentioned in one StackOverflow post but, also if I solve the null pointer exception, I can't me it work.

You need to put the updated codes of jsp and html files back into the jar to work properly.
The local jsp pages in the package com.econorma.ui.resources will be available only for development process and when you export your project as a war file. The jars will not have the changes that you made in jsp pages which you had in com.econorma.ui.resources.
So you need to copy of jsp or html files from the package you created. And then put these files in the jar in the appropriate packages and update it.
Now you have to build your project again after installing the new jars files and restart your jetty server to see the changes and you should be able to run your server without errors. Hope this helps.

There was two problems for my embedded jetty and jsp:
1-Missing code before calling my webClass:
System.setProperty("org.apache.jasper.compiler.disablejsr199", "true");
2-The Eclipse export to prepare the jar: I used the EXTRACT method and non the PACKAGE one and so it wasn't possibile to find jsp inside the jar.

Related

HTTP Status 404 – Not Found when calling servlet [duplicate]

This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 2 months ago.
I know that there are previous questions that ask the same but I still couldn't find the right solution. I am getting: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. I am using a Tomcat server.
Could someone please help me, the following is my project structure:
.
I was stuck into this problem for a month.
Finally, I figured out that in Eclipse, "Build Automatically" was not set, and I was trying to run my servlet.java file without any servlet.class file, since I wasn't building my project.
The thing which worked for me is to
first build the project => restart the server => run the server on
servlet.
Hope it helps!!
I was running into a similar problem, where my package name was wrong. I fixed the package name and fixed the problem.
Please check your configuration snippet in the web.xml file.
Also, for a cleaner structure, you can create a new folder "jsps" under WEB_INF and move the .jsp files into the same.
The following scenario is explained with the name of the project as abcd, and the port as 8080. The folder WebContent will be inside folder abcd.
In application.properties, add your path
upload.path=C:/.........../abcd/WebContent/
If the last slash after WebContent is ignored, then uploaded files are saved at abcd and not in WebContent (which is inside abcd).
Now, say, there is 1.JPG inside WebContent. If I have to access it, then in my browser ,I have to put URL as http://localhost:8080/abcd/1.JPG
Having the URL as
http://localhost:8080/abcd/1.jpg wont work (note the small case alphabets of .jpg)
Also make sure that the file 1.JPG is visible inside WebContent in abcd in IDE (in my case, it was Spring Tool Suite). Else refresh the project from the IDE.
Now, if the file 1.JPG is inside abcd/WebContent/new/1.JPG, then the URL will be http://localhost:8080/abcd/new/1.JPG
I faced this issue once when there was a runtime exception in the code block of ContextLoaderListener.
public class YourApplicationContenxtLoaderListener extends ContextLoaderListener{
#Override
public void contextInitialized(ServletContextEvent event){
/* There should not be any exception/error in this block , as it would impact the context initialization by tomcat server for respective war file. */
}
}
I encountered this problem on an Azure WebApp running on Java 11 and Tomcat 9.0.
I changed the Java Web Server version from Apache Tomcat 9.0 (auto update) to Apache Tomcat 9.0.20, and then the server worked.
here is the solution for your query,
Simply follow these steps:
go to project properties settings
type demployment and assembly settings
then click on add folder then and add the webcontent folder to your project,
click apply and close then run the project
I hope your problem will be solved.

What is the file path I need to use to view my html pages in my java project on netbeans?

I am using NetBeans IDE 8.2. I have a java web project, lets call it ProjectName. My project folder looks like this:
>ProjectName
>build
>dist
>nbproject
>src
>web
>index.html
>foo.html
>bar.html
>build.xml
Right now, if I run my project, a browser will open and display index.html. The URL reads "localhost:54782/ProjectName".
But I cannot view foo.html or bar.html. I have tried "localhost:54782/ProjectName/foo", and "localhost:54782/ProjectName/web/foo", and many other variations of this pattern. Please help me and tell me what I need to do to view my foo.html and bar.html.
You indicate that you tried localhost:54782/ProjectName/foo. Did you try it with the extension .html? if you did try it with localhost:54782/ProjectName/foo.html. It works by without the extension for index.html because there is a mapping defined in the web.xml file in the tag.

How Can A Non-Config File Be Loaded through a JSP Running in Tomcat 8.0

In Eclipse Luna I have a Dynamic Web Project (with default build settings) on Apache Tomcat 8.0. In the project, I have a JSP loader.jsp that calls a method in a Java class FileLoader that returns a String value for the location of a non-config txt file (lets call it key.txt).
My issue is that I am getting a null value for the file location when FileLoader tries to get key.txt and return its location.
Here is an idea of what I've tried and failed with:
In FileLoader constructor I pass it String "key.txt" as a value and it has a method called get getKeyPath() that returns the file path. I use the following in getKeyPath() to get the file path:
String path = this.getClass().getClassLoader().getResource("key.txt").getFile();
The path variable is returned to the calling object. Here is how I call getKeyPath() in loader.jsp:
String keyPath = new FileLoader("key.txt").getKeyPath();
My issue is that a NullPointerException is thrown in FileLoader when getKeyPath() tries to set the path value. I am lost because this happens no matter where I put the physical "key.txt" in my project directory or at file paths that should be recognized by my project. In my project I have tried the above code with key.txt at the following paths (assume the root folder for my project in Eclipse is called PRO):
PRO/build/classes
PRO/build
PRO/WebContent/WEB-INF/lib
PRO/WebContent(where loader.jsp is located)
PRO/Java Resources/src (Tomcat Install)
Dir>/lib /bin
I got the same NullPointerException for all attempts. Once I resorted to the Tomcat directories I realized I needed help.
Is there something else I need to do so "key.txt" can be loaded in the way I want? Am I doing this completely wrong? I can post screenshots if that would make answering this easier.
As an aside, due to application requirements, I'd prefer that loader.jsp not load key.txt directly. Of course, I'll have to do that if what I'm trying is not possible.
Please note that I am trying to do this in an Eclipse Dynamic Web Project so there is no "bin" folder like a standard Java project. Finally, I'd prefer not to make any build config changes in Eclipse but of course I will if I have to.
Separate out the initialization and calling of the method into two separate lines.
Problem is not that file is not loaded but rather that at the time .getKeyPath(); is called object initisation is not done and that the reason for NullPointer
So change to this :
FileLoader FileLoader fileLoader = new FileLoader("key.txt");
String keyPath = fileLoader.getKeyPath();

Classpath Resource in Tomcat6 (Works in Jetty)

I'm having trouble with a legacy Web Application that I'm migrating to Maven3.
I need to obtain a file from the Classpath that in the directory structure is located in:
/src/main/resources/com/thinkglish/geoip/GeoIP.dat
When I create the .war file with the Maven build, I can confirm that this .dat file is located (as it should be) in:
WEB-INF/classes/com/thinkglish/geoip/GeoIP.dat
I'm trying two different approaches to get the resource from one of my classes, which implements javax.servlet.Filter:
ClassPathResource resource = new ClassPathResource("com/thinkglish/geoip/GeoIp.dat");
and
URL resource = getClass().getResource("/com/thinkglish/geoip/GeoIp.dat");
If I start the application using Maven's Jetty plugin, that works fine in both ways. However, when I deploy the application in a Tomcat and start the server, the resource cannot be located.
In the first case I get a java.io.FileNotFoundException: class path resource [com/thinkglish/geoip/GeoIp.dat] cannot be resolved to URL because it does not exist and in the second case the resource is null.
A curious thing about all this is that if I use one method or the other trying to obtain another resource from the Classpath (e.g. com/thinkglish/struts/i18n/MessageResources.properties or com/thinkglish/filter/LanguageFilter.class) it works without any problems.
Do you have any guess about this? Is it possible that the .dat extension has anything to do with this?
Edited - More data!
I added a new .properties mock file to the exact same directory in which the .dat file lives:
/src/main/resources/com/thinkglish/geoip/mock.properties
I tried to obtain it in Tomcat6 and it worked!
ClassPathResource resource = new ClassPathResource("com/thinkglish/geoip/mock.properties");
I'm starting to think that I need to do something else configuration-wise to make Tomcat6 accept the .dat file as a Classpath resource.
Thanks in advance!
I might be barking up completely the wrong tree here... but have you checked the capitalisation of GeoIP.dat / GeoIp.dat? Is Tomcat running on a case-sensitive OS?
Following should work:
String classpathLocation = "com/thinkglish/geoip/GeoIp.dat";
URL classpathResource = Thread.currentThread().getContextClassLoader().getResource(classpathLocation);
// Or:
InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream(classpathLocation);

Access a file from a web service

I am using netbeans to create a web service and using Glassfish as the server to test it within netbeans.
I have a file that i wish the web service to be able to read data from and possibly write to it. But where do i put the file. If 'course' is my netbeans project root i have tried placing the file in the following locations:
\Course
\Course\xml-resources\jaxb\FlightRequest
\Course\web
\Course\web\WEB-INF
\Course\src\java\org\me\FBooking
\Course\build\web
\Course\build\web\WEB-INF
\Course\build\web\WEB-INF\classes
and tried accessing it in the web service in my unmarshalling code using (as the file i am trying to access is an xml document):
un = (AvailableFlights) unmarshaller.unmarshal(new java.io.File("AvailableFlights.xml"));
But it cant find the file
So where am i supposed to place it?
If you need to write to the file, you should locate the file outside of your deployable code.
If you only need read-only access, putting the file in web/ will make the file accessible from a web browser. That may not be what you want.
If you put the file under WEB-INF/classes, it will be accessible to your code, but not publicly exposed.
Your code fragment for accessing the file will only work for files on the file system, and not for files you deploy as part of the WAR, so you need to look into other ways of loading the file if you decide to package it as part of your WAR.
Take a look at
getClass().getResourceAsStream("file")
which should be able to read from files within a WAR file. (Haven't tested it right now..) But this is only for reading from the file.

Categories

Resources