• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to send Parameters from JSP to a Servlet?

 
Somnath Mallick
Ranch Hand
Posts: 482
  • 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:

 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 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: 482
  • 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!
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Do you know what method="POST" does here ?
 
Somnath Mallick
Ranch Hand
Posts: 482
  • 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!
 
shivendra tripathi
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 Java VI Editor
  • 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: 482
  • 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)?
 
Eduardo Bueno
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: 482
  • 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?
 
chandra kambham
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the action should be just: "B2BLogin"

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64613
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64613
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic