2015-12-07 145 views
0

這是我的默認servlet。 代碼:爲什麼我的JavaScript不能在jsp頁面中工作?

@WebServlet(value="/") 
 
public class DefaultServlet extends HttpServlet { 
 

 
\t private static final long serialVersionUID = 1L; 
 

 
\t protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
 
\t \t doPost(request, response); 
 
\t \t 
 
\t } 
 

 
\t protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
 
\t \t String contextPath = request.getContextPath(); 
 
\t \t response.sendRedirect(contextPath+"/login.jsp"); 
 
\t } 
 

 
}

的login.jsp地處的WebContent。

login.jsp的代碼:

<%@ page language="java" contentType="text/html; charset=UTF-8" 
 
\t pageEncoding="UTF-8"%> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
 
<html> 
 
<head> 
 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
 
<script type="text/javascript" charset="utf-8" src="js/check.js"></script> 
 
<title>Login here</title> 
 
</head> 
 
<body> 
 
\t <center> 
 
\t \t <br> 
 
\t \t <form action="login" method="post" onsubmit="return checkForm();"> 
 
\t \t \t <table cellspacing=0 cellpadding=2 align=center border=0 id="table" style="margin-left: 550px"> 
 
\t \t \t \t <tr> 
 
\t \t \t \t \t <td align=right>userName:</td> 
 
\t \t \t \t \t <td><input type="text" name="userName" 
 
\t \t \t \t \t \t id="userName" /> </td><td><span id="userName_notice"></span></td> 
 
\t \t \t \t </tr> 
 
\t \t \t \t <tr> 
 
\t \t \t \t \t <td align=right>userPass:</td> 
 
\t \t \t \t \t <td><input type="password" name="userPass" 
 
\t \t \t \t \t \t id="userPass" /></td><td> <span id="userPass_notice"></span></td> 
 
\t \t \t \t </tr> 
 
\t \t \t \t <tr> 
 
\t \t \t \t \t <td colspan="2" align=right> 
 
\t \t \t \t \t <input type='submit' value='sign in'> 
 
\t \t \t \t \t </td> 
 
\t \t \t \t </tr> 
 
\t \t \t </table> 
 
\t \t </form> 
 
\t </center> 
 
</body> 
 
</html>
check.js代碼:

var username_empty = "<span style='COLOR:#ff0000'><font size='2px'> × name in null!</font></span>"; 
 
var password_empty = "<span style='COLOR:#ff0000'><font size='2px'> × pass is null!</font></span>"; 
 
var info_right = "<span style='COLOR:#006600'><font size='2px'> √ </font></span>"; 
 
var pass_error = "<span style='COLOR:#ff0000'><font size='2px'> × </font></span>"; 
 

 
window.onload = function() { 
 
\t document.getElementById("userName").focus(); 
 
}; 
 

 
/** 
 
* 
 
* 
 
* @param {} 
 
*   target 
 
* @param {} 
 
*   Infos 
 
*/ 
 
function showInfo(target, Infos) { 
 
\t document.getElementById(target).innerHTML = Infos; 
 
} 
 

 
/** 
 
* check form 
 
* 
 
* @return {Boolean} 
 
*/ 
 

 
function checkForm() { 
 
\t var userName = document.getElementById("userName").value; 
 
\t var userPass = document.getElementById('userPass').value; 
 
\t if (userName == null || userName == "") { 
 
\t \t showInfo("userName_notice", username_empty); 
 
\t \t document.getElementById("userName").focus(); 
 
\t \t return false; 
 
\t }else{ 
 
\t \t showInfo("userName_notice", info_right); 
 
\t } 
 

 
\t if (userPass == null || userPass == "") { 
 
\t \t showInfo("userPass_notice", password_empty); 
 
\t \t document.getElementById("userPass").focus(); 
 
\t \t return false; 
 
\t }else{ 
 
\t \t showInfo("userPass_notice", info_right); 
 
\t } 
 
\t 
 
\t return true; 
 
}


但當我訪問login.jsp時,找到了check.js 302.JavaScript不起作用。 我該怎麼做才能解決這個問題? 任何建議將不勝感激。

+0

「不起作用」?請告訴我們更多。 – marekful

+0

瀏覽器只是檢索登錄頁面而不是JS文件。 – BalusC

回答

-1

找到您的js文件,並確認結構。

網絡內容
            --login.jsp
            --js/check.js

,這意味着你需要創建JS文件夾,並把你的文件夾內的js文件應該工作。

相關問題