• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HFSJ book:Bean Example Page 345

 
Jamed
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hallo everyone,

I am trying the example on page 345
it worked for me using the servlet only
i had to change the method to doGet instead of doPost in the servlet code

but when i tried the jsp, it didn't work
is there anything missing on the jsp?

thanks in advance
 
nils appeldoorn
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you be a little more specific? what is not working? how do you see it's not working?

please post your form, the code and the error you're seeing (if any)
 
Jamed
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is error that i get
********************************************************
HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:370)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause

java.lang.NullPointerException
org.apache.jsp.result_jsp._jspService(org.apache.jsp.result_jsp:47)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


note The full stack trace of the root cause is available in the Apache Tomcat/5.5.9 logs.


--------------------------------------------------------------------------------

Apache Tomcat/5.5.9
***********************************************************************
this is my Test.java
package com.example.web;


import com.example.web.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;


public class Test extends HttpServlet{

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{


Person p = new Person();
p.setName("Evan");
request.setAttribute("person",p);

RequestDispatcher view= request.getRequestDispatcher("result.jsp");
view.forward(request,response);

}

********************************************************************
this is the bean class Person.java
package com.example.web;


public class Person {

private String name;



public void setName(String n)
{
name=n;
}

public String getName()
{
return name;
}

}//Person
****************************************************
this is my jsp result.jsp
<html><body>

<% com.example.web.Person p= (com.example.web.Person) request.getAttribute("person");%>
Person is :<%=p.getName()%>

</body></html>
*********************************************************
this is my DD web.xml

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.4">

<servlet>
<servlet-name>BeansTest</servlet-name>
<servlet-class>com.example.web.Test</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>BeansTest</servlet-name>
<url-pattern>/Tester.do</url-pattern>
</servlet-mapping>

</web-app>
 
nils appeldoorn
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmmmmm, I can't see why this shouldn't work ... I'll try to build it myself
 
nils appeldoorn
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay ... got the exact same problem, same output ... anyone else got a solution?

ps. that's why I never really try these examples, nobody can guarantee me that they really work
[ June 13, 2005: Message edited by: nils appeldoorn ]
 
nils appeldoorn
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a co-worker solved it!

the code is indeed correct, I just made the same mistake you probably did, calling directly the result.jsp! do not do that!
the problem is, the Person object is not known/filled by that time, because the servlet fills it and you don't pass the servlet that way

instead, call the /Tester.do directly, this will call the doGet which will fill the object. alas, it works
 
Jamed
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nils,

thank you very much for your help. It did work with me by calling Tester.do directly but I wanted it to work with the jsp otherwise why do they have it there?

thanks a lot again
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic