关于request.getParameter和session.getAttribute

来源:百度知道 编辑:UC知道 时间:2024/06/14 18:38:30
我想从一个页面:ex24.jsp中输入用户名,在另外个页面ex24_2.jsp中显示出来。主要代码: ex24.jsp
====================================================================
<form id="form1" name="form1" method="post" action="ex24.jsp" onsubmit="return on_submit()">
.
.
.
script language="javascript" type="text/javascript">
function on_submit(){
if(form1.name.value=="")
{
alert("name不能为空");
form1.name.focus();
return false;
}
if(form1.password.value=="")
{
alert("password不能为空");
form1.passsword.focus();
return false;
}
}
</script>
<%!
public String codetoString(String str){
String s=str;
try{
byte temp[]=s.getBytes("iso-8859-1");
s=new String(temp);
return s;
} catch(Exception

request.getParameter("name") 获得的name 为null; 因为ex24.jsp有response.sendRedirect("ex24_2.jsp"); 所ex24.jsp和ex24_2.jsp是两个不同的请求,你在ex24_2.jsp里看到的request.getParameter("name")是null
ex24.jsp里面有session.setAttribute("name",codetoString(name)); 而
你的表单直接提交到ex24_2.jsp,session.getAttribute("name")当然是空的,

request.getParameter("name") 获得的name 为null是因为你使用了response.sendRedirect("ex24_2.jsp")重定向,使用重定向时放在request中的值将丢失。
既然你要把结果显示在ex24_2.jsp中,那么form的action应该设成它。
session中有没有值和设form的action没有关系。
有一个可能性就是,你的往session里设值的if没执行,
你写的是if(name!=null&password!=null)
你应该想表达if(name!=null&&password!=null)吧,
这两个可是不同的啊。。

你使用了response.sendRedirect("ex24_2.jsp"); 下一个页面不是同一个request对象,所以你的值是NULL
name!=null&password!=null 没有错?是不是少了一个&