response.sendRedirect()的用法

sendRedirect()

response和request一样都是jsp内置对象,request是获取用户的请求,response处理用户请求。sendRedirect()函数的作用是重定向网页,向浏览器发送一个特殊的Header,然后由浏览器来做重定向,转到指定的页面。下面我将创建四个页面,首先是sex.jsp,有一个下拉列表和提交按钮确定,选择“男”,就跳转到male.jsp,选择“女”就跳转到female.jsp,中间通过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+"/";
%>






Sex Select's page








	

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>






My JSP 'sex_action.jsp' starting page








	<% 
    	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;
    	}
    %>


你可能感兴趣的:(JSP)