2011-01-05 3 views
0

JSP로 약간의 응용 프로그램을 만들려고합니다. 숙제 야. 그래서, 나는 클라이언트가 그의 이름을 입력하는 index.jsp 페이지를 가지고 있고 클라이언트가 데이터베이스에 존재한다면 세션에 저장된 속성 클라이언트와 함께 clientexists.jsp로 전달됩니다. 그런 다음 클라이언트는 2 개의 라디오 버튼 중 하나를 선택해야하며 선택에 따라 지불 할 세금을 표시해야합니다. 이 예외의 출처를 파악할 수 없습니까?

18: <INPUT TYPE="SUBMIT" NAME="buton2" VALUE="Continua"></form> 
19: 
20: <% 
21:  client = request.getAttribute("nume").toString(); 
22:  
23:  if (request.getParameter("buton2") != null) 
24:  { 


Stacktrace:] with root cause 
java.lang.NullPointerException 
    at org.apache.jsp.clientexists_jsp._jspService(clientexists_jsp.java:82) 
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:68) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:387) 
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:363) 
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:306) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:203) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:379) 
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:242) 
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:259) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:281) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 

그리고 웹 페이지에, 내가 같은 줄을 가리키는 또 다른 하나가

: 여기

org.apache.jasper.JasperException: An exception occurred processing JSP page /clientexists.jsp at line 21 

내의 index.jsp 페이지입니다 그러나 그의 선택 후, 나는 콘솔에서이 예외가 :

<%@page import="dao.*"%> 
<%@page import="model.*"%> 
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1" import="java.util.*,java.text.*,java.io.*"%> 
<jsp:useBean id="rep" class="dao.RepositoryDb"></jsp:useBean> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Tax machine</title> 
</head> 
<body> 

<div align="center"> 

<h1>Your name:</h1> 
<form method="POST" action="index.jsp"><INPUT TYPE="TEXT" 
    NAME="name"><BR> 
<BR> 
<INPUT TYPE="SUBMIT" NAME="buton" VALUE="Continua"></form> 

</div> 
<% 
    if (request.getParameter("buton") != null) 
    { 
     String client = request.getParameter("name"); 

     if (rep.clientExists(client)) 
     { 
      session.setAttribute("nume", client); 
%> 
      <jsp:forward page="clientexists.jsp"></jsp:forward> 
<% 
     } 
     else 
     { 
%> 
      <jsp:forward page="clientnotexists.jsp"></jsp:forward> 
<% 
     } 
    } 
%> 

</body> 
</html> 

그리고 clientexists.jsp 페이지 :

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
    <%! String client = null; %> 
<jsp:useBean id="rep" class="dao.RepositoryDb"></jsp:useBean> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Choose your options</title> 
</head> 
<body> 
<div align="center"> 

<form method="POST" action="clientexists.jsp"> 
<INPUT TYPE="RADIO" NAME="group" VALUE=paid" CHECKED>Paid taxes<BR> 
<INPUT TYPE="RADIO" NAME="group" VALUE="unpaid">Unpaid taxes<BR> 
<BR> 
<INPUT TYPE="SUBMIT" NAME="buton2" VALUE="Continue"></form> 

<% 
    client = session.getAttribute("nume").toString(); 

    if (request.getParameter("buton2") != null) 
    { 
     String option = request.getParameter("group"); 

     if (option.equalsIgnoreCase("paid")) 
     { 
%> 
      <%= rep.getImpozitePlatite(client).toString()%> 
<% 
     } 
     else 
     { 
%> 
      <%= rep.getImpoziteNeplatite(client).toString()%> 
<% 
     } 
    } 
%> 
</div> 
</body> 
</html> 

제발 도와 주실 수 있나요? 덕분에

+1

Java 코드를 Java 클래스로 이동하고 JSP를 사용하여 값을 표시해야합니다. 한 가지 : Java 코드는 디버그하기가 훨씬 쉽습니다. –

+0

귀중한 의견을 보내 주셔서 감사합니다. 몇 가지 예를 들어 링크를 알고 있다면 더 좋을 수도 있습니다. 감사합니다. – DaJackal

답변

3

request.getAttribute("nume").toString(); 

request.getAttribute("nume")null을 보인다 오류가 나는 때문에 &입니다 Client.jsp를 들어

client = "SOME_DEFAULT_VALUE"; 
if(request.getAttribute("nume") != null){ 
client = request.getAttribute("nume").toString(); 
} 

처럼 만들 null

이상 toString()를 호출 여기에있다 컴파일러에서의 선언을 볼 수 없습니다. (10) 변수

제안

+0

올바르지 않습니다.이 오류는 jsp에서는 null 포인터가 아니지만 컴파일러에서 null 포인터를 발생시키는 구문 예외입니다. – LINEMAN78

+0

대단히 감사합니다. 많은 도움이되었습니다. – DaJackal

+0

@ LINEMAN78 그건 client.jsp입니다 –

0

오타 :

client = request.getAttribute("nume").toString(); 

가되어야한다

client = request.getAttribute("name").toString(); 

getAttribute()는 찾고자하는 것을 찾을 수없고 null에서 toString()을 실행할 수 없기 때문에 null 객체를 반환합니다.

다른 하나는 HTML이 모두 대문자로 표시 될 필요는 없습니다. 여전히 유효하지만 추악하고 코드가 오래된 것으로 보입니다.

편집 : 단지 자신 앞에에서받은 포스터가 언급 한 바와 같이, 당신은 당신의 마크 업 출력에서 ​​애플리케이션 로직을 분리하는 것이 현명 할 것입니다, 그것을 유지하는 일이 훨씬 쉬워집니다. 현재 디자인은 숙제에 대해서는 문제가 없지만 실제 프로젝트에서는 이상적이지 않습니다.

+0

고맙습니다. 더 읽을 거리가 있습니다. – DaJackal

+0

그리고 'Continua'는 오타이기도합니다. –

+0

'index.jsp'에서 session : session.setAttribute ("nume", client);'에 정확히이 속성을 설정했기 때문에 NPE를 유발하는 이상한 * nume *가 아닌지 의심 스럽습니다. –

관련 문제