2012-11-28 58 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腳本函數 –