executing servlets

advertisement
SERVLETS
Installation Of Tomcat 7.0.35
1) Before Installing Tomcat, first install the latest version of Java JDK.
http://www.oracle.com/technetwork/java/javase/downloads/index.html
While creating this ppt, latest version of JDK 7 was downloaded from :
http://download.oracle.com/otn-pub/java/jdk/7u11-b21/jdk-7u11windows-x64.exe
Documentation :
http://docs.oracle.com/javase/7/docs/api/index.html
2) Install Tomcat 7.0.35
http://tomcat.apache.org/
While creating this ppt, latest version of Tomcat was downloaded from :
http://apache.techartifact.com/mirror/tomcat/tomcat-7/v7.0.35/bin/apache-tomcat7.0.35.exe
Documentation :
http://tomcat.apache.org/tomcat-7.0-doc/servletapi/index.html
Installing Apache Tomcat
Please select all the options.
3) Setting Of Environment Variables.
(Setting of Environment Variables is required only for the first time.)
- Right Click On My Computer.
- Select Properties
- Click On Advanced Tab.
- And then click on Environment Variables.
4) Environment Variables Dialog Box will appear.
5) Now there are four Environment Variables that we have to set. If
the variables are already present, don’t erase the contents, just
put a semicolon “;” and then write your path. (For Eg :
C:\Program Files;C:\WINDOWS). If Variable Is Not Present,
Select New. See further slides for clarifaction.
In this case, since PATH is
already present, to add your
own path, select PATH, and
click on “Edit”. Put a
semicolon at end, and then
put your own path.
If PATH was not present,
select “New”, And add your
path.
Following Environment variables are to be
set by user(copy paste the path from address bar).
1) PATH = The path of bin directory of JAVA.
2) JAVA_HOME = Top level directory of JAVA.
3) CATALINA_HOME = Top level directory of Tomcat 7.0
4) CLASSPATH = servlet-api.jar file of Folder : “\Apache Software
Foundation\Tomcat 7.0\lib”
(In This Case)
1)
PATH = C:\Program Files\Java\jdk1.7.0_11\bin
2) JAVA_HOME = C:\Program Files\Java\jdk1.7.0_11
3) CATALINA_HOME = C:\Program Files\Apache Software
Foundation\Tomcat 7.0
4) CLASSPATH =C:\Program Files\Apache Software Foundation\Tomcat
7.0\lib\servlet-api.jar;.
(Note a “dot & semicolon” at end of CLASSPATH”)
Note : (When setting the CLASSPATH
variable, PLEASE put ;. (semicolon &
dot )after the path. For Example :
C:\Program Files\Apache Software
Foundation\Tomcat 7.0\lib\servletapi.jar;.
If you don’t do this, you wont be able to
run any java files with main().)
6) To verify whether Environment variables are set properly or not.
Go to command prompt and verify using “set” command.
7) EXECUTING SERVLET PROGRAMS :
Write the program in Notepad and save it in directory :
“C:\Program Files\Apache Software Foundation\Tomcat
7.0\webapps\examples\WEB-INF\classes”
Note :
(If you save the file in bin directory of java, and then
compile it, then save the .class file in “C:\Program
Files\Apache Software Foundation\Tomcat
7.0\webapps\examples\WEB-INF\classes”)
SAMPLE PROGRAM TO DISPLAY A TEXT IN A BROWSER
HelloServlet.java file
import java.io.*;
import javax.servlet.*;
public class HelloServlet extends GenericServlet
{
public void service(ServletRequest request, ServletResponse
response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<B>Hello Servlet !!!");
pw.close();
}
}
8) Set the current directory in command prompt as
C:\Program Files\Apache Software Foundation\Tomcat
7.0\webapps\examples\WEB-INF\classes
by typing “cd (space)“ in command prompt and dragging the
“classes” folder in Command Prompt.
OR
You can also type the whole path.
9) Compile the java file using “javac”
After compiling, it will create a .class file
10) Editing web.xml file: The web.xml file is present in
C:\Program Files\Apache Software Foundation\Tomcat
7.0\webapps\examples\WEB-INF
Important Note :
(Before editing web.xml file, create a backup of that file somewhere
else so that if something wrong is written in that file, you can again
replace it.)
Open the web.xml file using Notepad for editing.
Following file will open.
11) Find <servlet> tag, copy the first 4 lines (till </servlet>) and paste
it just below it. Then change the name of class to your own class
(Here, HelloServlet).
For Example : (First 4 lines are already present, copy it, paste it just below
it and change its class name to your own class name. Don’t leave any
blank lines between corresponding servlet tags.)
<servlet>
<servlet-name>ServletToJsp</servlet-name>
<servlet-class>ServletToJsp</servlet-class>
</servlet>
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>HelloServlet</servlet-class>
</servlet>
12) Then Find <servlet-mapping> tag, copy the first 4 lines (till </servletmapping> and paste it just below it. Then change the name of class to
your own class (Here, HelloServlet).
For Example :
<servlet-mapping>
<servlet-name>CompressionFilterTestServlet</servlet-name>
<url-pattern>/CompressionTest</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
Please notice that there is a slash ‘/’ before HelloServlet in url pattern.
13) Then save the file, and close it.
14) Starting Tomcat 7.0
Go to Start -> All Programs -> Apache Tomcat 7.0 Tomcat7 -> Configure Tomcat
15) Following window will open. Click on Start. And After that, on
OK.
16) Now open a web browser and type : http://localhost:8080/
If Apache Tomcat opens, then it means you've setup Tomcat successfully.
17) Now Type :
http://localhost:8080/examples/YourProgramName
Here : http://localhost:8080/examples/HelloServlet
18) After executing servlets, close the browser and then again
open Configure Tomcat.
Then click on “Stop”
Download