Win a copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java this week in the Agile and Other Processes forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
master stewards:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
garden masters:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
gardeners:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

How to send Parameters from JSP to a Servlet?

 
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
  • 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: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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: 155
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
the action should be just: "B2BLogin"

 
Marshal
Posts: 67357
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
Marshal
Posts: 67357
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • 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.
 
We find this kind of rampant individuality very disturbing. But not this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!