1 <%@page contentType="text/html" pageEncoding="UTF-8"%> 2 <!DOCTYPE html> 3 <html> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 6 <title>数据输入页面</title> 7 </head> 8 <body> 9 <form action="servlet/Verify" method="post"> 10 请输入一个实数:<input type="text" name="number"> 11 <input Type="submit" value="确定"> 12 </form> 13 </body> 14 </html>
1 package servlet; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.RequestDispatcher; 7 import javax.servlet.ServletException; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 public class Verify extends HttpServlet { 13 public Verify() { 14 super(); 15 } 16 17 public void destroy() { 18 super.destroy(); // Just puts "destroy" string in log 19 } 20 21 public void doGet(HttpServletRequest request, HttpServletResponse response) 22 throws ServletException, IOException { 23 doPost(request, response); 24 } 25 26 public void doPost(HttpServletRequest request, HttpServletResponse response) 27 throws ServletException, IOException { 28 String number = request.getParameter("number"); 29 try { 30 // 把字符串转换为Double 31 double n = Double.parseDouble(number); 32 if (n > 6000 || n < -6000) { 33 // 重定向到sendForward.jsp 34 response.sendRedirect("sendForward.jsp"); 35 } else { 36 RequestDispatcher rd = request.getRequestDispatcher("ShowMessage"); 37 // 转发到另一个servlet文件 38 rd.forward(request, response); 39 } 40 } catch (NumberFormatException ex) { 41 // 重定向到sendForward.jsp 42 response.sendRedirect("sendForward.jsp"); 43 } 44 } 45 46 public void init() throws ServletException { 47 } 48 }
1 package servlet; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 public class ShowMessage extends HttpServlet { 12 public ShowMessage() { 13 super(); 14 } 15 16 public void init() throws ServletException { 17 } 18 19 public void doGet(HttpServletRequest request, HttpServletResponse response) 20 throws ServletException, IOException { 21 doPost(request, response); 22 } 23 24 public void doPost(HttpServletRequest request, HttpServletResponse response) 25 throws ServletException, IOException { 26 response.setContentType("text/html; charset=UTF8"); 27 PrintWriter out = response.getWriter(); 28 // 获取客户提交的信息 29 String number = request.getParameter("number"); 30 double n = Double.parseDouble(number); 31 out.println(number + "的平方为:" + (n * n)); 32 } 33 34 public void destroy() { 35 super.destroy(); // Just puts "destroy" string in log 36 } 37 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> 3 <web-app> 4 <servlet> 5 <servlet-name>Verify</servlet-name> 6 <servlet-class>servlet.Verify</servlet-class> 7 </servlet> 8 <servlet> 9 <servlet-name>ShowMessage</servlet-name> 10 <display-name>This is the display name of my J2EE component</display-name> 11 <description>This is the description of my J2EE component</description> 12 <servlet-class>servlet.ShowMessage</servlet-class> 13 </servlet> 14 15 <servlet-mapping> 16 <servlet-name>Verify</servlet-name> 17 <url-pattern>/servlet/Verify</url-pattern> 18 </servlet-mapping> 19 <servlet-mapping> 20 <servlet-name>ShowMessage</servlet-name> 21 <url-pattern>/servlet/ShowMessage</url-pattern> 22 </servlet-mapping> 23 </web-app>
时间: 2024-10-06 08:47:37