|
|
|
|
![]() ![]() |
Oct 9 2004, 08:01 AM
Post
#1
|
|
|
Premium Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 189 Joined: 9-October 04 Member No.: 1,550 |
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 |
|
|
|
Oct 9 2004, 03:20 PM
Post
#2
|
|
|
Newbie [Level 1] ![]() Group: Members Posts: 10 Joined: 9-October 04 Member No.: 1,570 |
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://java.sun.com/developer/onlineTraini...s/Fundamentals/ 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 |
|
|
|
Oct 10 2004, 03:10 AM
Post
#3
|
|
|
Member [Level 1] ![]() ![]() ![]() ![]() Group: Members Posts: 51 Joined: 24-September 04 Member No.: 1,256 |
good advice!
But have you got a servlet container for your code? and do you know how to deploy the code? |
|
|
|
Oct 10 2004, 11:56 AM
Post
#4
|
|
|
Newbie [Level 1] ![]() Group: Members Posts: 19 Joined: 10-October 04 Member No.: 1,626 |
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(); } } //////////////////////////////////////////////// |
|
|
|
Oct 10 2004, 12:07 PM
Post
#5
|
|
|
Newbie [Level 1] ![]() Group: Members Posts: 19 Joined: 10-October 04 Member No.: 1,626 |
To deploy this code you need
- Sun Application Server or - Tomcat I advise you to download Tomcat from this site http://jakarta.apache.org/tomcat/ and read tutorial. You need serveral hours to learn about it -- eldeo |
|
|
|
Oct 26 2004, 06:46 PM
Post
#6
|
|
|
Newbie [Level 1] ![]() Group: Members Posts: 11 Joined: 25-October 04 Member No.: 1,949 |
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 under http://localhost: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.. etc a 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: CODE <?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <description> An examplatory web.xml file </description> <servlet> <servlet-name>helloWorld</servlet-name> <servlet-class>my.java.package.HelloWorldServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>helloWorld</servlet-name> <url-pattern>/sayHello</url-pattern> </servlet-mapping> </web-app> If you write a servlet class my.java.package.HelloWorldServlet now, place it under WEB-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 http://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 Spec Link to JSP specification have fun... |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 22nd November 2008 - 09:58 AM |