We help IT Professionals succeed at work.

Calling jsp from Servlet?

AntoniRyszard656
on
Medium Priority
404 Views
Last Modified: 2010-04-01
Hello,

My java server book recommends storing the jsp pages inside the web-inf folder. To be sure the jsp pages cannot be directly accessed by the user.

Could I ask to call the jsp from a servlet, do I need to add the jsp in the web.xml. And should the html pages also be placed in the web-inf?

tomcat/webapps/dbtest
tomcat/webapps/dbtest/src/ ----- src contains .java files
tomcat/webapps/dbtest/web/ ------ web contains .jsp and html files
tomcat/webapps/dbtest/web/web-inf/ ---- web-inf contains web.xml
tomcat/webapps/dbtest/web/web-inf/classes/code/ ------- contains the .class files

Thanks
Comment
Watch Question

Author

Commented:
Would I use the sendRedirect() method in the servlet's to start the jsp call?

res.sendRedirect();

I thought the alternative forward method was the same as c++ geto() and not recommended.

thanks
Commented:
use request dispatcher like follows

RequestDispatcher dispatcher = request.getRequestDispatcher("/relative/path/to/jsp");
dispatcher.forward(request, response);


Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
When copying the jsp files to the web-inf folder do we add the files to the web.xml

And should the html be in the web-inf folder?

Thanks

Commented:
When copying the jsp files to the web-inf folder do we add the files to the web.xml
And should the html be in the web-inf folder?

>> Why to do this?? the present structure to keep the jsp files in the folder named web is fine. Why to move them to WEB-INF folder?


Author

Commented:
I read in my java text moving the jsp files to the web-inf means the user cannot access the .jsp pages directly. Which is good for security reasons.

Commented:
well this is all very contradictory. In tomcat if one make the listings option in web.xml to false..then also one cannot access the jsp files. This option of putting jsp files in WEB-INF is all coming now..and cannot be said as a good practise.

Author

Commented:
It's just in most books I have seen they recommend putting the jsp's in the web-inf folder. At the moment I generally follow the books as a reference to learning servlets.
rrzstudent
CERTIFIED EXPERT

Commented:
>It's just in most books I have seen they recommend putting the jsp's in the web-inf folder    
Please post the names and authors of those books.  
I do share fargo doubts. What code are you hiding ?  Anyway if you really want to put them inside  WEB-INF  then use something like the following to map your JSP file.    

    <servlet>      
                   <servlet-name>mySecureJsp</servlet-name>
                   <jsp-file>/WEB-INF/secure.jsp</jsp-file>
    </servlet>
    <servlet-mapping>
                    <servlet-name>mySecureJsp</servlet-name>
                    <url-pattern>/securedFile</url-pattern>
    </servlet-mapping>

Author

Commented:
The book was Begining Java Server Pages from wrox press.
rrzstudent
CERTIFIED EXPERT

Commented:
In the book, how did they access the JSPs ?

Author

Commented:
I think it was in a section of recommendations.

I am not sure if the actually demonstrated how to access the jsp's from the web-inf folder.
rrzstudent
CERTIFIED EXPERT

Commented:
Did you try my method ?
CERTIFIED EXPERT
Top Expert 2004
Commented:
When using Struts, I often stick the jsps inside the WEB-INF directory for the reasons stated here...

People go to:

    Whatever.do

And the templated tiled jsp from WEB-INF/pages is returned

If people just look at the component JSPs which make up the complete page there is a chance something I would rather is hidden may sneak through...

Probably won't though, and I don't do it all the time ;-)

And I would only bother when using a framework...  Otherwise you're going to need some sort of dispatcher to handle it all...

Tim
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.