I'm learning about servlets and JSP and I try to make an app. The app is from this page
This is the structure of the app:
And I put all the JSP in WEB-INF:
When I try to run any jsp it's not working, I get this error: HTTP Status 404 – Not Found... Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
If I move the jsp in WebContent the app is working succesfully. But I want to keep the jsp in WEB-INF. What should I do? Thanks in advance!
Seems i got your problem. If you have servlet class write code to forward to jsp page like this. Because you have put your jsp page inside WEB-INF/view
request.getRequestDispatcher("/WEB-INF/view/homeView.jsp").forward(request, response);
Related
I am building a servlet app to run on tomcat server. However when i was not able to update the jsp page using:
request.setAttribute("operation", "op_name");
request.getRequestDispatcher("\WEB-INF\index.jsp").forward(request, response);
404 index.jsp not found comes.
I think that servlet and web elements like jsp are in different paths that's why it is going wrong. So do i need to make another project using maven structure like having src\main\java etc. or this structure is also fine? Also how to update the index.jsp using this structure?
I've had this simple jsp log-in form that used the css style sheet. It was working just fine until I included the web.xml file in my directory. Now the JSP file won't load the css files no matter what. I've tried changing the link address of the css in multiple different ways but with no luck.
I'm pretty positive that the web.xml file is the reason it's not loading because if I remove it, everything works again.
Here's my project structure:
Here's my login.jsp:
here's my web.xml:
Your web.xml includes /* in a security constraint, limiting access to all content of your application to those users with the role users. This means that, instead of serving your CSS file as requested to the browser, tomcat will redirect to the login.jsp as well (which is obviously an incorrect and not very stylish stylesheet)
i have made one project on E-Commerce using JSP technology of JEE, in that i have assigned a page named as home.jsp as a welcome page of my project. The page is located in the home_page folder of the Web Contents folder of my project. But when i run the project the contents of home.jsp don't get loaded in the browser page. I am attaching the code of the welcome page and the screenshot of the default welcome page in the browser apart from that i am also attaching the screenshot of the welcome page which is expected to come when the project executes.
<welcome-file-list>
<welcome-file>/home_page/home.jsp</welcome-file>
</welcome-file-list>
Try adding "/" at the end of the URL you're trying to access. It should fix it if you have servlets mapped in your web.xml.
i have a question:
I have a scenario that when user opens http://MYWEBSITE.com/abc/ , the user is directed to xyz.html page which is in abc subdirectory. I am using Java for web development. How can I do this in web.xml?
P.S. URL is http://MYWEBSITE.com/abc/ not http://MYWEBSITE.com/abc
you can use the welcome-file attribute in the web.xml
example:
<welcome-file>xyz.html</welcome-file>
This would get tomcat to look for a page matching the name (if found) and load it.
My question is how to put all the JSP files in WEB-INF/JSP/ in the proper manner?
Is there any configuration for this as the structure I'm aware of is:
WEB-INF / JSP --> all jsp is reside in that folder
/ CLASSES -- all classes is reside that folder
/ LIB --> library file reside in that folder
How do I set this up properly according to the spec. Please help me with an answer for this.
You can put your JSP in
WEB-INF/jsp
folder and access that JSP using servlet.
Create login.jsp and then access that JSP using preloginservlet.java. This servlet redirects to login.jsp which is in the WEB-INF/jsp folder.
Its not a standard practice or valid as per the J2EE spec (I know using most of the java Web development frameworks like Struts, Spring MVC, Stripes you can do this). As per the spec, all our publicly accessibly pages should be out side of WEB-INF. But if you want the pages to be in web-inf, what you can do is to create a servlet along the lines of a controller servlet and forward the requests to jsp pages from your servlet and those pages can be in WEB-INF, and there is no special configuration that can be done to do this.
Create an intermediary JSP outside of WEB-INF that includes your JSP.
e.g.
your page inside of WEB-INF is ProjectName/WEB-INF/JSP/yourPage2.jsp
create a page ProjectName/yourPage1.jsp
Write below code in yourPage1.jsp
yourPage1.jsp
<%# include file="WEB-INF/JSP/yourPage2.jsp" %>
You create a jsp page out side WEB-INF folder and inside that jsp use jsp:forward as
In web.xml file use give outside jsp name in welcome file list.
It works for me...