I can not run the servlet in Tomchat 7.0.90 - java

I'm new to Java. I make my first servlet in Intellij Idea 2018.2.1.(in this screen my idea project) I do as instructed from here:
http://www.ntu.edu.sg/home/ehchua/programming/java/javaservlets.html
  I created all the directories, as it says in the tooltip, created and compiled the class HelloServlet.java and put it in the desired directory (in this screen my my Tomcat directory with my class), created and filled the web.xml filehere my web.xml and it's Tomcat path. But when I try to call it in a browser string "http://localhost:8080/helloservlet/sayhello" (as stated in the instruction) I get an error in this screen.
What am i do wrong? What i need to do to solve this issue?

Problem is solved. Unfortunatelly, i understand wrong my instructuion, i need to put HelloServlet.class in this path "C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\helloservlet\WEB-INF\classes\mypkg", no this file - HelloServlet.java. Now all works correctly. Thank's to all.

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.

.exe file Failed due to exception in main class

I made an app using javafx and packaged it as exe file. It works fine being started from eclipse but being installed it says
Failed due to exception in main class
no matter what pc it was started at. I've found a few same questions but everyone found solution by themselves but don't tell how. One question has an answer:
the issue were VM parameters I added in the build.xml in the
fx:platform / fx:jvmarg section. These params were put into the
package.cfg file which is called from the .exe file to initialize the
VM.
In my build.xml there is only one string related to the named fx:platform But in my case there is only <fx:platform basedir="${java.home}"/> and nothing else. What am I doing wrong?

Get File path not apache tomcat path

I'm trying to load a jasper report from my project's directory, but when I start the application, it gives me an error about file location, it says:
java.io.FileNotFoundException: C:\Program Files\Apache Software Foundation\apache-tomcat-7.0.50\bin\report.jasper
To get the file path, I use this:
new File("report.jasper").getAbsolutePath()
If I run that in a simple class, it gives me the right path, but when I run the application it gives me the tomcat's path, I tried some other functions like getCanonicalPath, getCanonicalFile and getAbsoluteFile; but it's always the same result.
Is there a solution respect this? My application will run on both platforms: Windows and Linux, it will be annoying place the report's file in the each respective tomcat's path everytime I update the application, I'm trying not to do that.
Thanks in advance.
If your file is located inside the web app root you should use servlet context to ask it the realpath
as javadoc explains
Another way is to user getClassLoader().getResourceAsStream() , but you can use it only if you don't have the need of knowing the filepath.
You can use Class#getResource(String name) or ClassLoader#getResource(String name) method both returns URL object and than you can use Url#getPath() to get the path.

JETTY: Embedded JSP inside its jar

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.

Unable to read applicationContext.xml when executing the spring class as a runnable jar

I have a spring class with main method. Inside the class am trying to read the values applicationContext.xml . My intention is to jar this main class along with its dependant jars,property files and applicationContext.xml .
But when i try to run the jar file via unix command prompt, it looks like the applicationContext file is not getting loaded.
The applicationContext.xml is seen inside the jar file and am able to see the sysouts inside my class.The code used to read the applicationContext.xml is
ApplicationContext context = new ClassPathXmlApplicationContext(
"classpath*:**/applicationContext.xml");
When i print context it is giving me the below message.
org.springframework.context.support.ClassPathXmlApplicationContext#89fbe3: start
up date [Mon Oct 01 15:07:43 IST 2012]; root of context hierarchy
When i try to print context.getBeanDefinitionCount() -- it gives me 0.
But am able to successfully excute this via eclipse . It is able able to read the applicationContext.xml and giving me the bean count as 13.
Not sure why it is not working when i run it as a jar.Please help me with your comments.
How did you get the .jar package? Did you use Eclipse export wizard? It's likely that you have put the "applicationContext.xml" in "/resources" folder. Make sure you fix the "Java Build Path" in Eclipse. Remove all the exclude tags that may be present on "/src/main/resources" folder. The resource folder should be considered a classpath, not just a folder with some xml files in the .jar package. Hope that helps.
A quote from the Spring documentation (emphasis mine):
Please note that "classpath*:" when combined with Ant-style patterns will only work reliably with at least one root directory before the pattern starts, unless the actual target files reside in the file system. This means that a pattern like "classpath*:*.xml" will not retrieve files from the root of jar files but rather only from the root of expanded directories. This originates from a limitation in the JDK's ClassLoader.getResources() method which only returns file system locations for a passed-in empty string (indicating potential roots to search).
I have struggled with the same issue. I can run my application fine from inside Eclipse, but when I export it as an executable jar, it fails.
I have worked around the issue by:
going to the build path screen in Eclipse and removing the filters on the /resources folder. This causes the export to put the META-INF folder and its contents that are under /resources into the top level of the .jar file.
Remove all the wildcards from the application context path:
applicationContext = new ClassPathXmlApplicationContext(
"classpath*:META-INF/spring/applicationContext*.xml");
becomes
applicationContext = new ClassPathXmlApplicationContext("classpath:META-INF/spring/applicationContext.xml");
I am sure this is due to some difference in the classloader between the Eclipse environment and my windows java environment. Figuring out how to resolve that will be the next task.

Categories

Resources