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

Making a Servlet display a JSP Page on first request  RSS feed

 
Ray mann
Ranch Hand
Posts: 48
Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,
I would like some help with my Servlet as I would like my Servlet to run first and to create a session var then forward the value to the JSP (the GUI ) and have the JSP able to send a var back to the Servlet for reprocessing
and update the session var to again be sent to the JSP and so on . - The Servlet contains logic to handle incorrect input types and directly out puts a HTML error page...hope this makes sense!

Below I will add the code I have so far and a link to the question in my Text Book.

My code:

JSP Page:

bank.JSP :



Servlet :

JSPBank.java :



The Question is no 7 on this page : Text Book
Hope this helps you to understand what i am trying to achieve .

Thanks in advanced for any and all help!!
 
Prab Singh
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems like you need RequestDispatcher

You use this to call one web app component from another

Example - Forwarding requesting from Servlet to JSP

RequestDispatcher requestDispatcher = request.getRequestDispatcher("\login.jsp");
requestDispatcher.forward(request, response);

Inside the jsp you can use session.getAttribute("attributeName") or even better if you use <jsp:useBean> to access that as it is better to avoid scriptlets in jsp.

Hope it helps.
 
Ray mann
Ranch Hand
Posts: 48
Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome Thanks it did the trick!!

Now its almost right ...for some reason the transactions always seem to be one transaction behind eg. if I start on a 0.00 balance then deposit 100 then it displays 0.00 balance then I deposit 50 it displays a balance of 100.00 then I deposit 10 it shows a balance of 150.00 and so on ....

And the error handling (incorrect input type detection) is not working no matter what.

bank.jsp




JSPBank.java


 
K. Tsang
Bartender
Posts: 3620
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How come you are forwarding to the JSP (lines 36-7) then do the processing logic (line 40 onward)?

You should forward to the jsp at the very end after all the processing is done.

Also since you are using sessions, did the jsp page have session set to true?


 
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
Your code structure is very haphazard and poorly constructed. Please read this article for information on how to properly structure modern Java web applications.
 
Stefan Evans
Bartender
Posts: 1834
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I think it is meant to be this way. The textbook examples being followed start with a "bad" way of doing things, and then progressively shows you how to make things better, explaining why along the way.
So the first example (posted in another thread) had everything in the servlet. This step is starting to pull the front end code out into a JSP.
Baby steps.

There is still definitely room for improvement in this class though.
A couple of points I think are relevant:

A servlet should either handle the output itself (writing to the response) OR forward via the request dispatcher. It should NEVER do both - suddenly you have two bits of code writing to the response, which probably leads to an Exception.
In general the structure should be something like:


Right now you are displaying your page, and THEN going and running actions (which might result in an error message)

As a generic coding suggestion, there looks to be a lot of repeated/redundant code. I would recommend refactoring it into a displayErrorMessage() method which then makes your code easier to read as it condenses the rest of your logic.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!