Results 1 to 5 of 5
Thread: Create servlet
- 11-05-2010, 05:16 PM #1
Create servlet
I just started with jsp and java servlet's.
Now I downloaded the tomcat-apache-server for my .jsp pages and servlets.
I can run my .jsp files with http://localhost:8080/myJspPage.jsp.
But how could I run a servlet? When I try to go to http://localhost:8080/myServlet, it gives te error that the file doesn't exist.
my servlet:
I compiled it to an .class file and put it into the src folder of the tomcat-server.Java Code:import java.io.IOException; import java.io.PrintWriter; import javax.servlet.*; import javax.servlet.http.*; public class TestServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void init() { } public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); /* Display some response to the user */ out.println("<html><head>"); out.println("<title>TestServlet</title>"); out.println("</head>"); out.println("<body>"); out.println("Hello World"); out.println("</body></html>"); out.close(); } }
Could someone help me?
Thanks,
DennisLast edited by Dennis; 11-05-2010 at 05:22 PM. Reason: spelling mistake
- 11-05-2010, 08:07 PM #2
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 364
- Rep Power
- 4
First steps always looks heavy, but don't worry :)
You have to become familiar with structure of web project
and web.xml and how servlet is described and used there.
Millions of examples are on net for this.
What IDE you use? Eclipse? Netbeans? Have you crated something like
'Dynamic web project' using wizard or you just write all by yourself?
Next time pls paste your error so we know what went wrong.
- 11-06-2010, 07:49 AM #3
I used eclipse to compile the servlet.
But I don't understand the "structure".
I've tomcat 7.0.4.
In the webapps directory I have the directory "servlet-examples" created.
in "servlet-examples" I created the directory "WEB-INF".
In WEB-INF I put the "web.xml" file.
With inside:
In the "WEB-INF" directory I created a directory called "classes".Java Code:<web-app> <servlet> <servlet-name>TestServlet</servlet-name> <servlet-class>TestServlet</servlet-class> </servlet> </web-app>
In the directory "classes" I put the "TestServlet.class" file.
Structure now:
But now I don't know how to run it, or is someting wrong.Java Code:servlet-examples WEB-INF classes TestServlet.class web.xmlLast edited by Dennis; 11-06-2010 at 07:57 AM.
- 11-06-2010, 10:56 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
You're missing the servlet-mapping element of the xml, which maps the servlet-name to a url.
- 11-06-2010, 08:37 PM #5
Similar Threads
-
how can i link html+servlet+xml to form servlet
By balachandarr in forum Java ServletReplies: 0Last Post: 04-15-2009, 04:06 PM -
Servlet to Servlet connection through URLConnection
By bsrjavaforum in forum NetworkingReplies: 0Last Post: 04-01-2009, 04:12 PM -
Servlet - apache tomcat/glassfish server shuts down on servlet usage
By StewS in forum Java ServletReplies: 1Last Post: 12-02-2008, 12:23 AM -
Is there any way to transfer the file Streams from one servlet to another servlet .
By HenaPriyadarsini in forum Java ServletReplies: 0Last Post: 08-30-2008, 11:52 AM -
javax.servlet.ServletException: Wrapper cannot find servlet class util.t2
By osval in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 03:47 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks