Hello Friends,
This tutorial is for the starters who want to know how exactly Servlet can be written and executed on the browser. In this, we will create a simple servlet program – Hello World.
First of all, build the Directory tree structure anywhere you want. For now I am creating it in my C Drive like –
Create a java file and save it in src folder and similarly create as web.xml file if etc folder. Classes folder is the one where .class file will be saved after compilation.
Now its time to add the code in the java file. We will discuss each elements from the below code in the later tutorials. For now you can copy and paste the below code.
FirstServlet.java file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class FirstServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Hello World</h1>"); out.println("</body>"); out.println("</html>"); } } |
web.xml file
1 2 3 4 5 6 7 8 9 10 11 |
; html-script: true ]<?xml version="1.0" encoding="ISO-8859-1"?> <web-app> <servlet> <servlet-name>First Servlet</servlet-name> <servlet-class>FirstServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>First Servlet</servlet-name> <url-pattern>/Servlet1</url-pattern> </servlet-mapping> </web-app> |
Now create another folder structure inside the tomcat directory in webapps folder. The directory structure will be as:
C:\\Tomcat\\webapps\\code2java\\WEB-INF\\classes
Once the folder structure is created, put the web.xml file in the WEB-INF folder as shown above. Also,put the .class file, which we will generate from FirstServlet.java file, in the classes folder. Note that web.xml file and classes folder are both at same hierarchy in WEB-INF folder.
Now, we will compile the servlet file. The file FirstServlet. java can be compiled just like any other java file (e.g %path%>javac filename.java), but as we will move ahead with servlets this type of compilation will not work. So, we have to use following method for compilation.
Open the command prompt (windows + R) and go to the path of the parent folder where we created java file and xml file. In this case we have the path C:code2java. Now type the following command in cmd prompt.
Root folder path>javac –classpath “path of tomcatlibservlet-api.jar ; dependencies class file path “ –d “path for class file” “path for .java file”
In the above command, javac is the java compiler command, -classpath refers to the path of servlet-api.jar file and -d is the path where the .class file should be saved after compilation (in this case the .class file will be saved in classes folder). And at last there is the path for java file which is stored in src folder. In the -classpath there is one extra value- “dependencies class file path” which is added after the jar file path divided by a “;”. This indicates the path for any class file that may be used in the java file we are compiling.
Now put the generated .class file in the directory structure C:Tomcatwebappscode2javaWEB-INF classes i.e inside the classes folder and web.xml file in the C:Tomcatwebappscode2javaWEB-INFclasses so that it will be in same hierarchy with that of classes folder.
After this, restart the tomcat server and run the link- localhost:8080/code2java/Servlet1. It Should display-Hello World
Hope this will help you.
Regards,
admin@code2java
Sir I create a servlet program code is
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class Hello extends HttpServlet
{
//@override
public void doGet(HttpServletRequest request,HttpServletResponse response)
{
try{
PrintWriter out = response.getWriter();
out.println(“hello this is my first servlet program”);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
this code is compiled but it is not running while i have root structure just like as “ROOT>WEB-INF>web.xml file and classes folder” inside classes folder this is stored
what error you getting while running the program?
Hi Amit,
What error you are getting while you run the program?