• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can i call a servlet from within a JSP and return immediately?

 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks!

I have the following problem. I have a JSP which is called directly from a browser and this has to be a real existing JSP not just a "virtual" servlet mapping.

For this JSP i want to create some Java beans with the content the JSP should display. I wanted to do these initialization tasks with the help of a servlet so that there has to be no Java code in this JSP, just EL or JSTL tags. After the servlet has created my Java beans it should immediately return to the same JSP which then should display some content of these beans. The user should always just see the URL with "index.jsp" in his browser so all has to happen behind the scenes. And this should happen just by opening the page in a browser because i don't even have some form elements or something like this to call the servlet as a form action.

Is it possible to call something like a servlet (or possibly some other Java class) to do some initialization without a redirect, include oder forward? And of course with no scriptlets or Java code in the JSP ;)

With a RequestDispatcher i almost managed to get i work, but then i still would have the problem that the browser shows the URL of the servlet and not the JSP.
Another idea was a request listener but i don't see a way to register a listener for a JSP.

I know there are design patterns and thousand ways to do this better. But it's an exercise for an exam and our professor seemingly doesn't have the vaguest notion of producing good code respectively JSPs without code. So i just hast to do this like described above. At least i'm trying to avoid scriptlets in the JSP.


Thanks for any help!

Marco
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You know there is nothing stopping you from creating a servlet mapping named "index.jsp", right. I can't imagine why your professor would require that you have direct access to a JSP.

That being said...
A call to response.sendRedirect("index.jsp") would return you to the JSP and insure that "index.jsp" shows up in the browser's address window.



Look at the jsp:useBean action in the JSP spec.

It's shorthand for "See if the bean exists in the given scope, if not create it and bind it to scope.
You can also add jsp:setProperty tags to the body of the useBean action that will only be called when the bean is being instanciated.

With this, you can move all startup code into either the constructor or one of the accessor methods of the bean.

You might also want to look into custom tags.
[ July 19, 2007: Message edited by: Ben Souther ]
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You know there is nothing stopping you from creating a servlet mapping named "index.jsp", right.


Of course I do know, but my professor obviously doesn't know ;)


I can't imagine why your professor would require that you have direct access to a JSP.


If you have to rate an exam it's probably easier if you just allow the things you are familiar with. Being prof doesn't mean you have to know a lot about the topic of your lessons. Of course you should expect that a professor has more knowledge than his students but reality shows this is not always the case.

Your hint a useBean is very useful. I should have remembered this tag. But in my special case there's still a little problem left:

The user has to authenticate itself to be able to access the JSP. And the content of the JSP should vary depending on the logged in user.
So is there a way to get a request object in my JavaBean constructor to retrieve the user principal from it?

Marco
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the sake of anyone else reading this: This is a hack (of Rube Goldburg proportions) to get around a quirky requirement made by said professor).

Look at:
ServletRequestAttributeListener
and the ServletRequestAttributeEvent
object that is passed to it by the container when an object is bound, unbound, or replaced from request scope.

Because this is a homework assignment, I'm not going to get into any more than that but this should give you some options to explore.
[ July 19, 2007: Message edited by: Ben Souther ]
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, thanks a lot Ben! I'm unworthy

I knew the listeners but this idea wouldn't have ocurred to me. But surely this will do my work although it is a hack. I think there wouldn't be a need for such a hack if i wouldn't have to meet the strange requirements.

And for clarification: This is not a homework. I'm just doing this exercise to prepare for the exam because I'm sure the requirements for the exam won't be more reasonable ;)

Thanks again for your fast help!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!