• 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 do i set a value in a jsp and get the same value in a servlet?

 
ashley Jug
Ranch Hand
Posts: 60
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

i am new to the java world and trying to learn, basically i have a form jsp (using POST method) which i want to set a value"insert"(similar to a hidden filed) and get that value "insert" in my servlet, how can i do it plss?i tried this in my jsp :
<%
request.setAttribute("insert","insert");

%>

and to get it in my servlet by:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getAttribute("insert")
}

but no value is returned.

Thnks a lot for your help.
 
Prashant Chindhade
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can put this in jsp
<form>
<input type="hidden" name="insert" value="insert">
</form>
and put this in servlet
String insert=req.getAttribute("insert");

regs
prashant
 
ashley Jug
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Thanks for your reply and i have tried it but the insert is null.Do you know why?

Please find below how i have defined it in my jsp:



and please find below as it is defined in my servlet:


Thanks again.
 
Prashant Chindhade
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
we are sending parameter not attribute it is used to set error message from servlet to jsp or set the value to the session

don't put this in jsp.
<%
request.setAttribute("insert","insert");

%>
hidden will pass the value to servlet itself

and in servlet just put toString() method like

String insert=request.getParameter("insert").toString();

 
Prashant Chindhade
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry for the first reply it will be get parameter() instead of getAttribute()
 
ashley Jug
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Prashant it did resolved my problem. Last question, when do we use request.setAttribute?

I thought we used it to set value in jsp or servlet.

I have tried on line resources but did not understand much.

Your help is highly appreciated.
 
Prashant Chindhade
Ranch Hand
Posts: 77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can use request.setAttribute like this

in servlet put this

if(condition){
. . .
code you want to execute . .
}
else{

request.setAttribute("Errormessage","User name or password is incorrect");

//redirect to page where you want to redirect after error like if login fails than //redirect to login itself
}

in jsp(login.jsp) before html tag fetch Errormessage like this

<%
String message=(String)request.getAttribute("Errormessage");
if(message==null){
message = " ";//this will not show message when page will load first time
}
//this name must be same like set in servlet
%>

and than put this veriable where you want to put like

<table>
<tr>
<td>
<%=message%>
</td>
</tr>
</table>
 
ashley Jug
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brilliant, thanks a lot ;).. makes sense now.
 
Prashant Chindhade
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you always welcome...
 
Stoian Azarov
Ranch Hand
Posts: 113
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that it will be easier for you to draw every request on paper in order to have a good overview how objects interact.
In your example you have the following scenario:

1. Client makes request to the jsp form.
2. Container creates request(1) and response(1) objects.
3. Container finds the jsp page -> creates servlet from that jsp and then the servlet receives request and response objects(yes, jsp pages are transformed to servlets under the hood).
4. So if you have this: <% request.setAttribute("insert","insert"); %> in your jsp it sets attribute insert to request(1).
5. container sends back the response(1) - including the the jsp form(in form of HTML).
6. Container destroys request(1) and response(1) and everything that was set to these objects is gone.

7. Client renders the form and clicks submit button.
8. Container creates new request(2) and response(2) objects and sends them to the servlet(the servlet you have mapped to process the form request).
9.The servlet has no clue about request(1), nor about insert request attribute.

That's why you have to include hidden form field in your form in order to receive request(2) parameter.
Request attributes are available only on the server(container) side. Between client and server you can transfer only request parameters.

Regards


 
Prashant Chindhade
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes thanks

i have also got some knowledge....

thanks once again...
 
Alexander Sales
Ranch Hand
Posts: 89
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

prashant chindhade wrote:you can put this in jsp
<form>
<input type="hidden" name="insert" value="insert">
</form>
and put this in servlet
String insert=req.getAttribute("insert");

regs
prashant



Please put your code in the code bracket.
 
Prashant Chindhade
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok...i will...Alexander Sales
 
Stoian Azarov
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alexander Sales wrote:

prashant chindhade wrote:you can put this in jsp
<form>
<input type="hidden" name="insert" value="insert">
</form>
and put this in servlet
String insert=req.getAttribute("insert");

regs
prashant



Please put your code in the code bracket.



Alexander, are you sure that you can access post request parameter "insert" using req.getAttribute() method?
I thought that only req.getParameter() will work, but maybe I am missing something.
Please confirm, so I can check in the specification.

Regards
 
Alexander Sales
Ranch Hand
Posts: 89
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ashley Jug wrote:Thanks a lot Prashant it did resolved my problem. Last question, when do we use request.setAttribute?
.



i usually use request.setAttribute in the servlet, not in JSP's. We don't want to see java codes in jsps. It is not used in coded that way in the real world so it is not a good practice. Then to retrieve the attribute which has been set, we use EL(expression language) in JSP.

Ex.

 
Alexander Sales
Ranch Hand
Posts: 89
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stoian Azarov wrote:

Alexander Sales wrote:

prashant chindhade wrote:you can put this in jsp
<form>
<input type="hidden" name="insert" value="insert">
</form>
and put this in servlet
String insert=req.getAttribute("insert");

regs
prashant



Please put your code in the code bracket.



Alexander, are you sure that you can access post request parameter "insert" using req.getAttribute() method?
I thought that only req.getParameter() will work, but maybe I am missing something.
Please confirm, so I can check in the specification.

Regards



Sorry, but it is not me. But anyway, i also haven't tried passing req.getAttribute using scriptlets.
 
Prashant Chindhade
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no guys it will not pass any value using the setAttribute method. diffrence between both is getAttribute and getParameter is that getParameter will return the value of a parameter that was submitted by an HTML form or that was included in a query string. getAttribute returns an object that you have set in the request, the only way you can use this is in conjunction with a RequestDispatcher. You use a RequestDispatcher to forward a request to another resource (JSP / Servlet). So before you forward the request you can set an attribute which will be available to the next resource.

thanks

Prasant Chindhade
 
Stoian Azarov
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Alexander and prashant
Now I am calm that I am not missing something.
 
Prashant Chindhade
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
always welcome....

regds
prashant
 
Let me tell you a story about a man named Jed. He made this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic