Without using the IED tool, write a servlet program and publish it to the tomcat server. Finally, access it through a browser.
There are four steps:
Step 1: Download tomcat and create an application under the webapps directory of tomcat. Then, create the classes folder in the application and write the corresponding java file.
Step 2: Use javac to compile the java file compiled above
Step 3: compile a web. xml file that provides external access
Step 4: start tomcat and access servlet through a browser
Bytes ---------------------------------------------------------------------------------------------------------------
Step 1: Download a tomcat file and put it in the specified drive letter. (for example, put tomcat in the D: \ tomcat directory)
A. Enter the tomcat webapps directory D: \ tomcat \ apache-tomcat \ webapps
B. Create a folder named "servletDemo".
C. Create a new WEB-INF folder in the servleetDemo folder (PS: note the WEB-INF, not WEB_INF)
D. Create a new classes folder in the WEB-INF folder ..
Okay. Our program will be written in the classes folder. Use the file editor to create a firstServlet. java
FirstServlet. java
Package com. servlet. demo; import java. io. *; import javax. servlet. *; public class firstServlet extends GenericServlet {public void service (ServletRequest req, ServletResponse res) throws ServletException, java. io. IOException {OutputStream out = res. getOutputStream (); out. write ("Hello servlet ". getBytes ());}}
Step 2: Use javac to compile the above firstServlet. java
After compiling the firstServlet. java file, you must use javac to edit it.
Go to CMD. Switch to D: \ tomcat \ apache-tomcat \ webapps \ servletDemo \ WEB-INF \ classes Directory
Use the dir command to view the files in the current directory:
The firstServlet. java in the red box is the java file to be compiled using javac.
In DOS, enter javac-d. firstServlet. java to compile and view the result:
If the above error occurs, it is because javac only uses the packages in javase during compilation, and our program involves servlet-related packages, but it is not found during javac, as a result, an error is reported during compilation.
So we need to add the servlet-related jar to the current classpath environment, just need to add the servlet-api.jar package in the tomcat directory lib to the current environment variable.
The above red box is to add the servlet-api.jar to the current environment variable.
(
PS: if the following error is reported during javac-d. firstServlet. java:
Error: The Main class com. sun. tools. javac. Main cannot be loaded.
It indicates that there is a problem with the JDK and JRE installation directories. In this case, you need to uninstall your JDK and reinstall it. When installing JDK, pay attention to it, JDK and JRE are divided into two directories:
)
After you use javac to correctly compile the firstServlet. java file, you can see that a column of directories is automatically added in the classes Directory:
At this time, the firstServlet. class file we need is ready.
Step 3: compile the web. xml file to provide external mappings for accessing firstServlet. class.
Add a web. xml text in the WEB-INF directory with the following content:
<? Xml version = "1.0" encoding = "ISO-8859-1"?> <Web-app xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version = "2.5"> <servlet-name> firstServlet </servlet-name> <servlet-class> com. servlet. demo. firstServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name> firstServlet </servlet-name> <url-pattern>/firstServlet </url-pattern> </servlet-mapping> </web-app>
In this way, start the tomcat server, find the corresponding D: \ tomcat \ apache-tomcat \ bin \ startup. bat in the tomcat directory, and double-click start .....
After successful startup, it looks like the following:
Step 4: access through a browser:
Check whether tomcat is started normally. If you enter http: // localhost: 8080, the following interface is displayed, indicating that your tomcat has started properly.
Then enter the firstServlet path to access: http: // localhost: 8080/servletDemo/firstServlet. If you can see the following interface, it indicates that your first servlet has been written and published successfully.
Compile servlet program manually