2

I'm currently trying to make a CMS project, where I will be making a so called "blogsite", something like wordpress. The problem that I'm facing is that I want to access the blogsite directly via url.

Something like local/CMS/site_name

normally it would be a 404 error since there is nothing that will be mapped there, so i catched it in the web.xml by

<error-page>
    <error-code>404</error-code>
    <location>/checkBlogUrl.action</location>
</error-page>

so I can process the URL request there, query the site, if it exists forward the page to there, if not error.jsp.

The code I'm currently trying to make it work is

    HttpServletRequest request = ServletActionContext.getRequest();

    String a = request.getServerName() +" : " + request.getRequestURI()
            + " : "+ request.getServletPath() +" : "+ request.getLocalName();
    System.out.println(a);

which sadly returned this

mycms : /CMS/checkBlogUrl.action : /checkBlogUrl.action : mycms

How can I get the site_name? Or, is there a better way to "forward" the url for a database query?

Jiro Manio
  • 137
  • 10
  • What you want to do ? redirect to a URL ? – Rookie007 Apr 21 '16 at 04:16
  • i want to do a query base on the sitename and pass that sitename to a struts2 action that will load my tiles for that sitename. eg. thisismysite.wordpress.com, if i access that, wordpress will send me to the blogsite i created. i just want to do something like that in the url format of locahost:8080/CMS/sitename – Jiro Manio Apr 21 '16 at 05:08
  • 1
    welp im stupid. i just found out about struts2 wildcard. will play around that for a while – Jiro Manio Apr 21 '16 at 05:39
  • You're not stupid, and the question is good. It has already two answers, though. Just use a default action ref and try reading the URL from there – Andrea Ligios Apr 21 '16 at 07:35
  • hm i think there is a misunderstanding. I'm not trying to catch the missing .action (actually I should, but not now). What I want to do was that if the URL is like this: localhost/CMS/sitename it will give me a 404, then by web.xml I will redirect it to an action, in my case checkBlogUrl.action. Everything is good up to that part. The problem was that I cannot get sitename inside the action. – Jiro Manio Apr 21 '16 at 08:16
  • basically I'm doing a roundabout way of forcing the site to check if the sitename exists (query in database) by catching 404 and forward the request to the blogsite. TLDR: user enter URL (local/CMS/sitename) -> web.xml gives 404 -> redirect to checkBlogUrl.action -> check database using sitename -> call action to load local/CMS/sitename. I'm stucked at getting the sitename – Jiro Manio Apr 21 '16 at 08:19
  • just to clarify further. directly typing local/CMS/sitename WILL give me 404 because I have no file, nor any action defined for that. what happens is that the by using sitename for query, i can get the template, then i can now call home-template via action – Jiro Manio Apr 21 '16 at 08:21
  • 1
    I have understood exactly what you were trying to do. The problem is that if you put 404 in web.xml, `web.xml gives 404 -> redirect to checkBlogUrl.action`. Redirection = new request = old URL is lots. TRY with the default-action-ref instead of the 404 in the web.xml, because everything is NOT right upt to that part, since you can't obtain what you want. I'm not sure if with default-action-ref the URL will still be accessible, but it is worth a try. If it works, answer your own question, and consider upvoting the one this is a duplicate of: http://stackoverflow.com/a/35549944/1654265 – Andrea Ligios Apr 21 '16 at 09:07
  • noted, will do thanks! – Jiro Manio Apr 21 '16 at 09:11
  • 1
    Not very clear, but how about just using named variables - http://struts.apache.org/docs/wildcard-mappings.html#WildcardMappings-Parametersinnamespaces. And of course w/o error page in web.xml. You can redirect to error page later from some action. – Aleksandr M Apr 21 '16 at 09:18
  • @AleksandrM actually that's what I did for now. dropped the .action/.do extensions so it will still load to the action. – Jiro Manio Apr 21 '16 at 09:21
  • @JiroManio This issue is solved then? You can post an answer and accept it. It might help future visitors with similar issues. – Aleksandr M Apr 21 '16 at 09:25
  • If I understand you correctly, you're basically asking to get the original request URI which has hit the ``, as in `${requestScope['javax.servlet.error.request_uri']}`? – BalusC Apr 21 '16 at 09:28
  • @AleksandrM I adjusted my code so it will fit my project, but the original question of "getting the original request URL/URI from the web.xml error_page" is not yet answered. I will try to find ways to make it work and post it ASAP. – Jiro Manio Apr 21 '16 at 09:44
  • But why? Why do you have to get url from error page? Don't define error page in web.xml at all. Create an action with named parameters. Check if it exist in db and act accordingly in the action. – Aleksandr M Apr 21 '16 at 09:47
  • @BalusC sorry how do I run that? in the jsp? i'm trying s:set and s:property value="${requestScope['javax.servlet.error.request_uri']}" and it says value does not support runtime expressions. I'm pretty sure I'm doing something wrong – Jiro Manio Apr 21 '16 at 09:49
  • @AleksandrM that was my initial "plan" that I wanted to know if it is feasible. Yes the named parameter worked. Hm, alright I'll create an answer for it – Jiro Manio Apr 21 '16 at 09:50
  • 1
    To verify, just put plain in error JSP page, outside any JSP tags. E.g. in a HTMl paragraph. It will simply be printed to response. If that works out, then you can reframe the question and advance in Struts context (I don't do Struts, I was just trying to guide you based on Servlet API which I do know). – BalusC Apr 21 '16 at 09:51

1 Answers1

2

Instead of using the error page in the web.xml, I changed it to named parameters.

<action name="/*" class="checkBlogUrl">
    <param name="blogSiteUrl">{1}</param>
    <result type="redirectAction">
        <param name="actionName">checkBlogUrl</param>
        <param name="blogSiteUrl">{1}</param> 
    </result>
</action>

As noted by Andrea Ligios, web.xml error page will drop the original request URI so the question was not feasible

EDIT:

putting ${requestScope['javax.servlet.error.request_uri']} in the JSP page will give you the URI, then maybe you can pass it to the action someway or another

Jiro Manio
  • 137
  • 10