Calling jsp from Servlet?

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
AntoniRyszard656Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AntoniRyszard656Author 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
fargoCommented:
use request dispatcher like follows

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


AntoniRyszard656Author 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
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

fargoCommented:
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?


AntoniRyszard656Author 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.
fargoCommented:
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.
AntoniRyszard656Author 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.
rrzCommented:
>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>
AntoniRyszard656Author Commented:
The book was Begining Java Server Pages from wrox press.
rrzCommented:
In the book, how did they access the JSPs ?
AntoniRyszard656Author 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.
rrzCommented:
Did you try my method ?
TimYatesCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.