为了验证自己的servlet服务的环境是否ok,自己在百度上找了好多的资源,感觉这一份的资源简单实用,很适合新手。
首先在建了一个web的文件后,在java source下的src包建一个com.till包,然后在资源的下面见一个类ForServletfirst的类。具体的代码如下:(环境时eclipse)
package com.cvil.boanda;
import java.io.IOException;
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
public class ForServerFirst extends HttpServlet {
/** * */ private static final long serialVersionUID = 1L;
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); }
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().print("Hello World!"); }
}
在建好代码后要注意web.xml的配置,这个时很很重要的。
<?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/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.4"> <servlet> <servlet-name>first</servlet-name> <servlet-class>com.cvil.boanda.ForServerFirst</servlet-class> </servlet> <servlet-mapping> <servlet-name>first</servlet-name> <url-pattern>/TestClient</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
</web-app>
下面附上图片:
ForServletfirst类:
web.xml的配置:
运行的结果:
|
请发表评论