Make Localhost/projectName homepage url - java

I'm not sure how to start on this, but I currently have a simple application that has a home page url of
localhost:8080/projectName/homePage.jsp
However, I'd like it so that
localhost:8080/projectName/
OR
localhost:8080/projectName
sends me to the homePage.jsp.
I've read about an index.jsp that was created by eclipse in other projects, but it seems like that hasn't been done for me - do I need to create this? I'm not using a web.xml, and am instead relying on #WebServlet to do wiring.

2 ways to do this.
1. Add an entry in web.xml as follows
<welcome-file-list>
<welcome-file>homepage.jsp</welcome-file>
</welcome-file-list>
2. create an index.jsp file in your WebContent folder and forward the request to homePage.jsp

According to the following post there is no such way possible with annotation:
Servlet 3.0 annotations <welcome-file>

Related

Tomcat: Servlet Mapping vs. WebServlet Annotation

There are two ways for servlet mapping.
The first is in web.xml:
<servlet>
<servlet-name>foo</servlet-name>
<servlet-class>com.whatever.foo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>foo</servlet-name>
<url-pattern>/foo</url-pattern>
</servlet-mapping>
The second method uses the WebServlet annotation:
#WebServlet("/foo")
public class foo extends HttpServlet {
...
}
Which one is better? Where are the advantages of the first and the second way?
Provided that you're sure that you're using Tomcat 7 or newer, the webapp's web.xml has to be declared conform Servlet 3.0 spec in order to get Tomcat to scan and process the annotations. Otherwise Tomcat will still run in a fallback modus matching the Servlet version in web.xml. The support for servlet API annotations was only added in Servlet 3.0 (Tomcat 7).
So, the root declaration of your web.xml must look like below (make sure you remove any DOCTYPE from web.xml too, otherwise it will still be interpreted as Servlet 2.3!).
<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
Further, there's a minor difference in the URL pattern. The URL pattern /notifications will let the servlet only listen on requests on exactly that path. It does not kick in on requests with an extra path like /notifications/list or something. The URL pattern /notifications/* will let the servlet listen on requests with extra path info as well.
The minimum #WebServlet annotation should thus look like this
#WebServlet("/notifications/*")
The rest of attributes are optional and thus not mandatory to get the servlet to function equally.
What is benefit of using java based config instead of web.xml for servlet 3.x?
It avoids repeating yourself, and making mistakes by doing so. The servlet class is, for example, com.foo.bar.SomeServlet. Using web.xml, you're forced to re-enter this class in web.xml:
<servlet-class>com.foo.bar.Someservlet</servlet-class>
But wait, you've made a typo and you'll only discover it at runtime.
Or you rename a servlet class, but you forget to rename it in the web.xml as well, and you only discover the mistake at deployment time.
Finally, they make our life easier. You're creating a servlet, and you obviously want to map it to some URL. So you just add an annotation. No need to go to another file to add the mapping, then go back to the class because you forgot its exact name, then go back to the file again. Everything regarding a servlet is in the servlet class. Same for a filter, listener, etc.
Annotations don't have all these problems.
I hope this helps you!
XML configuration :
advantages :
All mappings are in the same location, you have an overview of all of them in one single file.
disadvantages :
Needs a separate file in addition to the class files.
Annotation configuration :
advantages :
The mapping is described directly inside the relevant class.
disadvantages :
You have to open a particular class to see its mappings.

JSP Tomcat Bad Resources

