0

I have set my url pattern in my servlet as "/register/*". I have also researched about getting only the pattern starting from the "/register" from the getReuestURI()).

I have used request.getRequestURI().substring(request.getContextPath().length()) to get "/register/family" but it seems that it also returns the script source included in my header.jsp.

Header.jsp

<head>
        <title>Health Center</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link href="css/style.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
        <script type="text/javascript" src="js/script.js"></script>
        <script type="text/javascript" src="js/cufon-yui.js"></script>
        <script type="text/javascript" src="js/arial.js"></script>
        <script type="text/javascript" src="js/cuf_run.js"></script>
</head>

GlassFish Server 4 stacktrace:

INFO:   /register/family
INFO:   /register/js/script.js
INFO:   /register/css/style.css
INFO:   /register/js/cufon-yui.js
INFO:   /register/js/jquery-1.3.2.min.js
INFO:   /register/js/arial.js
INFO:   /register/js/cuf_run.js
INFO:   /register/images/search.gif

How can I fix this so that I can only get the "/register/family" only? Any help would be appreciated. Thanks!

EDITED: 1

I used system.out.println to test what request value am I getting. I have also noticed that even if i use request.getServletPath() it outputs multiple:

INFO:   /register
INFO:   /register
INFO:   /register
INFO:   /register
INFO:   /register
INFO:   /register
INFO:   /register
INFO:   /register

I also noticed that my css layout would not be used as it is redirected to another page.

EDITED 2:

I wanted to get "/register/family" from the requestURI because this will handle the request and determine the next page. Here is my code flow

     protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {


       String  userPath = request.getRequestURI().substring(request.getContextPath().length());


        if (userPath.equals("/register/family")) {
            // perform code registration
            userPath = "/familyRegistration"; // this is the .jsp page
        }
        else if (userPath.equals("/register/patient")) {
           // perform patient registration

             userPath = "/patientRegistration"; // this is . the .jsp page
        }

             // use RequestDispatcher to forward request internally
            String url = "/WEB-INF/view" + userPath + ".jsp";

            try {
                request.getRequestDispatcher(url).forward(request, response);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
}

EDIT 3

Fixed the just like @Serg answered below. Also fixed the CSS not loading issue! I used <link href="${pageContext.request.contextPath}/css/style.css" rel="stylesheet" type="text/css" /> as BalusC Said in this similar post.

Community
  • 1
  • 1
MLDS
  • 83
  • 1
  • 2
  • 12
  • Please show your code. – Jens Jun 12 '14 at 06:39
  • the container always checks for a url pattern which is the most specific. so "/register/* is not specific and will match everything. if you want to be more specific say "/register/family" then this should be ur url pattern – vikeng21 Jun 12 '14 at 06:42
  • @vikeng21 I have used "/register/* " to be able to use "/register/family" , "/reigster/empliyee" ( to cater multiple registrations). Are you saying I should prefer specifying each and every url pattern "/register/family", "/register/employee" instead? – MLDS Jun 12 '14 at 06:48
  • but in your question you said you wanted to only get the "/register/family" .so i thought you wanted only specific folder. but since you want multiple folders "/register/* is correct.but under /register/ you have lot of things so how will the container resolve what you want. what is request uri that you type in the browser and which page you access. update your question with proper code and data details so that it is clear – vikeng21 Jun 12 '14 at 07:02
  • @vikeng21 Sorry for not making the question clear. I have updated my question. Basically, I wanted to get the "/register/family" and perform the operation based upon that request as you can see in my code above. – MLDS Jun 12 '14 at 07:17
  • @Jens I have supplied my sample code above. Hope you can help me out – MLDS Jun 12 '14 at 07:17

2 Answers2

2

If I correctly understand, your css and js files are relative to the servlet path (/register) which is uncommon. If they were directly at the root of your web application, you won't have this problem. You header should then be :

<head>
        <title>Health Center</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link href="/css/style.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="/js/jquery-1.3.2.min.js"></script>
        <script type="text/javascript" src="/js/script.js"></script>
        <script type="text/javascript" src="/js/cufon-yui.js"></script>
        <script type="text/javascript" src="/js/arial.js"></script>
        <script type="text/javascript" src="/js/cuf_run.js"></script>
</head>

If it is acceptable in your app, only first line INFO: /register/family would be intercepted by your servlet, because the others won't start with /register.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
2

You can get the last index of slash and make a substring of the uri, so you get the pas without the jsp name

String  userPath = request.getRequestURI().substring(0,request.getRequestURI().lastIndexOf("/"));

Hope this helps.

Jens
  • 67,715
  • 15
  • 98
  • 113