Jump to content
xisto Community
Sign in to follow this  
hansley

Java Servlets Can anyone help me?

Recommended Posts

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

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

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

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
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:



[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 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 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...


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.