Welcome to my new post,
Here I have done hello world example using java servlet. Here are some requirements before doing this example.
Requirements:
1. JDK 1.6 or 1.7.
If not installed, download and install it.
Download JDK here.
2. Eclipse IDE.
If not installed, download and install it.
Download Eclipse here.
3. Apache Tomcat 7(recommended) or Tomcat 6.
If not installed, download and install it.
Download Tomcat here.
4. Basic knowledge of Java.
If do not have,learn basics.
Learn Basic Java here.
(Note: Before starting this tutorial I am assuming that all requirements are fulfilled by you.)
Step 1: Open Eclipse IDE click on File>New>Dynamic Web Project.
(if not found dynamic web project in 'New' click on 'other'.)
Step 2: Now, Name you project 'SevletHelloWorld'.
Step 3: Click to Next> two times, you will see below window.
Step 4: Copy following .jar file to lib folder located in "WebContent>WEB-INF>lib".
Step 5: Right Click on project click to "New>Class".
Here I have done hello world example using java servlet. Here are some requirements before doing this example.
Requirements:
1. JDK 1.6 or 1.7.
If not installed, download and install it.
Download JDK here.
2. Eclipse IDE.
If not installed, download and install it.
Download Eclipse here.
3. Apache Tomcat 7(recommended) or Tomcat 6.
If not installed, download and install it.
Download Tomcat here.
4. Basic knowledge of Java.
If do not have,learn basics.
Learn Basic Java here.
(Note: Before starting this tutorial I am assuming that all requirements are fulfilled by you.)
Step 1: Open Eclipse IDE click on File>New>Dynamic Web Project.
(if not found dynamic web project in 'New' click on 'other'.)
Step 2: Now, Name you project 'SevletHelloWorld'.
Step 3: Click to Next> two times, you will see below window.
- Tick on Generate web.xml deployment descriptor, it will be explained further.
- Now,click in 'Finish'.
- Now Your project Structure is ready,lets start development.
Step 4: Copy following .jar file to lib folder located in "WebContent>WEB-INF>lib".
- Download only Servletapi-2.4.jar.
- Download .jar file, Download here.
- Download and put it into lib folder.
Step 5: Right Click on project click to "New>Class".
- Set Class Name "HelloWorldServlet".
- Click to finish without doing anything else.
Step 6: Now Write following code in HelloWorldServlet.
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HelloWorldServlet extends HttpServlet{ public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException{ response.setContentType("text/html"); PrintWriter out=response.getWriter();
out.println("Hello world."); } }
//*It will look something like this.
Step 7: Now change in web.xml file located in "WebContent>WEB-INF".
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>ServletHelloWorld</display-name> <servlet> <servlet-name>ServletHelloWorld</servlet-name> <servlet-class>HelloWorldServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletHelloWorld</servlet-name> <url-pattern>/index.html</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
//*It will look something like this.
Step 8: You project is ready now it's time to run it.
(Note: I am assuming that you have already installed apache tomcat).
- Right Click on project "Run as> Run on Server >".
Step 9: Select apache tomcat then select your installed apache tomcat version.
- I have used tomcat 7.
- Click on Finish
- If everything goes correct you will see following output.
Step 9: Select apache tomcat then select your installed apache tomcat version.
To Download complete project,Click here.
No comments:
Post a Comment