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.