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 How to send Parameters from JSP to a Servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "How to send Parameters from JSP to a Servlet?" Watch "How to send Parameters from JSP to a Servlet?" New topic
Author

How to send Parameters from JSP to a Servlet?

Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
Hi Everyone,

I am trying to make a login servlet code with JSP. The first page is a login page where there is a username and password field. Now on clicking on submit i want the JSP to send those values to the servlet where it can be verified and then redirected to the corresponding page. Could you please tell me what should i do to do this? Righ now my Login.jsp looks like this:

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

put an action attribute in your form which will point to the url to which the servlet is mapped. Also please put the attribute values in double quotes. Your input tag which looks like this



should look like this



SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
Ok. Thanks!



I hope this is ok! But now how do i send the parameters to a servlet. This HealthCheck is name of my servlet!
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874



Do you know what method="POST" does here ?
Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
POST sends the data as a part of the message body. That's what i know!
shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263
When you submit this page userName and password will be availabe in servlet as request param.
request.getParameter("userName") will return user name.


SCJP 1.5(97%) My Blog
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874

Somnath Mallick wrote:POST sends the data as a part of the message body. That's what i know!


what data ? its includes form data , all fields in the form are submitted to the action called. Its done automatically during form submission.
As said in above post , use request.getParameter() to get values for the submitted data.
Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
Well it seems that i am running into some problems of a different kind! I have exported the war file deployed it on a JBoss server from the admin console, then when I open the the URL http://localhost:8080/HealthCheck/Login.jsp the fields come in filled with the value "admin" in username field and some value in the password field!

Also would like to ask.. That i want to send the data of the JSP to the servlet so the action parameter would contain the Servlet name right (in my case it is B2BLogin)?
Eduardo Bueno
Ranch Hand

Joined: Jun 04, 2009
Posts: 155
Somnath Mallick wrote:
Also would like to ask.. That i want to send the data of the JSP to the servlet so the action parameter would contain the Servlet name right (in my case it is B2BLogin)?

It should contain whatever url pattern you mapped in your web.xml for that servlet.
Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
My web.xml file is:



So action should contain /B2BLogin or http://localhost:8080/HealthCheck/B2BLogin?
chandra kambham
Ranch Hand

Joined: Jun 09, 2008
Posts: 74
the action should be just: "B2BLogin"

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61772
    
  67

Sloppy.

The action should be server-relative, prefixed with the context path:


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
chandra kambham
Ranch Hand

Joined: Jun 09, 2008
Posts: 74
If the servlet is in the same context as the JSP then we can just use the "B2BLogin" as the action ,
but if the servlet is in different context then we should give the complete path with the context.
How ever it will be a best practise to use complete path always.

please correct me if i am wrong.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61772
    
  67

That's sloppy, imprecise and fragile. It's a much better practice to be crisp and precise. Using a server-relative path including the context path can never break.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to send Parameters from JSP to a Servlet?