当前位置: 代码网 > it编程>编程语言>Java > jsp response.sendRedirect()用法详解

jsp response.sendRedirect()用法详解

2024年05月15日 Java 我要评论
sendredirect()response和request一样都是jsp内置对象,request是获取用户的请求,response处理用户请求。sendredirect()函数的作用是重定向网页,向

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()内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com