Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting wrong result ,but why?

 
Abhra Kar
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

are you running the invoking the servlet using POST method?
may be you are invoking the JSP directly (not through the servlet)
 
Abhra Kar
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Abhra Kar
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic