日期:2014-05-18  浏览次数:20657 次

sendRedirect()问题


myexampleResponse1.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head></head>
<body>
<p>输入名字:</p>
<form action="myexampleResponse1R.jsp" method="post" name="Response1R_FORM"> 
<input type="text" name="username" />
<input type="submit" name="boy" value="登陆"/>
</form>
</body>
</html>
myexampleResponse1R.jsp:
<html>
<head></head>
<body>
<% 
request.setCharacterEncoding("GBK");
String str = request.getParameter("username");
//out.print(str);
if(str.isEmpty()){  //如果输入为空则跳转
// str ="";
out.print("<p>ddddd</p>");
response.sendRedirect("myexampleResponse1.jsp");
}
else{
//str = new String(str.getBytes("ISO8859-1"),"GBK");
}
 %>
 <p>欢迎  <%=str %> 进入本网页</p>
</body>
</html>

有个问题就是下面的JSP页面中,,if判断中我写成if(str==null)的话,下面的sendRedirect不生效(不会重定向) 写成现在这样if(str.isEmpty()) 就会执行重定向
java jsp javaee

------解决方案--------------------
空字符串和null是不相等的,应该是if(!str.isEmpty()&&str!=null)再判断