Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

servlet error message to jsp  RSS feed

 
Nassia Papasaras
Greenhorn
Posts: 7
MySQL Database Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,
I have a servlet that adds entries to a table and when there is a duplicate it redirects to an error page and I'd like to make it display the duplicate entry id.
The problem is when I try to get the parameter and show the error (the duplicate entry id) on my jsp error page it shows null instead of the appropriate number.

this is my servlet:



and the jsp:



What should I do?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, stop using Java code n your JSPs. That's a bad practice from over 13 years ago. 13 years.

Second, you are doing a redirect, so all request info is lost. You'll either need to pass any info as parameters, or store in session.
 
Nassia Papasaras
Greenhorn
Posts: 7
MySQL Database Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I stored it in session and now it works, so thanks for that.
I agree on not using java code in JSPs, however only when there's a lot of code that can be written let's say into a Servlet, small bits IMHO are acceptable.
 
Dave Tolls
Ranch Hand
Posts: 2504
27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nassia Papasaras wrote:I stored it in session and now it works, so thanks for that.
I agree on not using java code in JSPs, however only when there's a lot of code that can be written let's say into a Servlet, small bits IMHO are acceptable.


Both bits of Java code in your JSP can be done using tags and el, which is what they are for. So no, you should have no Java code in your JSP.

Also, you should not be storing request stuff in your session if it's not needed between requests (which this is not likely to be).
Why are you not simply forwarding to the JSP pages?
Why do a redirect?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nassia Papasaras wrote:small bits IMHO are acceptable.

Sorry, but they are not. The small bits that you have are easily replaceable with JSTL and EL expressions. Using scriptlets in 2015 is not only a bad practice, it is irresponsible and amateurish.
 
Marshall Blythe
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:
Why are you not simply forwarding to the JSP pages?
Why do a redirect?


The OP may be implementing the Post-Redirect-Get pattern. However, you could make an argument for forwarding in cases like this where an error prevents the state from actually changing.
 
Nassia Papasaras
Greenhorn
Posts: 7
MySQL Database Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:
Both bits of Java code in your JSP can be done using tags and el, which is what they are for. So no, you should have no Java code in your JSP.

Also, you should not be storing request stuff in your session if it's not needed between requests (which this is not likely to be).
Why are you not simply forwarding to the JSP pages?
Why do a redirect?


Tbh I didn't know about tags, my java instructor taught my class to do it this way and that's what I've been doing.
I'll look into tags, thank you.
I know about forwarding, I use it in other pages I just couldn't get it to work for this one.

Bear Bibeault wrote:
Using scriptlets in 2015 is not only a bad practice, it is irresponsible and amateurish.

lol
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!