I am trying to make servlet.
I have installed tomcat6 on ubuntu with admin examples and docs. I am able to run the examples provided. But when i try to make my own servlet it doesnt work.
I did following steps
Under the ROOT i create folder with
-ROOT
----myapp
------WEB-INF
---------classes
I made two files one is index.html which have a button and action on form to call the servlet. Second is .java file. I compiled the .java file and .class is done. So now tree look like
-ROOT
----myapp
------index.html
------WEB-INF
---------classes
-----------TestServ.java
-----------TestServ.class
Now i open this in browser using http://localhost:8080/myapp
It shows up with index.html page with button. But when i click on the button it says
Error 404:
http://localhost:8080/myapp/TestServ not found !!
I dont know where m going wrong. I have set the CATALINA_HOME too. But still this problem continue.
You need to create a web.xml in the WEB-INF directory, and define the servlet mapping in web.xml, so that the myapp/TestServ URL is forwarded to the TestServ servlet class.
Here is a page describing web.xml, and has the example and elements you need to set up. For your class, these elements will probably look something like this:
<servlet>
<servlet-name>testServ</servlet-name>
<servlet-class>TestServ</servlet-class>
</servlet>
<servlet-mapping>
<!-- For any URL starting with /content/, the rewriter servlet will be called -->
<servlet-name>testServ</servlet-name>
<url-pattern>/TestServ</url-pattern>
</servlet-mapping>
You shouldn't be deploying any of your code under ROOT.
You shouldn't have any Java class in the default package. Try putting your TestServ.java in a package.
Your deployment should NOT include any .java files.
You've got to register your servlet properly in web.xml. Include a mapping to a particular URL.
Your best best is to create a WAR file named myapp.war, which includes WEB-INF/classes and WEB-INF/lib and a web.xml for your situation. Put that in the Tomcat /webapps and start the container. If you've registered your servlet properly you should be able to access it via http://localhost:8080/myapp/TestServ.
I'd read the deployment docs carefully.
Related
I have an mp4 video on my GlassFish java application server. What URL do I use to access it in a browser?
The video path in the application on the server is:
myapp/WEB-INF/videos/myvideo.mp4
I tried adding this to my web.xml file:
<servlet>
<servlet-name>myvideo</servlet-name>
<servlet-class>videos.myvideo.mp4</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myvideo</servlet-name>
<url-pattern>/myvideo</url-pattern>
</servlet-mapping>
and restart the server, then go to the URL: https://www.example.com/myapp/myvideo but that just gave this error:
java.lang.ClassNotFoundException: videos.myvideo.mp4
Obviously it's not a servlet, but not sure what else to try.
Why are you trying to access the video as a servlet and not as a static asset, like you would an image? Using a servlet mapping is obviously wrong.
Instead if you have a running WebApp in GlassFish you should be able to access it via https://www.example.com/myapp/videos/myvideo.mp4 assuming you've put the videos folder under your webapp's j2ee root.
The j2ee root would be configured in your application.xml as the web-uri. In your case it is likely that the myapp folder is your j2ee root. So move your videos up a folder.
(Converted comment 'discussion' into an answer).
So I have this google app engine app that a development company developed and they put a test server up online and it works completely. I can build the app locally and run it on the gae dev server locally and everything works, but when I upload to google app engine it gets errors and 404 page not found. I fixed all the errors by including specific .jar files in the WEB-INF/lib directory that I found in target/lib.
I've tried just including all the .jar files that are in the target/lib directory but then the app gets errors locally so I've had to find the specific files and include those in the WEB-INF/lib directory.
In the logs I get this error
org.springframework.web.servlet.PageNotFound noHandlerFound: No mapping found
for HTTP request with URI [/api/universe/v1/auth/signup/student/email] in DispatcherServlet
with name 'spring-dispatcher'
For this error I can't find which file may be missing that I need to include in the WEB-INF/lib directory because I have already included all of the spring .jar files. What file may I be missing? Is there anything else I should include in here to help find what the problem is?
Also if there a way to see what files are being uploaded to google app engine? If I could just see what was missing I could stick it into the WEB-INF/lib directory.
Your error message indicates that there's a servlet-mapping element missing and/or improperly set up for the DispatcherServlet ('spring-dispatcher' could either be a param-name or the actual servlet-name, without your web.xml file I cannot say for certain). You can set the servlet-mapping element in the web.xml file in WEB-INF.
As an example, a given servlet could be set up like this:
<servlet>
<servlet-name>redteam</servlet-name>
<servlet-class>mysite.server.TeamServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>redteam</servlet-name>
<url-pattern>/red/*</url-pattern>
</servlet-mapping>
There's documentation here for more information on the web.xml file
This is my first attempt at making a Servlet Program which just displays text/html. I created the directories as is should be -
C:\Java_Advanced\DemoServlet
DemoServlet (folder)
--WEB-INF
----classes (containing my .class file)
----lib
----web.xml
--MyServlet.java (sitting in parallel with WEB-INF)
I'm using tomcat apache 8.0.9
Here is my web.xml -
<web-app>
<servlet>
<servlet-name>Hi</servlet-name>
<servlet-class>MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hi</servlet-name>
<url-pattern>/run</url-pattern>
</servlet-mapping>
</web-app>
I made the war file (demo.war) of whatever was inside the DemoServlet.
I started the Tomcat Apache service. I have given a port number other than the default 8080. I deployed my demo.war successfully (I am assuming because the message OK was printed above AND the application /demo's Running is coming to be true(I am told that a true state means there is no problem with the xml file)).
But still I open the new tab/window of the browser and type "localhost:myportnumber/demo/run" and all I get is HTTP Status Error 404 description: The requested resource is not available. I have tried refreshing with ctrl+F5, tried different browsers but still gives the same error.
What is wrong here? I searched over the internet and found something on this site:
http://www.ntu.edu.sg/home/ehchua/programming/howto/Tomcat_HowTo.html#TomcatDebug
(Scroll all the way to the bottom where it says for error 404)
Everything is fine until 3.d point. The sub-directory mentioned as xxxx is "demo" in my case right? It does exist. And so does demo.war alongside it in parallel(is that how it should be?)
C:\Program Files\Apache Software Foundation\Tomcat 8.0\webapps\demo
C:\Program Files\Apache Software Foundation\Tomcat 8.0\webapps\demo.war
When I open the demo directory I DO NOT get a WEB-INF sub-directory. Instead there is a META-INF sub-directory and alongside it MyServlet.java file. Opening the META-INF folder there is a MANIFEST.MF file. Now what is that?? So, is this the reason for getting 404. Even if it is how do I fix it?
It looks as if the structor of your war file is wrong.
The inside of the war file should at least look like:
WEB-INF/web.xml
WEB-INF/classes/MyServlet.class
Look in your war file. It is just a zip file.
I'd strongly suggest using correct tool for building Java EE applications. You can use Eclipse with WTP. It will create correct project structure, allow you to more easily create and validate descriptors and you will be able to deploy your applications directly to server from the tool.
There must be a problem with current structure of your war file (its hard to tell from your description what is file/folder nesting) or building war itself.
This is the structure of the JSP app on eclipse.Once I run it on eclipse using tomcat server(localhost:8090/index4.html) it works.On the index page i have to add details,this details are uploaded via the servlet as you can see above Java Resources->src->FileUPloadDBServlet(Also I am not sure if it uses this or it uses WEB-INF->src->FileUploadServlet below.)
In my index4.html the action is action=uploadServlet(no address given for it).This everything works on using eclipse.
But once I put the QMS folder(not WAR) from eclipse worskspace to tomcat ROOT,the index4.html works but the following action,i.e uploadServlet doesnt work(here I use the address localhost:8090/WebContent/index4.html)I dont have a web.xml.
Is that creating a problem?
Please provide me help.
You need to put your project folder or .war file in the
webapps folder of Tomcat directly not in ROOT folder under webapps.
Try to follow directory structure as follows
Photo Courtesy http://www.studytonight.com/servlet/steps-to-create-servlet-using-tomcat-server.php (Here you can also find more details on Servlet and JSP, as well as project structure)
Where you have your web.xml put into the WEB-INF folder under your application folder.
For java files you don't need the source files, the classes folder will have all the source folder (src) files compiled and ready to execute.
The lib will contain all the .jar files you need for your application to run.
Make sure you put web.xml file at proper place, because without it, application will not be able to run. Because as they say web.xml is Heart of the application.
Let me explain you the problem.
When we configure a dynamic java project to run on eclipse the server takes the just the stuff from webcontent folder and deploy it the wbcontent folder contains web.xml file which defines the url descriptor for servlet.
now when you copy the whole folder the server can not find the web.xml file which is a descriptor where it expects the file to be.
hope its clear comment for clarification
GWT RPC call don't seems to work when i deploy my war file to TOMCAT (tomcat/webapps/ROOT/war).
It gives me an error:
The requested resource
(/war/myproject/call) is not
available.
If i change the directory structure and then deploy directly war contents (not war directory itself), like (tomcat/webapps/ROOT/project.html, project.css, project, etc...) then it works.
Can someone please explain me whats going on?
I think there might a problem at:
<servlet>
<servlet-name>callServlet</servlet-name>
<servlet-class>com.myproject.server.dao.Call</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>callServlet</servlet-name>
<url-pattern>/myproject/call</url-pattern>
</servlet-mapping>
The thing is that a single Tomcat server can have multiple applications deployed, each in its so-called context. The applications are deployed in the webapps folder and each folder is mapped to one context, while the ROOT folder is the default (no-context).
To access an application on Tomcat, you specify the context after the URL. For example if you had an application (context) Test in webapps/Test folder, you would access it like this:
http://localhost:8080/Test
But applications in the ROOT folder have no context and are accessed by simply going to localhost:8080. And this is your case. Tomcat is looking for you application directly in the ROOT folder but you have your app in the ROOT/war folder. In other words, the RPC call expects the myproject folder to be under the ROOT folder and not under the ROOT/war folder. That's why it's not working.
If you still wanted to have your war folder within the ROOT folder, you would have to change the url-pattern to /war/myProject/call.
Well i found the solution, it had to do with Tomcat's way of operation.
Open your project war directory
Select all the files (html/jsp , images, WEB-INF etc...)
Compress all the files into a single project.zip archieve
Rename the project.zip into project.war
Copy project.war into Tomcat/webapps/
Restart Tomcat server
You will now notice inside webapps directory that project.war has been decompressed into a project directory, if you open it you will find all the .war contents(html/jsp, images, WEB-INF, etc...)
Access it from here http://localhost/project or http://localhost/project/index.html or index.jsp.
The error was: I was compressing only the war directory (not it's inside contents) into project.war.
look like, servlet is not initializing for you war try to change SERVLET tag as
i.e. add tag
<load-on-startup>1</load-on-startup>
this tag ensures that servlet should be loaded
<servlet>
<servlet-name>callServlet</servlet-name>
<servlet-class>com.myproject.server.dao.Call</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
hopefully this will work