当前位置: 代码迷 >> Java Web开发 >> {新手求解}不知道错哪了,很简单的程序没有达到想要的效果
  详细解决方案

{新手求解}不知道错哪了,很简单的程序没有达到想要的效果

热度:48   发布时间:2016-04-16 22:17:49.0
{新手求解}不知道哪里错了,很简单的程序没有达到想要的效果
做了一个登陆页面login.jsp,输入用户名和设定的密码,经过check.jsp,跳转进入success.jsp
success点jsp实现的功能是显示“Hello! +之前输入的用户名”可是我运行出来的结果是
“hello !null”,没有达到预期的效果

以下是各个页面的代码
login.jsp的代码

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>


  </head>
  
  <body>
     <form name="f1" id="f1" action="check.jsp" method="post" >
     <table>
      <tr>
      <td>用户名:</td>
      <td><input type="text" name="username"></td>
      </tr>
      <tr>
      <td>密码:</td>
      <td><input type="password" name="password"></td>
      </tr>
      <tr>
      <td><input type="submit" name="submit" value="提交"></td>
     
     </table>
     </form>
  </body>
</html>


check.jsp的代码
<%
String s_name="";
String s_pass="";
s_name=request.getParameter("username");
s_pass=request.getParameter("password");
if("admin".equals(s_name)&&"pass".equals(s_pass))
{
response.sendRedirect("success.jsp");
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location","success.jsp");
}
else
{
response.sendRedirect("login.jsp");
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location","login.jsp");
}




%>




success.jsp页面的代码


<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  
  <body>
   <center><%String a1=request.getParameter("username"); %>
    <h1>Hello! &nbsp;<%=a1 %></h1>
   </center>
  </body>
</html>

------解决方案--------------------
你在check.jsp那里把response.sendRedirect("success.jsp");
改成response.sendRedirect("succeed.jsp?s_name="+username);
因为session他只保存一次页面跳转后的值,如果要继续为下一页服务,就要传参
------解决方案--------------------
check.jsp的代码
 <%
 String s_name="";
 String s_pass="";
 s_name=request.getParameter("username");
 s_pass=request.getParameter("password");
 if("admin".equals(s_name)&&"pass".equals(s_pass))
 {
request.getSession(false).setAttribute("userName", s_name);
 response.sendRedirect("success.jsp");
 response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
 response.setHeader("Location","success.jsp");
 }
 else
 {
 response.sendRedirect("login.jsp");
 response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
 response.setHeader("Location","login.jsp");
 }

 


%>


<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 <html>
   <head>
   </head>
   
   <body>
    <center><%Object userNameObj = request.getSession(false).getAttribute("userName");
                       String userName = new String();
                       userName = userNameObj != null ? userName.toString() : "Hey, guy. You haven't signed on yet.Get to sign on first then you are alowed to use this page.";%>
     <h1>Hello! &nbsp;<%=userName %></h1>
    </center>
   </body>
 </html>
------解决方案--------------------
你用的是重定向 所以不是同一个request
这里应该用转发
------解决方案--------------------
check.jsp里,sendRedirect和下面setStatus+setHeader有一个是多余的

浏览器会重定向到success.jsp,要获取到前面的用户信息,可以像一楼等传递请求参数,或者服务器把数据保存起来,像2楼一样的用session
  相关解决方案