2015-12-07 185 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文件应该工作。

相关问题