Spring mvc: Dynamic 404 page - java

<error-page>
<error-code>404</error-code>
<location>/404.html</location>
</error-page>
This is my current configuration for 404 errors.
What I want:
When a person types www.host.com/word . I want the string "word" as input for my search url (which is www.host.com/search.do?keyword).
Does anyone know how to do this?

Map / to your DispatcherServlet (see section 16.2 of the Spring reference documentation) and set up a URI template as shown in section 16.3.2.1. For example, you could set up a request mapping like so:
#RequestMapping(value="/{keyword}", method=RequestMethod.GET)
public ModelAndView doSearch(#PathVariable("keyword") String keyword)
{
// forward to your search page
}

Related

Java EE - more generic error code mapping

I'm currently mapping error codes in web.xml and so far it looks like this:
<error-page>
<error-code>404</error-code>
<location>/errorHandler?code=404</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/errorHandler?code=500</location>
</error-page>
Do I have to map every single error code manually or is there more automatic way? I'd like to have something like this:
<error-page>
<error-code>*</error-code>
<location>/errorHandler?code=*</location>
</error-page>
That's possible, according to the documentation of oracle. It depends on whether you are using servlet 2.5 (not supported) or 3.0 (supported).
I would recommend you to use plain html for the error codes.
The reason behind this, is these pages will be shown when something is going very wrong. Full list of status codes
Generic solution example: moreinfo with onehippo
From security perspective it is not recommended to give information what went wrong in the application: https://blog.whitehatsec.com/error-handling-in-java-web-xml/

Replace Error Handling from JSP to Servlet

I have this code in my JSP script
<%# page errorPage ="error.jsp"%>
<%!
...My JSP Code... //No matter for this question
%>
My error.jsp script is:
<%# page isErrorPage = "true"%>
<% if (exception != null) { %>
The cause of the exception error has been:
<% exception.printStackTrace(new java.io.PrintWriter(out)); %>
<% } %>
like:
http://www.tutorialspoint.com/jsp/jsp_exception_handling.htm
But I want to replace all before code to servlet...
As you can see...
I need to translate <%# page errorPage ="error.jsp"%> to Servlet code...
and error.jsp entirely...for example How translate this:<%# page isErrorPage = "true"%>?
Sorry, but I don't know how to do it...
PD:
I was looking for my question, but sites tell that I need to change web.xml file, I don't want to do it, I think that (My JSP script works fine, and the converted by Tomcat servlet works fine too without change that file).
hey buddy #Luigi Giuseppe. I thing this one will be going to satisfying for you doubt.
Two ways to define Error page in Java web application written using Servlet and JSP like your question related.
1. First way is page wise error page which is defined on each jsp page and if there is any unhanded exception thrown from that page, corresponding error page will be displayed.
2. Second approach is an application wide general or default error page which is shown if any Exception is thrown from any Servlet or JSP and there is no page specific error page defined.
HTTP standard error codes:
1. Information This one is a new return code which is not 100%
supported and normally and only provides information to the client
about the request.
2. Success response, the request has been correctly executed ->
expected answer from server (http code 200).
3. Redirection response. The resource has moved and is not any more at
this URL.
4. Error on client side. Probably most known of all is error 404 : Not
Found.
Defining error.jsp page like:
//error.jsp
<%# page isErrorPage="true"%>
//login.jsp
<%# page errorPage="error.jsp"%>
And, Error page in Java Web Application JSP Servlet page is like:
Default Error page based on Exception:
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/error.htm</location>
</error-page>
Default Error page based on HTTP Error code:
<error-page>
<error-code>500</error-code>
<location>/internal-server-error.htm</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/page-not-found-error.htm</location>
</error-page>
Create your custom error pages:
1. 400.html -> telling the user did something wrong
2. 500.html -> telling the server did something wrong
400.html:
<error-page>
<error-code>400</error-code>
<location>/404.html</location>
</error-page>
<error-page>
<error-code>401</error-code>
<location>/404.html</location>
</error-page>
500.html:
<error-page>
<error-code>500</error-code>
<location>/500.html</location>
</error-page>
<error-page>
<error-code>501</error-code>
<location>/500.html</location>
</error-page>
hey buddy #Luigi Giuseppe. Yes, #mikemil's telling right buddy, You should just use the try/catch and otherwise use the method to display the exception error like as following code like this:
package com.jmail.servlet.exception;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#WebServlet("/MyExceptionServlet")
public class MyExceptionServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
throw new ServletException("GET method is not supported.");
}
}
Now, you just getting error page like this:
GET method is not supported.

servlet-mapping url-patterns static pages and REST API

