[英]display html pages from WEB-INF in java
我是Java Web的入門者。 我正在嘗試解決此問題,但是它不起作用。 請給我一個完整的解決方案。 我需要添加或更改以從WEB-INF顯示(在本例中為search.html)制作html頁面的內容。
這是我的servlet代碼和login.js
@WebServlet( “/調度程序”)
公共類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();
}
}
});
});
相反,我們可以將JSP和HTML文件放置在與WEB-INF平行的單獨文件夾中... WEB-INF是專用文件夾..如果我沒記錯的話,還將Servlet放在WEB-INF \\ classs中,它們需要編譯完成的所有修改,容器自動識別JSp和HTML文件的修改
將擴展名更改為jsp,將其移動到WebContent並在開始時添加用戶登錄的驗證(您可以在HttpSession中輸入相應的內容)。 如果沒有將他重定向到登錄頁面...
根據您的評論,您想先將用戶重定向到登錄頁面。 為此,我們可以選擇該html文件作為web.xml(部署描述符)中的歡迎文件。
見下文
<welcome-file-list>
<welcome-file>login.html</welcome-file>
</welcome-file-list>
身份驗證正確之后,我們應該使用sendRedirect將它們允許進入search.html文件。
規范第70頁無法做到這一點。
WEB-INF節點不是應用程序的公共文檔樹的一部分。 容器不能將WEB-INF目錄中包含的文件直接提供給客戶端。
通過servlet-3.0,有可能在WEB-INF/classes/resources/
提供文件,但是我認為這是不好的做法。
抱歉
嘗試添加SearchServlet
@WebServlet("/search")
public class SearchServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException{
request.getRequestDispatcher("/WEB-INF/search.html").forward(request, response);
}
}
在您的DispatcherServlet中,如果登錄成功,則通過調用response.sendRedirect("/search")
將用戶重定向到搜索servlet。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.