Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Java Servlets, Can anyone help me!?
hansley
post 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
Go to the top of the page
 
+Quote Post
arp
post 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
Go to the top of the page
 
+Quote Post
sanweikui
post 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?
Go to the top of the page
 
+Quote Post
eldeo
post 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();
}
}
////////////////////////////////////////////////
Go to the top of the page
 
+Quote Post
eldeo
post 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
Go to the top of the page
 
+Quote Post
björn
post 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...
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. convert .java to .c(5)
  2. [help] Java Script: Window.open(10)
  3. Java Mobile Applications(7)
  4. File Upload Script In Java(5)
  5. How To Create Java Button Or Frame(14)
  6. Hotspot Virtual Machine Exception_access_violation(5)
  7. Java For The Beginning Programmer Ebook(4)
  8. What's The Relationship Between Javascript And Java(7)
  9. Best Java Framework For J2ee?(8)
  10. Jsp Or Java Chat Script Like Mig33(5)
  11. Helpful Registry Edit For Java Programmers(3)
  12. Firefox Not Loading Java(2)
  13. Java Applet Query(2)
  14. Budding Java Game Developers?(10)
  15. Java Or C++(10)
  1. Learn Java Programming Language Online Step By Step(1)
  2. Call Pdf995 From Java(0)
  3. How To Implement Single Instance Application On Java(0)
  4. Java Multithreading Issues(2)
  5. Java And Xml: Links You Must Have(1)
  6. Java De/compiler(5)
  7. Java Obfuscator(1)
  8. Which Is Good Java Or Dot Net(3)
  9. Java Script To Hide The Url In Address Bar(6)
  10. Fun In Sun (java)(3)
  11. Java And Jsp On Trap 17(2)
  12. Which Is Faster Today Java Or Php Or .net Technology(7)
  13. What Is The Basic Diiference In Java Script And Vbscript(0)


 



- Lo-Fi Version Time is now: 22nd November 2008 - 09:58 AM