• 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

How to send Parameters from JSP to a Servlet?

 
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Somnath Mallick
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Do you know what method="POST" does here ?
 
Somnath Mallick
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
POST sends the data as a part of the message body. That's what i know!
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you submit this page userName and password will be availabe in servlet as request param.
request.getParameter("userName") will return user name.
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 483
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)?
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 483
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My web.xml file is:



So action should contain /B2BLogin or http://localhost:8080/HealthCheck/B2BLogin?
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the action should be just: "B2BLogin"

 
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
Sloppy.

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

 
chandra kambham
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
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
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic