servletdemo5.java代码:
package com.haidi8.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.Random; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class servletdemo5 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setIntHeader("refresh", 1); Random r= new Random(); response.getWriter().write(r.nextInt()+""); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
web.xml代码:
<?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"> <display-name></display-name> <servlet> <servlet-name>servletdemo5</servlet-name> <servlet-class>com.haidi8.servlet.servletdemo5</servlet-class> </servlet> <servlet-mapping> <servlet-name>servletdemo5</servlet-name> <url-pattern>/servletdemo5</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
运行效果: