呵呵,想来第一个servlet实例

在学习任何语言,之前,我们都喜欢来一个“hello world”!~ 不是有一个笑话,一个来程序员,在年老的时候,觉得无聊,买来毛笔开始练习写毛笔字,一切都准备好了,你猜写的第一个字是什么,呵呵呵—-“hello word”。

先来看看代码:

———————————————————————–

package ynu.sanwen.gaga;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FirstHello extends HttpServlet {

private static final long serialVersionUID = -7874083763768998126L;

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType(“text/html”);
PrintWriter out = response.getWriter();
out.println(“<!DOCTYPE HTML PUBLIC \”-//W3C//DTD HTML 4.01 Transitional//EN\”>”);
out.println(“<HTML>”);
out.println(“  <HEAD><TITLE>hello world</TITLE></HEAD>”);
out.println(“hello world”);
out.println(“</HTML>”);
out.flush();
out.close();
}

}

———————————————————————————–

配置文件:

————————————————————————————

<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app version=”2.5″
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“>
<servlet>
<servlet-name>Test</servlet-name>
<servlet-class>ynu.sanwen.gaga.FirstHello</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/firstHello.do</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

———————————————————————————

代码下载:gagaj2ee001

查看效果:

共有 0 条评论

Top