最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
jsp response.sendRedirect()用法详解
时间:2025-07-06 13:00:02 编辑:袖梨 来源:一聚教程网
sendRedirect()
response和request一样都是jsp内置对象,request是获取用户的请求,response处理用户请求。sendRedirect()函数的作用是重定向网页,向浏览器发送一个特殊的Header,然后由浏览器来做重定向,转到指定的页面。下面我将创建四个页面,首先是sex.jsp,有一个下拉列表和提交按钮确定,选择“男”,就跳转到male.jsp,选择“女”就跳转到female.jsp,中间通过sex_action.jsp进行重定向
<!-- sex.jsp --><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="<%=basePath%>" rel="external nofollow"rel="external nofollow" ><title>Sex Select's page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"></head><body><form action="<%=basePath%>c03/sex_action.jsp" method="post"><select name="sex"><option>男</option><option>女</option></select><button type="submit">提交</button></form></body></html>
<!-- sex_action.jsp --><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="<%=basePath%>" rel="external nofollow"rel="external nofollow" ><title>My JSP 'sex_action.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"></head><body><% request.setCharacterEncoding("UTF-8");String sex = request.getParameter("sex");out.println(sex);if("男".equals(sex)) {response.sendRedirect("male.jsp");return;}else if("女".equals(sex)) {response.sendRedirect("female.jsp");return;}%></body></html>
到此这篇关于jsp response.sendRedirect()用法详解的文章就介绍到这了,更多相关jsp response.sendRedirect()内容请搜索一聚教程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持一聚教程网!
相关文章
- 龙魂旅人阵容怎么搭配-龙魂旅人阵容搭配推荐 07-06
- 绝区零自选五星选哪个-绝区零自选五星选择攻略 07-06
- 比特币和以太坊上涨至小型代币启动_近日表现优异 07-06
- 龙魂旅人角色强度排名-龙魂旅人角色推荐 07-06
- 七日世界房子设计图有哪些-七日世界房子设计图一览 07-06
- 龙魂旅人兑换码在哪输入-龙魂旅人兑换码最新版2025 07-06