It took me almost two months to get the first servlet to run, from the beginning of the JDK to the end of the process, and it was really a lot harder to create the first servlet program than to create the first ASP, but it doesn't matter, but before I forget, It's a lot easier next time you come back!
1. Download and install JDK5.0 or JDK1.4.2
Do not question the version number, according to my understanding, JDK5.0 is JDK1.5, is the important upgrade of JDK1.4.2, which adds a lot of new language features, why call 5.0 instead of 1.5, I think it may be out of business considerations, this does not need to pay more attention, I always like new things , so the download is JDK5.0,
Under the address is:http://java.sun.com/j2se/1.5.0/download.jsp
Documentation also want to download Oh, otherwise a lot of help will be invisible.
After the completion of the download you may encounter the same problem with me, download the file has a strange name, and can not run, this problem I wasted hours, and then do well, and now tell you how to solve: The download of the JDK file name, while the extension to change. EXE can be installed, the download documentation file also changed the name, the extension is changed. ZIP, then unzip, so the first step is OK.
I set the installation path to D:\java , you can set the settings according to your own situation, all the configuration to change the appropriate, there will be two folders after installation, one is jdk , one is JRE In the following we will use jdk . such as:
2, configure the environment variable
Set the environment variable after installing the JDK, right-click My Computer, properties, advanced Environment variables My computer is with the following settings:
New variable
javahome = D:\Java\jdk1.5.0_02
classpath = .;D: \ Java\jdk1.5.0_02\lib Note: The front point cannot be
Edit the variable path , add on the last side; ;D: \java\jdk1.5.0_02\bin
Your specific installation location may be different from mine, according to the corresponding location modification. When the environment variable is configured, you can run both the Java and javac commands under the console, and the configuration is correct if the parameter prompt help appears.
3. Download and configure Tomcat
:http://jakarta.apache.org/tomcat/index.html
After downloading the unzip, I will extract the relevant files to F:\Tomcat , you can set the location according to their own situation, all the configuration to change the corresponding changes on the line, and the above similar
New variable tomcat_home = F:\Tomecat
Edit the variable CLASSPATH, add it on the last side ; F:\Tomcat\lib
At this point you can run the F:\Tomcat\bin\startup.bat in the console and then enter http://localhost:8080 in the browser, if the Tomcat Welcome page is displayed, then Tomcat is ready to work properly.
Note: The most important step here is to write a Servlet.
Copy the Servlet-api.jar(or Servlet.jar) under the F:\Tomcat\common\lib to D:\Java\jdk1.5.0_02\ Jre\lib\ext below, this step must be done, the package contains many of the required classes, otherwise the Java compiler can not find them.
4, of course, the need for a good IDE tools, recommended now the most fire Eclipse
:http://www.eclipse.org/downloads/index.php
Download after the decompression can be used directly, do not need to install, welcome interface is cool,
Take a look at the second picture above, Java installed after the two folders, a JDK, a JRE, writing a general program with Eclipse default JRE package is enough, but to write a Servlet must set the path of the JDK, because we just put servlet-a Pi.jar is placed in the folder below the JDK, select Window-〉preferences in the menu.
In the popup dialog box, locate java-〉installed JREs, we can see that there is already a default configuration of JRE, click the Add button, follow the path of your JDK and documentation installation, then click OK. So the second step is done.
5. Console Hello World
You can familiarize yourself with this IDE first, then try to write a Hello world program, create new projects, add Java files and Visula Studio, Delphi are similar, I believe this is difficult for you, the final effect is like this:
6. JSP Hello World
Find folder F:\Tomcat\webapps , this folder is to store a variety of Web programs, you can it below for each separate Web application to create a different folder, here we first create a firstjsp folder, Create a new JSP file in this folder with the following content
Save As time.jsp, and then firstjsp The following to create a new empty Web-inf folder, which is not used here temporarily, but must exist, if not, it will be wrong.
After doing all of this, restart Tomcat and then enter http://localhost:8080/FirstJSP/ and http://localhost:8080/FirstJSP/Time.jsp in the browser. , you can see the results of the run. Note that this is strictly case-sensitive , otherwise you will not get the results you want.
7. Servlet Hello World
Enter the following in Eclipse and compile to get hello.class
Import java.io.IOException;
Import Java.io.PrintWriter;
Import javax.servlet.*;
Import javax.servlet.http.*;
public class Hello extends HttpServlet...{
protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException ... {
Response.setcontenttype ("text/html");
Printwriter out=response.getwriter ();
out.println (" out.println (" out.println ("<body> out.println (" out.flush ();
}
}
Then create a new Firstservlet folder under F:\Tomcat\webapps , and then create a new Web-inf folder in the firstservlet folder. Then complete the following two steps:
The first step, in the web-inf folder to create a new in the Classes folder, just put the hello.class in this folder;
In the second step, create a Web. XML file in the web-inf folder, enter the following and save
<?xml version= "1.0" encoding= "iso-8859-1"? >
<! DOCTYPE web-app public "-//sun Microsystems, INC.//DTD Web application 2.3//en" "HTTP://JAVA.SUN.COM/DTD/WEB-APP_2_3.DTD" >&NBSP;
<web-app>
<servlet>
<servlet-name>yourwantname</servlet-name>
< servlet-class>hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>yourwantname</ servlet-name>
<url-pattern>/my/hello</url-pattern>
</servlet-mapping>
</web-app>
Here to explain,<servlet-name> to represent an alias, go to what name is OK;<servlet-class> is your class name;<url-pattern>/my/ Hello</url-pattern> sets the mode of access.
Final effect
After doing all this reboot Tomcat and then enter Http://localhost:8080/FirstServlet/my/hello in the browser, you can see the results of the run as. Note that this is strictly case-sensitive, otherwise you will not get the results you want. Description: Don't be surprised,My/hello is what we set in the <url-pattern> attribute.
Source: http://myqiao.cnblogs.com/archive/2005/08/29/225497.html