Forums Register Login

HTML input to JSP output

+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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 .
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
"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
+Pie Number of slices to send: Send
 

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

+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.

+Pie Number of slices to send: Send
Read this to learn more about context paths:
http://faq.javaranch.com/java/ResourceUrlProblems
+Pie Number of slices to send: Send
 

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.

+Pie Number of slices to send: Send
The 3 dots (an ellipse) mean that I left the rest out for brevity.

Make sure your tag is complete.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
Your not writing any output or forwarding to any JSPs in your servlet.


There is no reason why it should show you anything.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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
I can't beleive you just said that. Now I need to calm down with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 4970 times.
Similar Threads
JSP not able to find the servlet
/test/GreetingServlet doesn't work
NoClassDefFoundError
Problem with retreving the Attribute in jsp
security warning while running servlet
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 01:35:28.