Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Page history

 
Anthony Andras
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there everyone,

I had a quick question in regards to getting ahold of page history with servlets. Here is my problem:

I am currently in a JSP/Servlet class. For this assignment, I cannot use any MVC framework as I believe the spirit of this assignment is to learn how these frameworks work under the hood.

I am attempting to perform basic form validation on an HTML form when a user issues a request to a servlet using POST. I have a few files defined:

index.jsp (Main JSP)
AmtrackServlet.java (Servlet to process POST requests)
AmtrackForm.java (Form object that maintains state of form as well as performs form validation)
Validateable.java (Interface for future forms that defines a validate method)

Now when a request comes through, my servlet builds the AmtrackForm object based on parameters received from my HTTPServletRequest object. The servlet then calls my validate method. After the servlet calls my validate method, if errors occur, the servlet should forward the request back to the JSP page and my JSP page display the error messages (which will be written into the request).

The issue I am having is when I attempt to process the servlet, I haven't been able to locate a way to grab the URL of the JSP that initially submitted the form. A call to HttpServletRequest.getRequestURL() returns me the url-pattern that I have assigned to the servlet in the servlet mapping (web.xml). I would rather not hard code my forward into RequestDispatcher.forward if I don't have to.

I have basically come up with one solution which seems to be a hack. I started putting a hidden element in my HTML form and passing that through the request.

I was wondering if anyone had any ideas for solutions to this problem or if I am missing something entirely in the servlet API that would give me the solution?

Thank you everyone in advance and sorry it took me so long to describe the issue.

Best,

Anthony
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65226
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's hack-y about the hidden variable solution? It's a fine way to achieve loose coupling between the servlet and the JSP.
 
Anthony Andras
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I don't like about the hidden value solution is that in the event that the page needs to be moved to a different server, you then have to update all of your hidden fields to refer to the url on the new server or domain.

 
Vijitha Kumara
Bartender
Pie
Posts: 3918
10
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anthony Andras wrote:What I don't like about the hidden value solution is that in the event that the page needs to be moved to a different server, you then have to update all of your hidden fields to refer to the url on the new server or domain.

Aren't your hidden URL's context relative?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65226
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anthony Andras wrote:What I don't like about the hidden value solution is that in the event that the page needs to be moved to a different server, you then have to update all of your hidden fields to refer to the url on the new server or domain.

What are the chances that the page will be moved to a different application and server?
 
Anthony Andras
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
Anthony Andras wrote:What I don't like about the hidden value solution is that in the event that the page needs to be moved to a different server, you then have to update all of your hidden fields to refer to the url on the new server or domain.

What are the chances that the page will be moved to a different application and server?


Good point.

Thanks!
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11944
212
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anthony Andras wrote:What I don't like about the hidden value solution is that in the event that the page needs to be moved to a different server, you then have to update all of your hidden fields to refer to the url on the new server or domain.

If that was an issue for you, you could probably have the hidden field populated dynamically - there could be easy ways to do this from both server side and client side code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic