hansley 0 Report post Posted October 9, 2004 Can anyone post here one or some examples of Simple Java Servlets please?I need more resources to do my assignments.Searching the web takes up too much time.So I am using one of the purpose of a forum, sharing ideas....lol Share this post Link to post Share on other sites
arp 0 Report post Posted October 9, 2004 Hi; to show just how easy this is, here is probably the simplest/shortest servlet you can write: import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorld extends HttpServlet{ public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{ res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println("Hello World"); } } I suggest if/when you get the time to go through sun's course: http://www.oracle.com/technetwork/java/index.html there's obvioulsy so much more you can do with servlets, so you need to be more precise asto what you need to know/achieve. It may even be the case that you should be looking at JSP instead of servlets... hope this helps - arp Share this post Link to post Share on other sites
sanweikui 0 Report post Posted October 10, 2004 good advice!But have you got a servlet container for your code? and do you know how to deploy the code? Share this post Link to post Share on other sites
eldeo 0 Report post Posted October 10, 2004 very simple servlet:////////////////////////////////////////////////import javax.servlet.*;import javax.servlet.http.*;import java.io.*;public class SimpleServlet extends HttpServlet { int i = 0; // Servlet "persistence" public void service(HttpServletRequest req, HttpServletResponse res) throws IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.print("<HEAD><TITLE>"); out.print("simple servlet"); out.print("</TITLE></HEAD><BODY>"); out.print("<h1>This is simple servlet " + i++); out.print("</h1></BODY>"); out.close(); }}//////////////////////////////////////////////// Share this post Link to post Share on other sites
eldeo 0 Report post Posted October 10, 2004 To deploy this code you need - Sun Application Serveror- TomcatI advise you to download Tomcat from this sitehttp://tomcat.apache.org/and read tutorial. You need serveral hours to learn about it--eldeo Share this post Link to post Share on other sites
bj?rn 0 Report post Posted October 26, 2004 deploying on tomcat is actually quite easy. you have to make a so-called web-application (see below) and then zip all the files belonging to the webapp. then rename this to war. Call this file, say... mywebapp.war. Drop this file in your /path-to-tomcat/tomcat/webapps directory.start the tomcat and if you use a local tomcat installation you can now reach the webapp underLOCALHOST:8080/mywebapp/url-pattern (for explanation of url-pattern see my explanation of web-application.What is a web-application?Well.. basically it is a bunch of files, which can be JSP, html, java classes, jar files.. etca webapp basically has the following directory structure:./WEB-INF/./WEB-INF/classes/ (here you put the java classes of your servlet for ex. )./WEB-INF/lib/ (here you put any non-standard libraries you use in your java classes)./WEB-INF/web.xml (the so called web-application deployment descriptor... see below)./some_jsp_page.jsp (here you can put jsp pages...)./some_web_page.html (... as well as any static content)What is contained in the web.xml?It's an xml file where you describe your web-application. It could look like this: [br]<?xml version="1.0" encoding="ISO-8859-1"?>[br][/br]<web-app xmlns="http://java.sun.com/xml/ns/j2ee"[/br] xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance; xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee [/br] http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"[br] version="2.4">[/br] [br] <description>[/br] An examplatory web.xml file[br] </description>[/br] [br][/br] <servlet>[br] <servlet-name>helloWorld</servlet-name>[/br] <servlet-class>my.java.package.HelloWorldServlet</servlet-class>[br] </servlet>[br][/br] <servlet-mapping>[/br] <servlet-name>helloWorld</servlet-name>[br] <url-pattern>/sayHello</url-pattern>[/br] </servlet-mapping>[br]</web-app>[/br] If you write a servlet class my.java.package.HelloWorldServlet now, place it underWEB-INF/classes/my/java/package/HelloWorldServlet.class, make a war file called hello.war of it and then deploy it on your tomcat you have successfully written and deployed your first webapp...you can reach it under LOCALHOST:8080/hello/sayHello (if you have a local tomcat running)For further reading:There are many many tutorials on the web, but i would also recommend reading the servlet and the jsp specification from sun...Link to Servlet SpecLink to JSP specification have fun... Share this post Link to post Share on other sites