建工程步骤跟“第一个web”一样,下面是几个修改的地方。
1、新建HelloWorld类,代码如下:
HelloWorld.java
- import java.io.*;
- import java.text.*;
- import java.util.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- public class HelloWorld extends HttpServlet { //扩展HttpServlet类
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws IOException, ServletException //重载doGet()方法
- {
- response.setContentType("text/html");
- PrintWriter out = response.getWriter();
- out.println("");
- out.println("");
- out.println("Hello World");
- out.println("");
- out.println("");
- out.println("
Hello World
");
- out.println("
");
- out.println("");
- }
- }
修改web.xml文件
- <?xml version="1.0" encoding="ISO-8859-1"?>
- <web-app xmlns=""
- xmlns:xsi=""
- xsi:schemaLocation=" /web-app_2_4.xsd"
- version="2.4">
- <servlet>
- <servlet-name>HelloWorld</servlet-name>
- <servlet-class>HelloWorld</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>HelloWorld</servlet-name>
- <url-pattern>/hello</url-pattern>
- </servlet-mapping>
- </web-app>
访问地址,即能运行程序。
阅读(1544) | 评论(0) | 转发(0) |