File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes HTML input to JSP output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "HTML input to JSP output" Watch "HTML input to JSP output" New topic
Author

HTML input to JSP output

sachin mesala
Greenhorn

Joined: Jan 09, 2008
Posts: 17
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 ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

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 .


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

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.
Venkat Kanneganti
Ranch Hand

Joined: Nov 30, 2007
Posts: 43
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

Joined: Jan 09, 2008
Posts: 17
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
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
sachin mesala
Greenhorn

Joined: Jan 09, 2008
Posts: 17
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Jan 09, 2008
Posts: 17
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

Joined: Dec 11, 2004
Posts: 13410

"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

Joined: Jan 09, 2008
Posts: 17
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

Joined: Jan 09, 2008
Posts: 17
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Dec 11, 2004
Posts: 13410

Read this to learn more about context paths:
http://faq.javaranch.com/java/ResourceUrlProblems
sachin mesala
Greenhorn

Joined: Jan 09, 2008
Posts: 17
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

Joined: Dec 11, 2004
Posts: 13410

The 3 dots (an ellipse) mean that I left the rest out for brevity.

Make sure your tag is complete.
sachin mesala
Greenhorn

Joined: Jan 09, 2008
Posts: 17
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Jan 09, 2008
Posts: 17
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Jan 09, 2008
Posts: 17
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Jan 09, 2008
Posts: 17
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
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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


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]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HTML input to JSP output
 
Similar Threads
security warning while running servlet
Problem with retreving the Attribute in jsp
/test/GreetingServlet doesn't work
NoClassDefFoundError
JSP not able to find the servlet