一、Servlet初窥
1.手工编译Servlet
% cd YourWorkspace/FirstServlet
% javac -classpath Yourlibrary/YourTomcat/lib/servlet-api.jar -d ./classes src/cc/openhome/HelloServlet.java
2.使用标注(Annotation):@Webservlet("name.suffix")
标注在Java EE 6中的Servlet 3.0之后才有的功能,可以省去在web.xml中部署,但如果在web.xml部署则会覆盖标注。
@WebServlet(
name="HelloServlet",
uerlPatterns={"/hello.view"},
loadOnStartup=1
)
public class HelloServlet extends HttpServlet {
3.使用web.xml
<?xml version="1.0" emcoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.ory/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>HellServlet</servlet-name>
<servlet-class>cc.openhome.HelloServlet</servlet-class>
<load-on-startup>1</load-on-strtup>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello.view</url-pattern>
<servlet-mapping>
</web-app>
4.文件组织与部署
生成的WAR文件放在Tomcat目录下的webapps中,然后运行bin目录下的startup启动Tomcat。
5.URL模式设置
requestURI = contextPath + servletPath + pathInfo