2015-12-07 6 views
0

Это мой сервлет по умолчанию. Код:Почему мой 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 следующим образом и подтвердить структуру.

Web-контента
            --login.jsp
            --js/check.js

, что означает, что вам нужно создать папку Js и положить Файл js внутри папки должен работать.

Смежные вопросы