首先从一个最简单的Web程序开始
一个最简单的Web程序最重要的是什么呢,Servlet。
下面是The Java EE 6 Tutorial给的解释:
A servlet is a Java programming language class used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers. For such applications, Java Servlet technology defines HTTP-specific servlet classes.
The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing servlets. All servlets must implement the Servlet interface, which defines lifecycle methods. When implementing a generic service, you can use or extend the GenericServlet class provided with the Java Servlet API. The HttpServlet class provides methods, such as doGet and doPost, for handling HTTP-specific services.
大意就是:1、扩展服务器 通过请求-响应编程模型访问主机应用程序 的能力。
2、通常用于扩展Web服务器托管的应用程序,尽管Servlet可以响应任何类型的请求
3、对于大多数应用程序来说,Java Servlet技术定义了专门用于HTTP协议的Servlet类
4、所有的Servlet类都要实现Servlet接口,这个接口定义了生命周期方法
5、GenericServlet 通用服务继承它
6、HttpServlet 处理特定的HTTP服务继承它
Servlet要起作用需要在web.xml中添加对Servlet的映射,用以访问。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Demo</display-name>
<welcome-file-list>
<welcome-file>hello.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>Demo</display-name>
<servlet-name>demo</servlet-name>
<servlet-class>com.jcombat.sample.DemoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>demo</servlet-name>
<url-pattern>/demo</url-pattern>
</servlet-mapping>
</web-app>