Actually i try to create my first servlet and open it in a browser. But i alway get a 404 error.
My webapps folder from tomcat directory looks like:
docs
examples
host-manageR
manager
ROOT
test
index.htmL
servlet
bin
test.class
src
test.java
.classpath
.project
WEB-INF
classes
my index.html:
<html>
<body>
<div style="width:500px;height:300px;margin:auto;background-color:#EEEEEE;padding:0px 10px 0px 10px;margin-top:50px;">
<h1>Send Text</h1>
<form action="servlet/bin/test" method="get" style="font-size:10px;">
<textarea rows="10" cols="59" name="input"></textarea>
<input type="submit" style="margin-top:20px;"></input>
</form>
</div>
</body>
and this is my test class:
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
#WebServlet(urlPatterns={"/servlet/bin/test"})
public class test extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{
PrintWriter out = res.getWriter();
res.setContentType("text/html");
out.println("this is First servlet Example ");
}
}
I know it does nothing with the input from the html form but at this point I just want to open it and see the text inside^^... But when i open:
http://localhost:8080/test/index.html
, fill in the form and countinue or when i open:
http://localhost:8080/test/servlet/bin/test
directly, i always get a 404 error with everything i tried. I would prefer a solution without a xml but i think the problem is not there.
There are several problems in this code.
You're not respecting Java naming conventions. Classes should always start with an uppercase letter. They should never be in the default package.
You're deploying your whole development environment (sources, etc.), instead of generating a correct war file or exploded war file, and to deply that. The structure of your web-app should be
test
any static file you want
WEB-INF
classes
.class files, organized in directories strictly matching the package hierarchy
lib
jar files needed by your application
You're confused about what
#WebServlet(urlPatterns={"/servlet/bin/test"})
does. The url patterns is the set of URLs that you would like to use to access your servlet. The value doesn't have anything to do with the actual location of the servlet class on the disk. If you want the URL of your servlet to be /hello, then you simply use
#WebServlet(urlPatterns={"/hello"})
or, even simpler:
#WebServlet("/hello")
Once deployed, the complete URL of the servlet will thus be:
http://localhost:8080/test/hello
/test being the context path your webapp which is, by default, the name of the war file of exploded war directory you put under tomcat/webapps.
Try to follow these tutorials in Java-Brains for JSP & Servlets its very simple and explain every things from scratch :)
I think after all the comments the problem is just that your
web archive is not even registered within the server.
Try to package everthing under test as a zip file and rename it to test.war.
A war file is nothing more as a zip file with ending "war" for web archive
which indicates that server should resolve the archive as a web application.
if you extract this archive it should look like this.
index.html
WEB-INF
web.xml
|- classes
|- test.class
|- lib
Deploy this file using the management console of tomcat and manually deploy it to your server.
Additionally have a look about Webarchives "war".
If the servlet is registered within the server correctly than you reach it using
the specified pattern which you declared within #WebServlet Annotation.
The context path applies to the name of you war archive.
http://localhost:8080/<name of the warfile>/<urlPattern>
http://localhost:8080/test/servlet/bin/test // if the given example still applies
Related
In my tomcat webapps directory I have various projects with servlets in them ..
say named test,beer etc
My question is irrespective of the web.xml or contents when i type localhost:8080/folderName I must be able to view the contents right? but it doesnt work for all folders , for some it says resource not found ,
Example my test folder opens in the browser and my beer folder doesnt , I restarted the browser and tomcat after adding or modifying folders , why can this happen , please explain
My dear friend there is a certain process/configuration/rule that tomcat follows in order to render any web content.It is not some magic happening.
Tomcat has a way of reading web deployments within the /webapp directory.
When we type some URL On the browser tomcat does the following:
Example.
URL : http://localhost:8080/foldername/xyz
Here tomcat takes the part of the URL after http://localhost:8080,that is foldername/xyz.
So here the first part which is foldername means name of the folder present in the /webapps folder.
So reading this tomcat goes inside that folder.Later tomcat is at the mercy of a file called web.xml.All mapping from /foldername/ i.e. /xyz in our case, onwards are present in web.xml.
In your case , if you type http://localhost:8080/foldername/ , tomcat knows that browser refers to webapps/foldername but does not know which resource html/jsp/servlet to forward the request so as to be able to generate a response.
Hence it gives a resource not found exception.
If you want to run the above URL (http://localhost:8080/foldername) then you need to configure a <welcome-file-list> tag in the web.xml file.
So for the folders which are working in your case with the above URL, just open their web.xml file and you shall find the <welcome-file-list> tag.
Actually this is what happened
I had a folder test which had some files (NO WEB-INF) , tomcat listed its contents on typing localhost:8080/test
My other folder beer had a WRONG WEB-INF web.xml configuration
So the following is clear to me now
Without a WEB-INF folder and web.xml tomcat will just list the contents of the directory but when you have a WEB-INF and web.xml and something is wrong in them it doesnt even list the directory contents .
Just my understanding of it .
Thanks!
I have a very simple java web-application which is deployed to Tomcat.
In this application I have some code which goes like this:
package com.mywebapp.hello;
import javax.servlet.http.*;
import java.io.*;
public class PdfTwoServlet extends HttpServlet {
public void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException {
httpServletResponse.setContentType("application/pdf");
InputStream is = PdfTwoServlet.class.getResourceAsStream("/two.pdf");
When I compile my code and deploy it to tomcat, the directory structure goes like this:
This is under say C:\Tomcat\webapps\myApplication :
So
PdfTwoServlet.class.getResourceAsStream("/two.pdf");
works fine and finds the file two.pdf which is under classes folder, but I have no idea how this works.
Accessing properties file in a JSF application programmatically here BalusC says:
The Class#getResourceAsStream() can take a path which is relative to
the location of the Class which you're using there as starting point.
If you use /foo/filename.properties, then it will actually load
foo/filename.properties from the classpath root.
I have 2 questions:
1) Why is the classpath root is WEB-INF\classes folder? Where is it determined? ( As far as I understand, it should be because code is working fine as I said. )
According to this: http://docs.oracle.com/javase/tutorial/essential/environment/paths.html , I do not have a classpath set in my local machine. So maybe when I start tomcat, it sets the classpath? But there are few web-apps deployed, are there few classpaths?
2) Is there a better way to make this work instead of: PdfTwoServlet.class.getResourceAsStream ? Something like getClassPath().getResourceAsStrem ?
Edit: Maybe someone more experienced and with better English can edit the title of this question. I am not sure if it is good enough.
For 1) The classpath root in a servlet application is by specification the WEB-INF\classes folder of the jar, plus the root of all the jars in WEB-INF/lib of that WAR. Anything in those locations will be seen as the root of the classpath.
For the question on how classpaths in tomcat work, when tomcat deploys it set's the classpath in the following way: each WAR corresponds to a separate class loader which has access to WEB-INF/classes and all the jars in WEB-INF/lib.
By default if the resource searched is not found here, it will be searched for in the tomcat/lib directory. If it's not found there, it will ask the parent class loader, and so on, the explanation can be found here
If there are several web-apps deployed, each WAR will have it's own class loader pointing to it's own WEB-INF/classes and WEB-INF/lib jars.
For 2) there is not method like getClasspath(), ServletContext.getResourceAsStream() is the advised way in servlet applications for getting resources from inside the WAR. The WAR might be zipped or exploded, and this works for both, see this answer.
I know it has something to do with WEB-INF\classes. But here is the code I compiled after putting the appropriate .jar files in my class path.
import java.io.* ;
import javax.servlet.http.* ;
public class BeeServlet extends HttpServlet
{
public void doGet( HttpServletRequest request , HttpServletResponse response )
{
response.setContentType("text/html");
try
{
PrintWriter out = response.getWriter();
out.println( "a-buzz-buzz ..." );
out.close();
}
catch( Exception e )
{
System.out.println( "cannot get writer: " + e );
}
}
}
It compiles fine, but I have not found any type of like example as in where to put it and how to call it with the localhost:8080 URL. I'm doing this without an IDE to try to learn as best I can but this point is simply confusing to me...
EDIT- I had compiled this code. I put it into the tomcat 7.0/webapps/BeeServlet/WEB-INF/classes directory like all the tutorials say to do. I type in localhost:8080/BeeServlet, and nothing happens. This just doesn't make sense.
You need a web application deployment descriptor (web.xml), which provides a mapping of the URL to your Servlet and a certain directory structure for your project on the web server. Usually this structure is created for you if you use a web application project template in an IDE.
Theory to read: http://tomcat.apache.org/tomcat-7.0-doc/appdev/deployment.html
Tutorial: http://www.nogoodatcoding.com/howto/deploy-a-servlet-on-tomcat
Example of web.xml file: running and deploying servlet with eclipse and tomcat 7
The structure of a webapp is the following:
root
- WEB-INF
- classes
- .class files, respecting the package hierarchy
- lib
- .jar files used by your application, other than the servlet and JSP jar files
- web.xml
- whatever you want
deploy the root directory under webapps, or make a war file containing the contents of the root directory and deploy this war file, and you'll get a webapp.
The web.xml is not necessary since servlet 3.0 (Tomcat 7), if you declare the servlet and its mappings using annotations. Otherwise, you need one.
I am new to java and jsp. I have just created a small package and that package contains a simple java class. Let me show what i really want to do. I am not working with servlets so please tell me about this simple example so that i can go ahead with my work. Below is my code or a package class and a jsp page.
my java package
package mypack; //this is my package
public class Abc{
public Abc(){
}
public void message(){
System.out.println("My first java package");
}
}
index.jsp this is my jsp page where i need to use this package
<%# page import="java.sql.*"%>
<%# page import="mypack.*"%>
<%
Abc a = new Abc();
a.messsage();
%>
I was using JDK1.5 and tomcat 3. But i want to update my system. Now i have JDK1.7.0_11 and tomcat 7. I know the path of tomcat 3 to put my packages but i don't know where to put these packages in tomcat 7.
tomcat 3 directory path to place packages:
D:\Java\tomcat\examples\WEB-INF\classes //i put my package at this path in tomcat3
tomcat 7 directory path to place packages:
D:\web\Tomcat 7.0\webapps\ROOT\WEB-INF //trying to put my package in here but no use.
I could not find the classes folder under the direcoty WEB-INF in tomcat 7. I made a folder myself named classes inside of WEB-INF but it does not work. Even i have deleted that my "classes folder" and put my package in WEB-INF, but it does not work. Please tell me the path where i can put my java package in tomcat server 7.
I have placed my jsp page in here:
D:\web\Tomcat 7.0\webapps\ROOT\a //folder "a" contains my jsp file. index.jsp and
its working
Problem is, jsp page could not find the package. Please help me out with this.
packages are always located in WEB-INF/classes, not directly in WEB-INF.
Try to put them here thus:
D:\web\Tomcat 7.0\webapps\ROOT\WEB-INF\classes
I'm trying to run old servlet under resin. I have deployed it as a war file. After starting resin there is a dir ic. It contains Webcontent dir with WEB-INF/lib/ic.jar (fatjar), jsp etc. While extracting this ic.jar I see there package com/x/y/z but while trying to access servlet page I'm getting an error: package com.x.y.z does not exist.
Can anyone give me any clue?
The jar needs to be in WEB-INF/lib, not in WEB-INF.
When extracting the JAR file, you should see
com/x/y/z/ClassName.class
not
com.x.y.z/ClassName.class
Assuming that what you wrote wasn't a summary of what you saw (or a typo).