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

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


0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
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
0
 
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?


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

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.