wood burning stoves 2.0*
The moose likes JSP and the fly likes How to pass data form jsp to servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to pass data form jsp to servlet" Watch "How to pass data form jsp to servlet" New topic
Author

How to pass data form jsp to servlet

baxhuli pema
Greenhorn

Joined: Nov 09, 2012
Posts: 11
Hello, I am passing data to my servlet through the jsp in this way:

So I am passing the firstName parameter from my bean class (if I am correct). Now I want to pass the firstName through the session.getAttribute("username");
So I want to do something like this:

and then

But I am getting errors when I try this. Can anyone please tell me what is my bad?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60759
    
  65

Perhaps it would help if you let us know what the errors were?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Karn Kumar
Ranch Hand

Joined: Aug 06, 2009
Posts: 145

Hi baxhuli pema,


Can you describe error you are getting , so that it will be easy to find out exact mis ?

If you are getting error on the JSP then put try-catch block and all your code inside it and print Stack Trace.

Also same can be done in the servlet .


- Chetan
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60759
    
  65

Chetan Dorle wrote:then put try-catch block and all your code inside it and print Stack Trace.

Not good advice -- modern JSPs should have no Java code inside them. (Even though it is clear that the OP is violating this precept.)

The best advice is for the errors to be posted so we can evaluate what is going on.

To the OP: Java code in a JSP is an obsolete practice. Please see this JspFaq entry
baxhuli pema
Greenhorn

Joined: Nov 09, 2012
Posts: 11
This is the error:

Thank you!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60759
    
  65

It's telling you right where the error is: /detyra.jsp (line: 24, column: 24)

Look at what's there.
baxhuli pema
Greenhorn

Joined: Nov 09, 2012
Posts: 11
yes it is saying "quote symbol expected" so I understand that I can not enter in that way <%=user%>. That's why I am asking. If you know how can I do that. Thank you.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60759
    
  65

Is that what's at line 24?

Also remember that anything that's not a JSP mechanism is merely template text and has no meaning.

And, again, you should be using JSTL and EL rather than obsolete Java scriptlets.
baxhuli pema
Greenhorn

Joined: Nov 09, 2012
Posts: 11
Ok I will try to figure out and I will let you know! Thank you.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
Presuming that this is line 24:


There is an extra quote in there.
If the user was 'Bob' then what this would produce is:



There are three quotes in that line of html.
I think you should be removing the second one.

Also I would suggest you do some sort of Url Encoding if you are going to build up a URL string in this fashion.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60759
    
  65

The best way to format a URL in a JSP is with the JSTL <c:url> tag.
Karn Kumar
Ranch Hand

Joined: Aug 06, 2009
Posts: 145

Ya try what Stefan has suggested . Like below


<a href="DetyraServlet?action=listDetyra&firstName=<%=user%>">Something</a>



- Chetan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to pass data form jsp to servlet
 
Similar Threads
Extracting data from a bean
How to pass parameter to java program??
Doubt on calling java class in Servlet
Issues with using <jsp:setProperty>
Generating dynamic includes with servlet