2

Is it possible to have some kind of empty JSP (index.jsp) with ~only auto redirecting to servlet?

Or can I start my web app not from page (jsp/html) but from servlet? (web.xml says no)

I have logic needed in (for example) index.jsp page inside my Logic.java servlet - that's why I need to use servlet before any useful JSP (don't want to mix logic with UI using scriptlets)

Does it can be done?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
dantuch
  • 9,123
  • 6
  • 45
  • 68

2 Answers2

5

I think what you want is

<jsp:forward page="/controller-name" />

or you can redirect using

<c:redirect page="..."/>

The difference is first will forward which means the user's url won't change and the latter will change the user's url.

Amir Raminfar
  • 33,777
  • 7
  • 93
  • 123
1

That's possible if you provide an empty index.jsp and map the servlet on

<url-pattern>/index.jsp</url-pattern>

The servlet should in turn however forward to a JSP to present the results. The very same index.jsp maybe? :)

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • simple and works as I need :) ... **anyway is it "a good practice"?** my *logic needed* is labels internationalization, that I'm doing with *not jsp way* - without those jsp (or maybe jstl?) tags - just using `ResourceBundle.getBundle("MyResources", currentLocale);` with properties file ... it's more like desktop app internationalization – dantuch May 18 '11 at 19:34
  • For internationalization? No, that isn't. Rather use JSTL `fmt` taglib. See also http://stackoverflow.com/questions/4276061/how-to-internationalize-a-java-web-application for a mini tutorial. – BalusC May 18 '11 at 19:35
  • Also, if you want to internationalize all your JSPs by a servlet, you'd rather to map it on **all** JSPs, right? E.g. `*.jsp`. But after all, this isn't the right approach. Use JSTL. Or if you're using a (MVC) framework like JSF, Spring MVC, Struts2, etc, you should use the tags/expressions offered by the framework itself. – BalusC May 18 '11 at 19:42
  • ok, Ill stick to JSTL approach. Anyway - didn't need mapping all, I was just passing session object with setted locale and even translated words, so my `.jsp` codes looked really cute - like ``. Anyway this isn't popular way to do it, and it would be better to use `ftm`s, yes? – dantuch May 18 '11 at 19:48
  • Well, that depends on the mapping of the servlet where you created the session object and stored the translations. Does it work if you open index.jsp on a fresh new session? Anyway I think it's pretty cumbersome after all to put all keys in the request/session scope without having a base name. You have to be careful that you don't accidently use the same key for something else. And yes, using JSTL `fmt` taglib is definitely the way to go when it concerns a basic JSP/Servlet application. It's maybe not immediately trivial, but the mini tutorial should be clear enough to get started, is it? – BalusC May 18 '11 at 19:51
  • Sure it is :) This approach is simplier than my own. (anyway it's good to try something different) Btw. - Your tut is even better than that one that I found on ibm.com ;) Great work, you can write books :) – dantuch May 18 '11 at 19:57
  • You're welcome. I can't even count on my hands anymore how many people suggested me to write books... Should I really? Besides, I'm more a JSF oriented guy although I know the basic JSP/JSTL/Servlet concepts pretty well. – BalusC May 18 '11 at 19:58
  • Yes, it not about knowledge. But about the way you share it. How simple it was to follow your tut, and how easy is talking to you, even just for random guy like me :). Reading your tut, and your hints for me now, reminded me conversation between Gamma and some interviewing him guy - about design patterns and his thoughts after feedback to GoF... - here it is - http://www.artima.com/lejava/articles/gammadp.html - its really pleasurable to read and it does learn you much. Anyway writing book is business and way to make money or just lost time, but you should think about it in first place :) – dantuch May 18 '11 at 20:07