[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
Re: [jetty-users] Single page application routing
|
If somebody going to google for the same issue, this is how i solved mine.
ErrorHandler.java
-------------------------------------------------------------------
package com.myproject;
import com.myproject.logger.LoggerFactory;
import org.apache.log4j.Logger;
import org.eclipse.jetty.server.Request;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
public class ErrorHandler extends org.eclipse.jetty.server.handler.ErrorHandler
{
private static Logger logger = LoggerFactory.getLogger();
private boolean _fistRequest = true;
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException
{
// On 404 page we need to show index.html and let JS router do the work, otherwise show error page
String redirectRoute = "/index.html";
if ((response.getStatus() == HttpServletResponse.SC_NOT_FOUND) && _fistRequest) {
RequestDispatcher dispatcher = request.getRequestDispatcher(redirectRoute);
if (dispatcher != null) {
try {
// reset response
response.reset();
// On second 404 request we need to show original 404 page, otherwise will be redirect loop
_fistRequest = false;
dispatcher.forward(request, response);
} catch (ServletException e) {
super.handle(target, baseRequest, request, response);
}
}
} else if ((response.getStatus() == HttpServletResponse.SC_NOT_FOUND) && ! _fistRequest) {
logger.error("Can not find internal redirect route " + redirectRoute + " on 404 error. Will show system 404 page");
} else {
super.handle(target, baseRequest, request, response);
}
}
@Override
protected void writeErrorPageBody(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks) throws IOException
{
writeErrorPageMessage(request, writer, code, message, request.getRequestURI());
}
@Override
protected void writeErrorPageMessage(HttpServletRequest request, Writer writer, int code, String message, String uri)
throws IOException
{
String statusMessage = Integer.toString(code) + " " + message;
logger.error("Problem accessing " + uri + ". " + statusMessage);
writer.write(statusMessage);
}
}
------------------------------------------------------------------------------------------
Add error handler in Launcher.java
------------------------------------------------------------------------------------------
WebAppContext context = new WebAppContext("webapp", "/");
context.addServlet(new ServletHolder(new DefaultServlet()), "/*");
// Setting custom error handler. Which also acts as URL rewriter in some way.
// On 404 page we need to show index.html and let JS router do the work
context.setErrorHandler(new ErrorHandler());
> On 22 Dec 2014, at 11:26, Stefan Magnus Landrø <stefan.landro@xxxxxxxxx> wrote:
>
> Ok. Use a servlet instead then in the mapping and have it redirect to the correct page
>
> Sendt fra min iPhone
>
>> Den 22. des. 2014 kl. 10.22 skrev Dmitry Polovka <messaged@xxxxxxxxxxx>:
>>
>> Adding a 404 error-page to web.xml does the trick, but it sends actual 404 status code in header. I need 200.
>>> On 22 Dec 2014, at 05:57, Stefan Magnus Landrø <stefan.landro@xxxxxxxxx> wrote:
>>>
>>> Adding a 404 error-page to web.xml should do the trick.
>>>
>>> Stefan
>>>
>>>> Den 21. des. 2014 kl. 21.50 skrev Dmitry Polovka <messaged@xxxxxxxxxxx>:
>>>>
>>>> Hey everybody, i am beginner. Can somebody advise me on how to manage single page application routing?
>>>>
>>>> I need DefaultServlet to work as it works now - serve static content, handle ETags and do other stuff.
>>>> Since JS will handle routing, on unknown route to Jetty, Jetty will throw 404 and i need to intercept these 404 errors
>>>> to output content of index.html and let JS do the work.
>>>>
>>>> What is the best way to do that?
>>>>
>>>> Launcher.java
>>>> -----------------------------------------------------------------------------
>>>> public static void main(String[] args) throws Exception
>>>> {
>>>> Server server = new Server();
>>>>
>>>> // Remove Server:Jetty(9...) from Response Headers
>>>> HttpConfiguration httpConfig = new HttpConfiguration();
>>>> httpConfig.setSendServerVersion(false);
>>>> HttpConnectionFactory httpFactory = new HttpConnectionFactory(httpConfig);
>>>>
>>>> ServerConnector connector = new ServerConnector(server, httpFactory);
>>>> connector.setPort(8080);
>>>> server.addConnector(connector);
>>>>
>>>> WebAppContext context = new WebAppContext("webapp", "/");
>>>> context.addServlet(new ServletHolder(new DefaultServlet()), "/*");
>>>>
>>>> // Map WebSocket to listen on `/ws`
>>>> Broker broker = new Broker();
>>>> context.addServlet(new ServletHolder( new Servlet(broker)), "/ws");
>>>>
>>>> // Setting up browser caching. Binds params for org.eclipse.jetty.servlet.DefaultServlet.init()
>>>> context.setInitParameter("org.eclipse.jetty.servlet.Default.etags", "true");
>>>> context.setInitParameter("org.eclipse.jetty.servlet.Default.cacheControl", "public, max-age=0");
>>>>
>>>> // Disallow directory listing
>>>> context.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
>>>>
>>>> // Search for index.html/index.jsp pages
>>>> context.setInitParameter("org.eclipse.jetty.servlet.Default.welcomeServlets", "true");
>>>>
>>>> // Fix for Windows, so Jetty doesn't lock files
>>>> if (System.getProperty("os.name").toLowerCase().contains("windows")) {
>>>> context.setInitParameter("org.eclipse.jetty.servlet.Default.useFileMappedBuffer", "false");
>>>> }
>>>>
>>>> // Will throw an exception when will be unable to start server for some reason
>>>> context.setThrowUnavailableOnStartupException(true);
>>>>
>>>> server.setHandler(context);
>>>> server.start();
>>>> }
>>>> —————————————————
>>>>
>>>> I also have jersey mapping in web.xml
>>>>
>>>> <servlet-mapping>
>>>> <servlet-name>jersey-api-v1</servlet-name>
>>>> <url-pattern>/api/v1/*</url-pattern>
>>>> </servlet-mapping>
>>>> _______________________________________________
>>>> jetty-users mailing list
>>>> jetty-users@xxxxxxxxxxx
>>>> To change your delivery options, retrieve your password, or unsubscribe from this list, visit
>>>> https://dev.eclipse.org/mailman/listinfo/jetty-users
>>> _______________________________________________
>>> jetty-users mailing list
>>> jetty-users@xxxxxxxxxxx
>>> To change your delivery options, retrieve your password, or unsubscribe from this list, visit
>>> https://dev.eclipse.org/mailman/listinfo/jetty-users
>>
>> _______________________________________________
>> jetty-users mailing list
>> jetty-users@xxxxxxxxxxx
>> To change your delivery options, retrieve your password, or unsubscribe from this list, visit
>> https://dev.eclipse.org/mailman/listinfo/jetty-users
> _______________________________________________
> jetty-users mailing list
> jetty-users@xxxxxxxxxxx
> To change your delivery options, retrieve your password, or unsubscribe from this list, visit
> https://dev.eclipse.org/mailman/listinfo/jetty-users