2012-11-28 38 views
2

我想设置session范围内的值,但它不设置请找问题异常来:session范围内设置一个按钮值

<%String name=request.getParameter("name");%><% if(request.getParameter("name").equals(name)){session.setAttribute("EmployeeById",name);}%>

在这里你可以找到的代码实际上我想要在会话中设置按钮值,并且我正在使用其他jsp。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
     pageEncoding="ISO-8859-1" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> 
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %> 
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %> 
<%@ taglib prefix="x" uri="http://java.sun.com/jstl/xml" %> 

<html> 
<head> 
    <script type="text/javascript"> 
     function popuponclick() { 
      var mywindow = window.open("file.jsp", "file", "status=1,width=350,height=150"); 
     } 
    </script> 
</head> 
<body> 
<form name="form1"> 
    <%String name = request.getParameter("name");%> 

    <% if (request.getParameter("name").equals(name)) { 
     session.setAttribute("EmployeeById", name); 
    } 
    %> 
    <table> 
     <tr> 
      <td><input type="submit" onclick="popuponclick()" value="GetEmployeeById" name="name"/> 
       <input type="hidden" name="GetEmp" value="1"></td> 
     </tr> 

     <tr> 
      <td><input type="submit" onclick="popuponclick()" value="GetEmployeeByname" name="name1"></td> 
     </tr> 
    </table> 
</form> 
</body> 
</html> 

我的第二个.JSP是这其中i M利用会话范围内所获得的价值

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%> 
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt"%> 
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql"%> 
<%@ taglib prefix="x" uri="http://java.sun.com/jstl/xml"%> 
<%@page 
    import="com.nousinfo.tutorial.employee.service.model.bo.EmployeeBO"%> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 

</head> 
<% 
    String attributeValue = (String)session.getAttribute("EmployeeById"); 
out.println(attributeValue); 
%> 
<body> 




<% if ("GetEmployeeById".equals(attributeValue)) { %> 
<div> 
<table> 
<tr> 
<td>GetEmployeeByName</td> 
</tr> 
<tr> 
<td><input type="text" name="GetEmployeeByName"/></td></tr> 
</table> 
</div> 
<% } else { %> 
<div> 
<table> 
<tr> 
<td>GetEmployeeById</td> 
</tr> 
<tr> 
<td><input type="text" name="GetEmployeeById"/></td></tr> 
</table> 
</div> 
<% } %> 
<table> 

      <tr> 
       <td><input type="submit" name="submit" value="find"></td> 
      </tr> 
     </table> 

</body> 
</html> 
+0

哪个例外? –

+0

@RavindraBagale空指针异常 –

+0

你究竟在哪里得到NPE? –

回答

1

第一件事:

你知道,即使你没有得到的异常,这个代码是没有意义的?您正在获取名称参数,然后再次获取并使用相同的值进行检查。这种情况将永远是true

第二件事:

的代码,如:

String name = request.getParameter("name"); 
session.setAttribute("EmployeeById", name); 

应在servlet的使用,而不是在JSP页面。在JSP页面中,可以使用EL从请求,会话或应用程序范围中获取值。

对于例如为:

${requestScope['name']} 

这是获得在JSP页面的请求属性的正确途径。

您得到的原因NPE,是因为请求在您的JSP页面中尚不可用,并且它是null。首先,您需要检查请求是否不是null,如果不是,请执行此操作。

检查SO Servlets Wiki页面也。

0

试试这个<%!字符串名称=的request.getParameter(“名称”);%>

+0

这应该如何提供帮助? –

+0

@PauliusMatulionis能告诉我如何将jsp页面上的一个java脚本函数调用到其他jsp页面的另一个java脚本函数 –