Reputation: 980
I'm using a system which includes Jersey (1.18.1) for REST web services + custom servlets to treat client requests and call web services. I'm also using FORM method authentication.
The problem is that after web service has been called, the servlet forwards then to index.jsp. In that JSP the html/javascript code is well displayed but the java code is not executed. I have to refresh the page (F5) to get the java code to be executed 9but then loose the request/response informations). What could be the cause for this behaviour...?
Here is what my web.xml looks like:
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:http="http://cxf.apache.org/transports/http/configuration"
xmlns:sec="http://cxf.apache.org/configuration/security"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<display-name>AdminModule</display-name>
<!-- JERSEY 1.18.1 -->
<servlet>
<servlet-name>JerseySpringServlet</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>my.package.restapi</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>MainController</servlet-name>
<servlet-class>my.package.controller.MainController</servlet-class>
</servlet>
<servlet>
<description>
</description>
<display-name>Logout</display-name>
<servlet-name>Logout</servlet-name>
<servlet-class>my.package.controller.Logout</servlet-class>
</servlet>
<servlet>
<description>
</description>
<display-name>DefaultAction</display-name>
<servlet-name>DefaultAction</servlet-name>
<servlet-class>my.package.controller.DefaultAction</servlet-class>
</servlet>
<servlet>
<description>
</description>
<display-name>CreateRole</display-name>
<servlet-name>CreateRole</servlet-name>
<servlet-class>my.package.controller.CreateRole</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>JerseySpringServlet</servlet-name>
<url-pattern>/restapi/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>MainController</servlet-name>
<url-pattern>/CMQAdmin</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Logout</servlet-name>
<url-pattern>/Logout</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DefaultAction</servlet-name>
<url-pattern>/DefaultAction</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>CreateRole</servlet-name>
<url-pattern>/CreateRole</url-pattern>
</servlet-mapping>
<security-constraint>
<web-resource-collection>
<web-resource-name>Admin security manager</web-resource-name>
<url-pattern>/index.jsp</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
<http-method>PUT</http-method>
<http-method>DELETE</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>role1</role-name>
<role-name>role2</role-name>
<role-name>role3</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<security-constraint>
<web-resource-collection>
<web-resource-name>Admin security manager</web-resource-name>
<url-pattern>/DefaultAction</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
<http-method>PUT</http-method>
<http-method>DELETE</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>role1</role-name>
<role-name>role2</role-name>
<role-name>role3</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<security-constraint>
<web-resource-collection>
<web-resource-name>Admin security manager</web-resource-name>
<url-pattern>/CMQAdmin</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
<http-method>PUT</http-method>
<http-method>DELETE</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>role1</role-name>
<role-name>role2</role-name>
<role-name>role3</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<!-- Define the Login Configuration for this Application -->
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/login.jsp</form-login-page>
<form-error-page>/error.jsp</form-error-page>
</form-login-config>
</login-config>
<!-- Security roles referenced by this web application -->
<security-role>
<role-name>role1</role-name>
</security-role>
<security-role>
<role-name>role2</role-name>
</security-role>
<security-role>
<role-name>role3</role-name>
</security-role>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>CMQAdmin</welcome-file>
</welcome-file-list>
</web-app>
The code in the jsp. No java code is executed at all when the page dispays after the servlet forwards :
<%@ page import="org.json.*"%>
<%@ page import="java.io.*,java.util.*" %>
<html>
<b>Cookies: ${cookie.username.value} </b>
<head>
<meta charset="utf-8">
<title>Admin</title>
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/jquery.mobile-1.4.2.css" />
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/style.css" />
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/component.css" />
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="https://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>
<script src="${pageContext.request.contextPath}/js/cbpFWTabs.js"></script>
<script type="text/javascript">
$(document).ready(function() {
<%
if(!request.isUserInRole("1") && !request.isUserInRole("2") && !request.isUserInRole("3")) {
%>
<p>some html code AAA</p>
<%
}
else if(request.isUserInRole("1"))
{
%>
<p>some html code BBB</p>
<%
}
else if(request.isUserInRole("2"))
{
%>
<p>some html code CCC</p>
<%
}
else if(request.isUserInRole("3"))
{
%>
<p>some html code DDD</p>
<%
}
%>
});
</script>
</head>
<body>
<div id="logo" class="logo"> </div>
<div id="utils" class="utils">
<div id="logout" class="logout">
<form action="${pageContext.request.contextPath}/CMQAdmin" method="POST">
<input type="hidden" name="action" value="logoutaction">
<input type="submit" value="Logout" />
</form>
</div>
</div>
<div id="mainDiv" class="main">
<div id="title" class="title">Admin Module</div>
<div id="tabs" class="tabs">
<nav>
<ul>
<%
if(request.isUserInRole("2") || request.isUserInRole("1")) {
%>
<li><a href="#section-3" id="section-3-tab" class="icon-patients"><span>a</span></a></li>
<li><a href="#section-1" id="section-1-tab" class="icon-cfgclinics"><span>b</span></a></li>
<li><a href="#section-2" id="section-2-tab" class="icon-users"><span>c</span></a></li>
<li><a href="#section-4" id="section-4-tab" class="icon-patients"><span>d</span></a></li>
<li><a href="#section-5" id="section-5-tab" class="icon-cfgclinics"><span>e</span></a></li>
<%
}
else if(request.isUserInRole("3")) {
%>
<li><a href="#section-5" id="section-5-tab" class="icon-cfgclinics"><span>b</span></a></li>
<li><a href="#section-3" id="section-3-tab" class="icon-patients"><span>a</span></a></li>
<li><a href="#section-1" id="section-1-tab" class="icon-cfgclinics"><span>c</span></a></li>
<li><a href="#section-2" id="section-2-tab" class="icon-users"><span>d</span></a></li>
<li><a href="#section-4" id="section-4-tab" class="icon-patients"><span>e</span></a></li>
<%
}
%>
</ul>
</nav>
<div id="menu" class="menu">
<%
if(request.isUserInRole("2")) {
%>
<section id="section-3">
<div id="getPatientInfos" class="button">
<a id="lnkGetPatientInfos"
class="ui-btn ui-icon-plus ui-btn-icon-left ui-corner-all ui-shadow">Get infos</a>
</div>
<div id="creditTokens" class="button">
<a id="lnkCreditTokens"
class="ui-btn ui-icon-plus ui-btn-icon-left ui-corner-all ui-shadow">Post info</a>
</div>
<div id="createPatient" class="button">
<a id="lnkCreatePatient"
class="ui-btn ui-icon-plus ui-btn-icon-left ui-corner-all ui-shadow">put info</a>
</div>
<div id="registerPatient" class="button">
<a id="lnkRegisterPatient"
class="ui-btn ui-icon-plus ui-btn-icon-left ui-corner-all ui-shadow">delete info</a>
</div>
</section>
<%
}
else if(request.isUserInRole("3")) {
%>
<section id="section-5">
<div id="IndiActivation" class="button">
<a id="lnkIndiActivation"
class="ui-btn ui-icon-plus ui-btn-icon-left ui-corner-all ui-shadow">get info</a>
</div>
</section>
<%
}
else if(request.isUserInRole("1")) {
%>
<section id="section-3">
...
</section>
<%
}
%>
</div>
<!-- /menu -->
</div>
<!-- /tabs -->
<div id="content" class="content">Click on any button on the
left to load content in here.</div>
<!-- /content -->
<div id="info" class="info"> </div>
</div>
<!-- /mainDiv -->
<script>
new CBPFWTabs(document.getElementById('tabs'));
</script>
</body>
</html>
The servlet that makes the call to a webservice (call is ok) and then forwards back to index.jsp:
public class CreateRole extends HttpServlet {
private static final long serialVersionUID = 1L;
@Autowired
private RoleFacade roleFacade;
/**
* Logger...
*/
private static final Logger logger = Logger.getLogger(CreateRole.class.getName());
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
logger.info("CreateRole::processRequest");
String createRole = "AdminModule/SaveOrUpdateRole";
logger.info("CreateRole::rolename+description: " + rolename + " + " + description);
// get user roles from web service
String baseURL = https://localhost/AdminModule/restapi;
Form form = new Form();
form.add("rolename", rolename);
form.add("description", description);
JerseyClient client = new JerseyClient(form, baseURL+createRole);
ClientResponse clientResponse = client.post_Form(form);
if(clientResponse.getStatus() == 200 || clientResponse.getStatus() == 201) {
logger.info("ROLE [" + clientResponse.getEntity(String.class) + "] has been successfully created");
// do whatever with response
client.close();
request.setAttribute("", request.getParameter("action"));
this.getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
return;
}
else {
request.setAttribute("error", true);
request.setAttribute("wrErrorMessage", "Could not create new role");
RequestDispatcher r = this.getServletContext().getRequestDispatcher("/index.jsp");
r.forward(request, response);
return;
}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
}
Upvotes: 0
Views: 1615
Reputation: 4158
I think you may be missing a closing } for the javascript bit just above /script
script type="text/javascript">
. . .
<p>some html code DDD</p>
<%
}
%>
/script>
BTW Scriptlets are deprecated. I tend to use the Java Standard Tag Library (JSTL) and beans.
http://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm
Upvotes: 1