当前位置: 代码迷 >> Java Web开发 >> JSP没法跳转
  详细解决方案

JSP没法跳转

热度:855   发布时间:2013-02-25 21:09:51.0
JSP无法跳转
代码如下,点提交后无法页面跳转,要求根据选择的身份重定向到不同页面。
3-2.jsp 
<form action="3-2.jsp" method="post">
请输入你的姓名:<input type="text" name="name">
<br>
请选择您的身份:
  <input type="radio" name="job" value=“教师”>教师
  <input type="radio" name="job" value=“学生”>学生
  <input type="radio" name="job" value=“管理员”>管理员
  <input type="submit" value="提交" name="submit">
  </form>
  <%
  request.setCharacterEncoding("utf-8");
  String job=request.getParameter("job");
  String str=request.getParameter("name");
  if(str==null)
  { str="";
  byte b[]=str.getBytes("ISO-8859-1");
  str=new String(b);
  }
  if(job.equals(""))
  {response.sendRedirect("3-2.jsp");}
  else if(job.equals("教师")){
  response.sendRedirect("3-21.jsp");
  }
  else if(job.equals("学生")){
  response.sendRedirect("3-22.jsp");
  }
  else if(job.equals("管理员")){
  response.sendRedirect("3-23.jsp");
  }
  %>
3-21.jsp(与3-22,3-23基本一样)
  <body>
  <%out.print("您好+str+老师!"); %>
  </body>

------解决方案--------------------------------------------------------
首先判断不要用用中文, job.equals("教师") 如果job值是"教师"这个equls也返回false..
对于你的这行代码: <input type="radio" name="job" value=“教师”>教师
1:input应该这样 <input /> 你少了一个/
2: value的值 应该用英语标点"" 不是中文“” 这个是有区别的
3: 最后一句:<%out.print("您好+str+老师!"); %>
应该是:<%out.print("您好"+str+"老师!"); %>

建议你把中文判断改为英文..

ps:哥们先把昨天的帖子结了吧,嘿嘿~

------解决方案--------------------------------------------------------
探讨

首先判断不要用用中文, job.equals("教师") 如果job值是"教师"这个equls也返回false..
对于你的这行代码: <input type="radio" name="job" value=“教师”>教师
1:input应该这样 <input /> 你少了一个/
2: value的值 应该用英语标点"" 不是中文“” 这个是有区别的
3: 最后一句:<%o……

------解决方案--------------------------------------------------------
<%@ 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%>">

<title>3-2.jsp</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>
  
<body>
 <form action="3-2.jsp" method="post">
请输入你的姓名:<input type="text" name="name">
<br>
请选择您的身份:
  相关解决方案