We are deeper into our project, and have set up a basic web application with eclipse. Whenever we attempt to run the server, we get a 404 error like the following
From my research, I have found I need some sort of web.xml file. Where should put this file what should be in it. How do I make it
Your problem is your trying to access a resource within the WEB-INF folder, that is not allowed and that is the reason you have this error, you must put the jsp under the WebContent folder or any folder under the WebContent folder.
The WEB-INF\classes contains the .class files for your Java classes and it's not a good place to put any resource.
Since the 3.0 specification for servlets the web deployment descriptor (web.xml) is optional.
I hope this could help you to fix your problem
Putting JSPs under WEB-INF makes them inaccessible unless you map them in WEB.XML The purpose of WEB-INF is to hide things because users cannot download or access anything under WEB-INF. You only put JSPs under WEB-INF if you don't want the user to be able to go there by its real name (i.e. http://localhost/app/whatever.jsp) but want to map it to some specific url (i.e. http://localhost/app/somename/)
Mapping a JSP that is under WEB-INF to a URL cane be done with this in the WEB.XML
<servlet>
<servlet-name>somename</servlet-name>
<jsp-file>/WEB-INF/whatever.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>somename</servlet-name>
<url-pattern>/somename/*</url-pattern>
</servlet-mapping>
Of course, if you need to map another URL to the JSP but don't feel the need to disallow the user from going there by its .jsp filename, you can use a URL Rewriting filter for that. In that case, there is no point in putting it under WEB-INF.

Spring MVC unable to map URL's

I created a spring MVC project, with a controller class having request mapping methods but when I run the project on server it gives me a 404 error (Requested resource not found)
Attached below are the screenshots of my web.xml, idm-servlet.xml and the controller class files.
After running the project, the URL remains the same instead of getting changed due to mapping.
In the url-pattern of idm servlet you listen to any HTTP requests which ends with ".do"
While your request mapping contains "/home"
I would do change the mapping to "/home.do"
Please add in web.xml:
<welcome-file-list>
<welcome-file>home</welcome-file>
</welcome-file-list>
It should help.
Try to change DispatcherServlet mapping form *.do to /. Also I think you miss some dependencies(jars).

How to map all urls to a single .html page

I'm new to Tomcat, and understand servlet-mapping, but was hoping I could do some mapping to a html file residing in the webapp/ folder.
I have a simple javascript web application that resides as webapp/index.html. Since I'm messing with the url in javascript, I want to make it possible to map /console/* to hit webapp/index.html. For example /console/hi and /console/bye should both load up webapp/index.html.
Is this possible? If so, how?
You can change your index.html file to same index.jsp file
and then use this mapping in web.xml
<servlet>
<servlet-name>index</servlet-name>
<jsp-file>index.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>index</servlet-name>
<url-pattern>/console/*</url-pattern>
</servlet-mapping>
I think it is the easiest way.
Like #Pointy said, you'll need a filter, that will receive all requests and redirect to the .hmtl page you want...
Here is a link with a simple filter implementation.
Hope it helps
I've published a Java servlet filter implementation that does what you need to Github. The code is pretty simple, and I expect you might want to customize it to your own needs.
https://github.com/lookfirst/history-api-fallback

Remove Foobar.xhtml from URL with JSF

I have written a facelets web application using tomcat as a application server. My program has a foobar.xhtml and the URL to it is:
http://localhost:8080/Myapplication/foobar.faces
Can I change something in my application so that a link to:
http://localhost:8080/Myapplication/
..will actually render my application on http://localhost:8080/Myapplication/foobar.faces ?
Alternatively, could the http://localhost:8080/Myapplication/ be redirected to http://localhost:8080/Myapplication/foobar.faces ?
You would normally use the <welcome-file> entry in the web.xml for this. But unfortunately this doesn't work as expected on at least Tomcat when using fictive URL's which are to be passed through a servlet like a FacesServlet. Tomcat will scan for the physical file on the disk matching the exact name before forwarding. If it isn't present, then you will just face a default 404 error page.
Using /foobar.xhtml as <welcome-file> is also not going to work since that page requires to be parsed by the FacesServlet to get all the JSF stuff to work.
One of the ways to fix this is to place another real /foobar.faces file there next to the real /foobar.xhtml file. It doesn't need to be filled with code, it can be left empty. Just the presence of the physical file is enough for Tomcat to open the desired page as welcome page.
web.xml has a
<welcome-file-list>
<welcome-file>foobar.faces</welcome-file>
</welcome-file-list>
element where you can define the page to be opened.

Categories