我们如何访问HTML5中的Java Restful Web服务会话变量&;javascript
我正在开发混合移动应用程序。我已经用java开发了RESTful web服务,我需要在HTML页面(即客户端)中获得会话值。我们如何用javascript维护会话 这是我的restful Web服务我们如何访问HTML5中的Java Restful Web服务会话变量&;javascript,java,rest,session,Java,Rest,Session,我正在开发混合移动应用程序。我已经用java开发了RESTful web服务,我需要在HTML页面(即客户端)中获得会话值。我们如何用javascript维护会话 这是我的restful Web服务 @Context private HttpServletRequest request; @GET @Path("session") @Produces(MediaType.TEXT_HTML) @Consumes(MediaType.APPLICATION_FORM_URLENCODED) pu
@Context
private HttpServletRequest request;
@GET
@Path("session")
@Produces(MediaType.TEXT_HTML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String session(@QueryParam("lname") String name1) {
String response2 = null;
String name11 = "kheerthi";
try {
// Jdbc Connection
ResultSet rs = pst.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int cols = rsmd.getColumnCount();
while (rs.next()) {
if (rs.getString(1) != "null") {
request.getSession(true);
HttpSession session = request.getSession();
session.setAttribute("name", "value");
session.setAttribute("UserName", rs.getString(2));
String username = (String) session.getAttribute("UserName");
System.out.println(username);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return "fail";
}
这是我的前端代码
$(document).ready(function() {
$("#btn4").click(function() {
alert("Button Clicked");
$lname = $("#lname").val();
alert("function called1");
alert($lname);
$.ajax({
url: 'http://localhost:8080/samplerestful_demo/webresources/generic/session',
data: {lname: $lname},
type: 'GET',
crossDomain: true,
ContentType: 'html/text',
dataType: 'text',
cache: false
}).done(function(response) {
if (response == 'success') {
// var value = '@Request.RequestContext.HttpContext.Session["UserName"]';
window.location.replace("http://localhost:8383/HTML5Application1/listform.html");
}
else {
alert("else called");
window.location.replace("http://localhost:8383/HTML5Application1/login1.html");
}
}).fail(function(request, textStatus, errorThrown) {
alert(textStatus + " : " + errorThrown.toString());
});
});
});
如果有,这里抛出的具体错误是什么?发生了什么不应该发生的事情?比“我们如何在javascript中维护会话?”更具描述性@doveyg问题就像服务器端的RESTful web服务,我们将客户端的RESTful web服务称为前端技术(混合mobileapp)。这句话没有任何问题。对我来说似乎非常合理。probelm就像我们如何使用javascript来处理主会话一样。HttpSession session=request.getSession();session.setAttribute(“名称”、“值”);setAttribute(“用户名”,rs.getString(2));我们如何在javascript中获得这个会话值。@这里RESTfulWeb服务是java应用程序,HTML编码是另一个HTML应用程序。我们可以使用ajax调用restful URL,以及如何在HTML应用程序中维护会话。