I am trying to figure out how i am building a restFull Web server that will:
1: Serve a single index.html file when going to /
2: all other URLs will get caught by the controllers in a restful manner
for example:
/invoke1 -> will reach the request mapping of "/invoke1"
The problem:
First of all spring dispatcher has some weird fallout with serving html pages (i am not talking about resources and the <mvc:resources mapping ) i am talking about serving an html page and not JSP.
i solved this issue by using the default catalina servlet like so:
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>
org.apache.catalina.servlets.DefaultServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/docs/*</url-pattern>
</servlet-mapping>
With this code i can serve HTML pages (Even behind security, as long as they are under the /docs/* path), So from here i can do:
#RequestMapping(method = RequestMethod.GET)
public String main() {
return "redirect:/docs/index.html";
}
But that's not what i want. from here on out the URL on the browser will be displayed as /docs/
What i want to happen is that when the user goes to / he will get the index.html but when he goes to /invoke1 he will reach the spring controller.
This is something i am trying to figure out for some time now. hope you can guide me to the right solution.
Thank you.
1) The easiest solution would be to map Spring to something like /rest/ and leave / for your index.html.
2) Another solution, though you did not want it, to wrap your index.html up in a JSP and serve it on /index.html and define your controller as intended.
3) Or use <mvc:resources>. You can serve .html from a predefined static directory and simply return in your index controller "static/index.html". cf. this StackOverflow Question.
In your configuration file you can define one mvc:view-controller to handle the redirect URL:
<beans xmlns:mvc="http://www.springframework.org/schema/mvc"
.....
xsi:schemaLocation="...
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
....>
<mvc:view-controller path="/index" view-name="/docs/index.html" />
....
</beans>
And you can change your main method to redirect to /index:
#RequestMapping(method = RequestMethod.GET)
public String main() {
return "redirect:/index";
}
Now when a request is made to /, it will come to the main method, which will send a redirect request to /index. The above mapping will forward a request for /index to the static view index.html. And /docs/index.html will not be present in the browser URL.
With Spring 3+ and its resource handling you can just do:
map servlet to / - all requests (excluding *.jsp) will be handled by Spring
configure Spring to serve your resources - (thus the index.html will be served by Spring)
configure index.html as your <welcome-file> in web.xml
The simple answer is you need to use forward ("forward:/") and not redirect ("redirect:/").

Hide JSPs from FacesContext

I have a JSF application with a Servlet Filter configured for a urlPattern of /faces/*.
I want to hide a JSP from faces context so that it won't go through the Servlet Filter.
So I kept it under WebContent folder of my project as WebContent/Error.jsp and declared like the following in the web.xml:
<error-page>
<exception-type>java.lang.NullPointerException</exception-type>
<location>/Error.jsp</location>
</error-page>
But my Error.jsp never gets picked up. Instead I see 404 Page not found error.
To be more clear, I want my Error.jsp page URL to be:
http://localhost:8080/myappname/Error.jsp
But it is only reachable by:
http://localhost:8080/myappname/faces/Error.jsp
The same is the case when I declare any view-id in the faces-config.xml. Where do I keep the error JSP if I want to hide it from faces context?
Based on the information given so far, it looks as it should just work fine. You don't have a <dispatcher>ERROR</dispatcher> on the filter, so the filter should not be invoked at all whenever the NPE is thrown.
Apparently the NPE got wrapped up in another exception because it's been thrown at an illogical place such as bean's constructor instead of a normal bean action method. In such case, JSF would rethrow it as a ManagedBeanCreationException. The container would get it instead of the NPE and thus won't be able to locate the error page. In the container's default HTTP 500 error page, you should read the topmost exception of the stacktrace in order to determine the right exception to define an error page for.
Please keep in mind that runtime exceptions like NPEs are developer errors (bugs!), not production errors and that they should be fixed ASAP. I'd personally just use a global HTTP 500 error page for this kind of bugs:
<error-page>
<status-code>500<status-code>
<location>/errors/generic.jsp</location>
</error-page>
For more specific, real production exceptions, you can always declare a more specific error page:
<error-page>
<exception-type>com.example.YourDatabaseAccessException</exception-type>
<location>/errors/database.jsp</location>
</error-page>
<error-page>
<exception-type>javax.faces.application.ViewExpiredException</exception-type>
<location>/errors/sessionexpired.jsp</location>
</error-page>
If you define a filter and declear that in your web.xml, all the request will go through that filter, unless you define the filter mapping.
I think you can define a filter mapping in your web.xml as following:
<filter>
<filter-name>URLFilter</filter-name>
<filter-class>the filter class in your source code</filter-class>
</filter>
<filter-mapping>
<filter-name>URLFilter</filter-name>
<url-pattern>/some pages</url-pattern>//skip error.jsp here
</filter-mapping>
this is not tested, but just an inspiration.
edit: you can find out more from Oracle site
To me it doesn't sound like the Faces filter has anything to do with this issue. Can you reach http://localhost:8080/myappname/Error.jsp with the filter completely disabled? If not, then perhaps there is some issue with the Error.jsp file itself?
It's hard to say without seeing the code itself.

How to get original page url/uri after request dispatcher forward

I have a Error404Servlet which is configured as error-page for 404 in web.xml:
<servlet>
<servlet-name>Error404</servlet-name>
<servlet-class>com.foo.bar.Error404Servlet</servlet-class>
</servlet>
<error-page>
<error-code>404</error-code>
<location>/error404</location>
</error-page>
In this servlet i have to log the original url that caused 404, but request.getRequestURI() always returns "/error404"
How can i get the original url? The unly ugly method i know is to create filter that puts the original url to request attribute.
From the request you can retrieve attributes set by the container in the event of an error:
request.getAttribute("javax.servlet.error.request_uri");
other attributes that can provide usefull information;
javax.servlet.error.status_code
javax.servlet.error.exception_type
javax.servlet.error.message
javax.servlet.error.exception
See also this servlet 2.3 features article.

Categories