I will show you how to config your PC for Java Servlet and JSP programming.
I am asumming tha you have
- OS-Windows 7
- JDK 1.5 or later
- Tomcat -version 7 install on your machine.
- port number:1001
Setp1: Install JDK 1.5 or later and Tomcat 7.
Note: just dont forget to change the HTTP port nuber form 8080 to 1001 for avoiding port number config if you are using IIS for ASP.
Step2: now Once you have finished installation we are ready to go.Now we are going to check weather tomcat is working or not.For that first Go to "C:\Program Files\Apache Software Foundation\Tomcat 7.0\bin" and run Tomcat7.exe. it will start your tomcat server.
Setp3: Now open your browser and type "http;//localhost:1001" and u will see a webpage like this.
Step3: Now we will write a small code for Servlet and will run it.
First create java file name Hello.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Hello extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Hello World!</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello World!</h1>");
out.println("</body>");
out.println("</html>");
}
}
Now Compile it using
javac -classpath "C:\Program Files\Apache Software Foundation
\Tomcat 7.0\lib\servlet-api.jar" Hello.java
Now put the class file Hello.class into
C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps
\ROOT\WEB-INF\classes\
Note:Here you have to create a folder name classes.
Now we have to edit web.xml file which is located at
C:\Program Files\Apache Software Foundation\Tomcat 7.0
\webapps\ROOT\WEB-INF\
in web.xml file put bellow code in between <web-app></web-app> tags.
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>Hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>
Note:Here i have given my servlet name same as class name of Hello.java file
or say as name of java file.
now save web.xml file.
Now open your browser and write "http://localhost:1001/Hello"
And you will see Hello World! on your browser page.
so this is a short intro about how to config and run simple servlet.
If you have any query or want to learn more specific you can post your problem
here or you can mail me on my id.I will try my best to solve it.
Hope you have learn something useful.