Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling servlet from JSP

 
prakash muthu
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I'm calling servlet from and i'm sending some data also. but i couldn't get the value from servlet. I got a value "null" only.
Code from JSP:
<jsp:forward page="/servlet/senthil">
<jsp aram name="prak" value="Prakash">
</jsp:forward>
code from Servlet :
String gh=req.getParameter("prak");
out.println("Value from JSP "+gh);
Check it out and let me know the solution.
TIA
With regards
Prakash
 
Kapil Agrawal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this in your jsp file:
<FORM METHOD="POST" ACTION="/servlet/senthil">
<INPUT TYPE="TEXT" NAME="prak" VALUE="Prakash">
<INPUT TYPE="SUBMIT" VALUE="Send">
</FORM>
 
prakash muthu
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Kapil,
Thanks for your reply. Your has to work, but what probelm with my code. Is there any error or its totally mistake. let me know the details of how to call a servlet from JSP.
with regards
Prakash
 
Kapil Agrawal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Prakash,
I should have added little explanation for my recommendation. Well, jsp:forward lets you forward the request to another page. It has a single attribute, page, which consists of a URL. It is not capable of carrying a variable value with it. So, if you need to transfer any value and the value is coming from user input, then a 'form' should serve the purpose. If the value is coming from some internal calculation of some java bean, then you can use jsp:usebean, setProperty and getProperty to modify and retrieve bean properties.
Hope it helps.
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't forget that you can also use query parameters in a URL. SO your original example might be re-coded as:
JSP:

Servlet:
 
Kapil Agrawal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, you got the right nerve.
 
prakash muthu
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi frank
How it will work. It will search like a sevlet class in the name of senthil?prak=prakash. I got a error like that only
then what is the use of jsp aram tag.
I'm using java webserver 2.0.
let me know more details.
with regards
Prakash
 
Gopal Rao
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Prakash,
I am not sure if you ever solved the problem. It is simple: you missed the terminating slash character for the "param" tag.
<jsp:forward page="/servlet/senthil">
<jsp aram name="prak" value="Prakash" />
</jsp:forward>
I put it after "Prakash" as above and tested it. It works file.
Gopal
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic