You are on page 1of 3

How to execute the Servlet program using Tomcat Apache Server?

Basic Requirements:
1. Jdk1.7.0
2. Tomcat Apache server 6.0
3. Web Browser(Any standard java compactible Browser)

Files:
1. .html file(pass1.html)
2. .java file(pass1.java)
3. web.xml(web.xml)

Steps:
1. Create the directory as(tyif) under Apache Software Foundation\Tomcat 6.0\webapps folder
of Apache Software Foundation.
2. Create .html(pass1.html) file under tyif directory.

//pass1.html
<html>
<body>
<form action="pass1">
name:<input type="text" name="t1">
password:<input type="password" name="t2">
<input type="submit" value="login">
</form>
</body>
</html>
3. Copy the WEB-INF folder from webapps\examples.
4. Paste this in tyif directory.
5. Goto WEB-INF/classes folder and create .java(pass1.java) file.

//pass1.java (Sevlet Program)


import javax.servlet.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
public class pass1 extends GenericServlet
{
public void service(ServletRequest req,ServletResponse res) throws ServletException,
IOException
{
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
String un="abc";
String pwd="xyz";
String p1=req.getParameter("t1");
String p2=req.getParameter("t2");
if((p1.equals(un))&&(p2.equals(pwd)))
{
pw.println("<h1>welcome to home page</h1>");
}
else
pw.println("invalid username or password");
pw.close();
}
}
6. Copy the servlet-api.jar file from Apache Software Foundation\Tomcat 6.0\lib and paste it
on Java\jdk1.7.0\jre\lib\ext directory.
7. Copy the pass1.java file from classes folder & paste it in Java\jdk1.7.0\bin.
8. Compile the java program i.e. pass.java
9. It will get the pass1.class from in same directory.
10. Paste this pass1.class file into classes folder.
11. Add Configuration code given below in /WEB-INF/web.xml file & save the file.
//web.xml
<servlet>
<servlet-name>pass1</servlet-name>
<servlet-class>pass1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>pass1</servlet-name>
<url-pattern>/pass1</url-pattern>
</servlet-mapping>

12. Start the Tomcat 6. 0 server from given directory i.e. Apache Software Foundation\Tomcat
6.0\bin.
13. Open the browser type: http://localhost:8080/tyif/pass1.html

14. Enter username & password click on login button, if the user name & password is correct then
will get the servlet response as Welcome to homepage else invalid username or password.

15. If you get 404 response code it means you have done mistake in configuring web.xml file.

Prepared By,
Jadhav Santosh B.
Lecturer Gramin Polytechnic Nanded

You might also like