标签:
使用重定向方法sendRedirect()将用户重新定向到一个JSP页面或另一个Servlet。
RequestDispatcher对象调用void forward(ServletRequest request,ServletResponse response)
方法可以将用户对当前JSP页面或Servlet的请求转发给RequestDispatcher对象所指定的JSP页面或Servlet。
jsp文件如下:
1 <%@ page contentType="text/html;charset=GB2312" %> 2 <HTML><BODY ><Font size=2> 3 <FORM action="verifyYourMessage" method=post> 4 输入姓名:<Input Type=text name=name> 5 <BR>输入年龄:<Input Type=text name=age> 6 <BR><Input Type=submit value="提交"> 7 </FORM></BODY></HTML>
两个Java文件如下:
1 package star.moon; 2 import java.io.*; 3 import javax.servlet.*; 4 import javax.servlet.http.*; 5 public class Verify extends HttpServlet 6 { public void init(ServletConfig config) throws ServletException 7 {super.init(config); 8 } 9 public void doPost(HttpServletRequest request,HttpServletResponse response) 10 throws ServletException,IOException 11 { String name=request.getParameter("name"); //获取客户提交的信息 12 String age=request.getParameter("age"); //获取客户提交的信息 13 if(name.length()==0||name==null) 14 { response.sendRedirect("input.jsp"); //重定向 15 } 16 else if(age.length()==0||name==null) 17 { response.sendRedirect("input.jsp"); //重定向 18 } 19 else if(age.length()>0) 20 { try { int numberAge=Integer.parseInt(age); 21 if(numberAge<=0||numberAge>=150) 22 { response.sendRedirect("input.jsp"); 23 } 24 else 25 { RequestDispatcher dispatcher= 26 request.getRequestDispatcher("forYouShowMessage"); 27 dispatcher.forward(request, response); //转发 28 } 29 } 30 catch(NumberFormatException e) 31 { response.sendRedirect("input.jsp"); 32 } 33 } 34 } 35 public void doGet(HttpServletRequest request,HttpServletResponse response) 36 throws ServletException,IOException 37 { doPost(request,response); 38 } 39 }
1 package star.moon; 2 import java.io.*; 3 import javax.servlet.*; 4 import javax.servlet.http.*; 5 public class ShowMessage extends HttpServlet 6 { public void init(ServletConfig config) throws ServletException 7 {super.init(config); 8 } 9 public void doPost(HttpServletRequest request,HttpServletResponse response) 10 throws ServletException,IOException 11 { response.setContentType("text/html;charset=GB2312"); 12 PrintWriter out=response.getWriter(); 13 String name=request.getParameter("name"); //获取客户提交的信息 14 String age=request.getParameter("age"); //获取客户提交的信息 15 try{ byte bb[]=name.getBytes("ISO-8859-1"); 16 name=new String(bb,"gb2312"); 17 } 18 catch(Exception exp){} 19 out.print("<Font color=blue size=4>您的姓名是:"); 20 out.print(name); 21 out.print("<BR><Font color=pink size=3>您的年龄是:"); 22 out.print(age); 23 } 24 public void doGet(HttpServletRequest request,HttpServletResponse response) 25 throws ServletException,IOException 26 { doPost(request,response); 27 } 28 }
web.xml文件如下:
1 <?xml version="1.0" encoding="ISO-8859-1"?> 2 <!-- 3 Licensed to the Apache Software Foundation (ASF) under one or more 4 contributor license agreements. See the NOTICE file distributed with 5 this work for additional information regarding copyright ownership. 6 The ASF licenses this file to You under the Apache License, Version 2.0 7 (the "License"); you may not use this file except in compliance with 8 the License. You may obtain a copy of the License at 9 10 http://www.apache.org/licenses/LICENSE-2.0 11 12 Unless required by applicable law or agreed to in writing, software 13 distributed under the License is distributed on an "AS IS" BASIS, 14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 See the License for the specific language governing permissions and 16 limitations under the License. 17 --> 18 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 19 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 20 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 21 http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 22 version="3.1" 23 metadata-complete="true"> 24 25 <display-name>Welcome to Tomcat</display-name> 26 <description> 27 Welcome to Tomcat 28 </description> 29 30 31 32 33 <servlet> 34 <servlet-name>getSquare</servlet-name> 35 <servlet-class>star.moon.Verify</servlet-class> 36 </servlet> 37 <servlet-mapping> 38 <servlet-name>getSquare</servlet-name> 39 <url-pattern>/verifyYourMessage</url-pattern> 40 </servlet-mapping> 41 42 43 <servlet> 44 <servlet-name>getSquareOrCubic</servlet-name> 45 <servlet-class>star.moon.ShowMessage</servlet-class> 46 </servlet> 47 <servlet-mapping> 48 <servlet-name>getSquareOrCubic</servlet-name> 49 <url-pattern>/forYouShowMessage</url-pattern> 50 </servlet-mapping> 51 52 </web-app>
标签:
原文地址:http://www.cnblogs.com/xh0102/p/5721659.html