【发布时间】:2015-05-07 14:29:02
【问题描述】:
我是 java web 的初学者。我正在尝试解决这个问题,但它不起作用。请给我一个完整的解决方案。我需要添加或更改以从 WEB-INF 显示 html 页面(在本例中为 search.html)。
这是我的 servlet 代码和 login.js
@WebServlet("/dispatcher")
公共类 DispatcherServlet 扩展 HttpServlet {
private static final long serialVersionUID = 1L;
@SuppressWarnings("unchecked")
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("application/json");
String action = req.getParameter("action");
try{
switch(action){
case "login":
AuthenticationActionsHandler authentication = new AuthenticationActionsHandler();
sendResponse(resp, authentication.logIn(req) );
break;
case "registration":
authentication = new AuthenticationActionsHandler();
sendResponse(resp, authentication.registration(req) );
break;
case "getName":
if((Integer) req.getSession().getAttribute("Id") != null){
UserActionsHandler user = new UserActionsHandler();
sendResponse(resp, user.getName(req) );
}
break;
case "logOut":
authentication = new AuthenticationActionsHandler();
sendResponse(resp, authentication.logOut(req) );
break;
case "search":
ActionsHandler searchAction = new ActionsHandler();
sendResponse(resp, searchAction.search(req) );
break;
case "saveRequest":
ActionsHandler saveRequestAction = new ActionsHandler();
sendResponse(resp, saveRequestAction.saveRequest(req) );
break;
case "showRequestedBooks":
HttpSession session = req.getSession(true);
Integer userId = (Integer)session.getAttribute("Id");
if(userId == null){
JSONObject result = new JSONObject();
result.put("authentication", false);
sendResponse(resp, result);
} else{
ActionsHandler showRequestedBooks = new ActionsHandler();
sendResponse(resp, showRequestedBooks.showRequestedItems(req) );
}
break;
}
}catch(MissingParameterException e){
sendErrorResponse(resp, e.getMissingParams());
}
}
public void sendResponse(HttpServletResponse resp, JSONArray resultJson ){
PrintWriter out;
try {
out = resp.getWriter();
out.println(resultJson);
} catch (IOException e) {
e.printStackTrace();
}
}
$("#login").click(function(){
var hashPassword = hex_md5($("#password").val());
var requestData = {
username: $('#username').val(),
password: hashPassword,
action: "login",
};
$.ajax({
type : "POST",
url : "/Library/dispatcher",
data : requestData,
}).done(
function(responseData){
if(responseData.error){
console.log(responseData.error);
$('#unsuccess').show();
}
else{
if(responseData.success){
// window.location.href = "/Library/search.html";
window.location.href = "/WEB-INF/search.html";
}
else{
$('#unsuccess').show();
}
}
});
});
【问题讨论】:
-
你不应该把 html 文件放在 web-inf 中。您的 html 和 js 文件应位于您的 WebContent 文件夹或其子文件夹之一中。 web-inf 对客户端是隐藏的,只有服务器端可以看到。
-
WEB-INF 是 WebContent 的子文件夹 ;D
-
@YosefY 此文件夹可以隐藏,但取决于服务器配置。
-
WEB-INF 是 java web 服务器的内部文件夹。它默认是隐藏的,所以当我写“WebContent 文件夹或其子文件夹之一”时,我认为 WEB-INF 已排除... :)