File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Getting wrong result ,but why? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Getting wrong result ,but why?" Watch "Getting wrong result ,but why?" New topic
Author

Getting wrong result ,but why?

Abhra Kar
Ranch Hand

Joined: May 22, 2008
Posts: 124
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 .
Musab Al-Rawi
Ranch Hand

Joined: Aug 06, 2007
Posts: 231
hi,

are you running the invoking the servlet using POST method?
may be you are invoking the JSP directly (not through the servlet)


SCBCD - SCWCD - SCJD - SCJP - OCA
Abhra Kar
Ranch Hand

Joined: May 22, 2008
Posts: 124
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
Joe Matthew
Ranch Hand

Joined: Jun 10, 2008
Posts: 66
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!


Regards,<br />Joe<br /> <br />"Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live."<br />--Martin Golding
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

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.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Abhra Kar
Ranch Hand

Joined: May 22, 2008
Posts: 124
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
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42946
    
  70
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.
Abhra Kar
Ranch Hand

Joined: May 22, 2008
Posts: 124
Yes sir

That is the answer I want to know .
Now my doubt gets clear .
Thanks sir to all of you for helping me
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

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>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting wrong result ,but why?