aspose file tools*
The moose likes Servlets and the fly likes How do i set a value in a jsp and get the same value in a servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How do i set a value in a jsp and get the same value in a servlet?" Watch "How do i set a value in a jsp and get the same value in a servlet?" New topic
Author

How do i set a value in a jsp and get the same value in a servlet?

ashley Jug
Ranch Hand

Joined: Sep 02, 2011
Posts: 60
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

Joined: Jan 19, 2011
Posts: 77
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

Joined: Sep 02, 2011
Posts: 60
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

Joined: Jan 19, 2011
Posts: 77
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

Joined: Jan 19, 2011
Posts: 77
sorry for the first reply it will be get parameter() instead of getAttribute()
ashley Jug
Ranch Hand

Joined: Sep 02, 2011
Posts: 60
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

Joined: Jan 19, 2011
Posts: 77
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

Joined: Sep 02, 2011
Posts: 60
Brilliant, thanks a lot ;).. makes sense now.
Prashant Chindhade
Ranch Hand

Joined: Jan 19, 2011
Posts: 77
you always welcome...
Stoian Azarov
Ranch Hand

Joined: Jun 01, 2011
Posts: 113
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

Joined: Jan 19, 2011
Posts: 77
yes thanks

i have also got some knowledge....

thanks once again...
Alexander Sales
Ranch Hand

Joined: Feb 21, 2011
Posts: 89

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.


OCPJP 6, OCEWCD Java EE 6
Prashant Chindhade
Ranch Hand

Joined: Jan 19, 2011
Posts: 77
ok...i will...Alexander Sales
Stoian Azarov
Ranch Hand

Joined: Jun 01, 2011
Posts: 113
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

Joined: Feb 21, 2011
Posts: 89

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

Joined: Feb 21, 2011
Posts: 89

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

Joined: Jan 19, 2011
Posts: 77
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

Joined: Jun 01, 2011
Posts: 113
Thanks Alexander and prashant
Now I am calm that I am not missing something.
Prashant Chindhade
Ranch Hand

Joined: Jan 19, 2011
Posts: 77
always welcome....

regds
prashant
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do i set a value in a jsp and get the same value in a servlet?