我的第一个servlet.

类别:Java 点击:0 评论:0 推荐:

郁闷了2小时,感谢tomcat自带的servlet-sample帮我搞定

建立主目录,在主目录下建立WEB-INF文件夹,在WEB-INF下建立classes文件夹和web.xml文件,在web.xml中设置servlet指向,然后把生成的class放在classes目录下.
代码如下:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class wap86test extends HttpServlet {

public void service (HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
response.setContentType("text/vnd.wap.wml");
PrintWriter out = response.getWriter();
out.println("<?xml version=\"1.0\"?>");
out.println("<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\"");
out.println(" \"http://www.wapforum.org/DTD/wml_1.1.xml\">");
out.println("<wml>");
out.println("<card title=\"Hello,wap86\">");
out.println(" <p align=\"center\">");
out.println("Date and Time Service<br/>");
out.println("Date is: "+ new java.util.Date());
out.println("</p>");
out.println("</card>");
out.println("</wml>");
}
}

web.xml做如下配置:

<servlet> 
<servlet-name>wap86test.test</servlet-name>  
<servlet-class>wap86test</servlet-class>                         
 </servlet>                            

 <servlet-mapping>              
<servlet-name>wap86test.test</servlet-name>     
 <url-pattern>/wap86test</url-pattern>       
 </servlet-mapping> 

关于web.xml的功用以后在说,现在一知半解.

本文地址:http://com.8s8s.com/it/it11811.htm