Forums Register Login

Getting wrong result ,but why?

+Pie Number of slices to send: Send
I make a Bean class -->

package foo;
public class Person {

public String name;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

A servlet class -->

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class s1 extends HttpServlet {

public void doPost(HttpServletRequest req,HttpServletResponse res)
throws IOException,ServletException{

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

RequestDispatcher view =req.getRequestDispatcher("result.jsp");
view.forward(req,res);
}
}

And a JSP file -->

<html><body bgcolor="#11dd77">
Person is:
<%= request.getAttribute("person") %>
</body></html>

The classes get compiled and according to HFSJ page-344 I should get output
Person is: foo.Person@512d66[may be the number changed]
But I get output Person is:null
i can't understand why .
+Pie Number of slices to send: Send
hi,

are you running the invoking the servlet using POST method?
may be you are invoking the JSP directly (not through the servlet)
+Pie Number of slices to send: Send
Yes i was wrong i was directly invoking the jsp
it will running through the servlet but in get method not in post method
why post can't be used
+Pie Number of slices to send: Send
I dont see a function doGet() in the servlet code you gave.

What is the method that gets called by default in servlet? And what is the METHOD you mentioned in the submit JSP? Check the book.

Happy Studying!
+Pie Number of slices to send: Send
how are you actually *executing* the application? are you invoking the servlet which then redirects the request to the result.jsp or you directly invoke the jsp?

If you don't mention the method explicitly, the Get method is what gets invoked by default. There are no hard and fast rules that you should not use Post method. It is your choice but they do have their own +ves and -ves. You may have a look at this thread.
+Pie Number of slices to send: Send
Ok now i am using doPost() and invoking the servlet first ,saying get is not supporting this url then where to mention post method explicitly
because I am not using html so how will I mention post method
+Pie Number of slices to send: Send
How are you invoking the servlet if not from an HTML form?

If you enter the URL into the browser location bar then it will be a GET request; no way to do that via POST.
+Pie Number of slices to send: Send
Yes sir

That is the answer I want to know .
Now my doubt gets clear .
Thanks sir to all of you for helping me
+Pie Number of slices to send: Send
That's nice to hear. But you have to be clear on how to invoke the Servlet via a HTML form which supports two values (get, post) for the "method" attribute of it.

It is like

<blockquote>code:
<pre name="code" class="core">
<form name="MyForm1" action="..url_to_your_servlet" method="get">
all your form elements and controls go here!
</form>
</pre>
</blockquote>
And when my army is complete, I will rule the world! But, for now, I'm going to be happy with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1093 times.
Similar Threads
EL s not running
jsp:useBean
Why tomcat act like it error?
problem when trying code from HFSJ
get a servlet name
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 06:26:18.