• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HTML input to JSP output

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

I am using the html form for the input and using the servlet class in the tomcat container I want to get the attribute from the html and then the result will be displayed in the jsp page , I am beginner so please guide me for the solution.

Thanks in Advance, I know time is precious.

sachin

Below are

Html file

<html>

<body>

<form name="input" action="Test.jsp" method="post">

TITLE: <input type="text" name="title" size="20">

<br>
<br>
<br>


<input type="submit" value="SUBMIT">

</form>

</body>

</html>


Servelt class


}

Result.jsp

<html>
<body>

Hello

<%= request.getAttribute("name") %>

</body>


</html>

XML

<web-app>

<servlet>

<servlet-name>Sachin</servlet-name>
<servlet-class>Test</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>Sachin</servlet-name>
<url-pattern>/xyz</url-pattern>

</servlet-mapping>
<welcome-file-list>
<welcome-file>Test.html</welcome-file>
</welcome-file-list>

</web-app>



[BSouther: Added UBB CODE tags]
[ January 10, 2008: Message edited by: Ben Souther ]
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

First, here's a useful tip for making your posts easier to read. Please be sure to use UBB code tags when posting code to the forums. Unformatted code is extermely hard to read and most people will just go elsewhere. Please read this for more information.

You can go back and change your post to add code tags by clicking the .
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With regards to your question:

You say:

I am using the html form for the input and using the servlet class


And yet, your form action is:

To invoked the servlet, you need to use the mapping that you set up in the deployment descriptor (web.xml).

In this case that's:

(I'd come up with a better name than xyz!)

So your form action should be:



This assumes that your web application is the ROOT web app. Otherwise, you need to prefix the mapping with the context root. You'll need to let us know whether your application is the ROOT or not.

Also, I notice that your servlet class is not in a package. This will cause you trouble. Be sure to place all your Java classes in a package other than the default.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
you have to redirect your request from servlet to jsp using RequestDispatcher's object include or forward method.you can set attributes in request object, and retrive them in jsp.
 
sachin mesala
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bear Bibeault:
Welcome to the Ranch.

First, here's a useful tip for making your posts easier to read. Please be sure to use UBB code tags when posting code to the forums. Unformatted code is extermely hard to read and most people will just go elsewhere. Please read this for more information.

You can go back and change your post to add code tags by clicking the .



Hi Bear Bibeault ,

Thank you very much for your reply and for the basic infomation inside javaranche.

Regarding the code i even changed i.e. i gave post="/url-pattern" inside the html file but still it is not working.

I tried all the possible but still there is some problem between the sevlet and the Jsp.

Thanks

ashwin
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is your app the root webapp inside your container?

If you're not sure what this means, post the FULL path to your HTML file, starting with the drive letter of your hard drive.
 
sachin mesala
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ben Souther:
Is your app the root webapp inside your container?

If you're not sure what this means, post the FULL path to your HTML file, starting with the drive letter of your hard drive.



Ya my application root is inside the container.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, that's not what I was asking.
Is your app THE root web application for your container?

Please post the full path to your servlet.
 
sachin mesala
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ben Souther:
No, that's not what I was asking.
Is your app THE root web application for your container?

Please post the full path to your servlet.



Really i could not get you, but if you clearly show me it will be really greatful, coz i am trying too hard but no good result.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"coz" isn't a word.


Post the full path to your servlet class file.

Example:
c:\tomcat\webapps\myapp\WEB-INF\classes\com\mypackage\MyServlet.class
 
sachin mesala
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ben Souther:
"coz" isn't a word.


Post the full path to your servlet class file.

Example:
c:\tomcat\webapps\myapp\WEB-INF\classes\com\mypackage\MyServlet.class



If you're not sure what this means, post the FULL path to your HTML file, starting with the drive letter of your hard drive.



so i gave as

 
sachin mesala
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ben Souther:
"coz" isn't a word.


Post the full path to your servlet class file.

Example:
c:\tomcat\webapps\myapp\WEB-INF\classes\com\mypackage\MyServlet.class



If you're not sure what this means, post the FULL path to your HTML file, starting with the drive letter of your hard drive.



so i gave as




but no results.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sachin mesala:

<form name="input" action="C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\Lasto\WEB-INF\classes\Test1.class" method="post">



I wasn't suggesting that you put the full path in your form.
I just wanted you to post it here so we can see what it is.
Your application is not the root application in your server.
It has a contextPath of "Lasto".
Therefore, Lasto needs to be part of your URL when calling your servlet.

 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read this to learn more about context paths:
http://faq.javaranch.com/java/ResourceUrlProblems
 
sachin mesala
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ben Souther:


I wasn't suggesting that you put the full path in your form.
I just wanted you to post it here so we can see what it is.
Your application is not the root application in your server.
It has a contextPath of "Lasto".
Therefore, Lasto needs to be part of your URL when calling your servlet.



I kept in the form action="Lasto/xyz" ....

and when i enter some text in the form , i can see that jsp excutes but i am getting only a blank page, there is no display of any string which i enter in the html nor some text inside the JSP.

 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The 3 dots (an ellipse) mean that I left the rest out for brevity.

Make sure your tag is complete.
 
sachin mesala
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ben Souther:
The 3 dots (an ellipse) mean that I left the rest out for brevity.

Make sure your tag is complete.



obviously i dint gave like that. i gave the remainning code.

still there is some thing missing.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your not writing any output or forwarding to any JSPs in your servlet.


There is no reason why it should show you anything.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sachin mesala:

I kept in the form action="Lasto/xyz" ....



Should be:
action="/Lasto/xyz"

You left out the slash from the beginning.
 
sachin mesala
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ben Souther:
Your not writing any output or forwarding to any JSPs in your servlet.


There is no reason why it should show you anything.







In the jsp file I gave the above expression code and I knew that it wont be sufficient for the link between the servlet and the jsp.

Still some thing is missing in the servlet which has to be given so that string can be passed from html form to the jsp.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's nothing missing as far as the attribute goes.
You're binding it to request scope.
Your JSP is reading it back from session scope.

What's missing is the forward from the servlet to the JSP.

Download the SimpleMVC example in our code barn.
Drop the war file in your webapps directory and Tomcat will deploy it.

You will then have a working example that does all of this.
 
sachin mesala
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ben Souther:
There's nothing missing as far as the attribute goes.
You're binding it to request scope.
Your JSP is reading it back from session scope.

What's missing is the forward from the servlet to the JSP.

Download the SimpleMVC example in our code barn.
Drop the war file in your webapps directory and Tomcat will deploy it.

You will then have a working example that does all of this.



Hi,

I used the example which you mentioned and added the requestdispatcher in the servlet and now its working.

Thanks a lot




Any good suggestion from you to me, after seeing my errors , as i want to be a good programmer.

Thanks.
ashwin
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sachin mesala:
Any good suggestion from you to me, after seeing my errors , as i want to be a good programmer.



Yes. Pick up a good book on the subject.
I used Core Servlet and Java Server Pages years ago.
I know there is a newer edition of it out there but haven't looked at it yet.
Another one that is popular is the Head First Servlets book.
 
sachin mesala
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ben Souther:


Yes. Pick up a good book on the subject.
I used Core Servlet and Java Server Pages years ago.
I know there is a newer edition of it out there but haven't looked at it yet.
Another one that is popular is the Head First Servlets book.



Hi Bean,

Thank you for the information.

sachin
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sachin mesala:


Hi Bean,



He is Ben and NOT Bean. Just you can copy, paste the names to avoid confusions.

Ben, that was so nice to see your replies
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic