日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

重定向与转发

發布時間:2023/12/10 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 重定向与转发 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用重定向方法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>

?

轉載于:https://www.cnblogs.com/xh0102/p/5721659.html

總結

以上是生活随笔為你收集整理的重定向与转发的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。