gpt4 book ai didi

javascript - 为什么我的 javascript 在 jsp 页面中不起作用?

转载 作者:行者123 更新时间:2023-11-28 10:50:26 25 4
gpt4 key购买 nike

这是我的默认 servlet。代码:

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

private static final long serialVersionUID = 1L;

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

}

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

}

login.jsp 位于 WebContent 中。

login.jsp代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
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>
<center>
<br>
<form action="login" method="post" onsubmit="return checkForm();">
<table cellspacing=0 cellpadding=2 align=center border=0 id="table" style="margin-left: 550px">
<tr>
<td align=right>userName:</td>
<td><input type="text" name="userName"
id="userName" /> </td><td><span id="userName_notice"></span></td>
</tr>
<tr>
<td align=right>userPass:</td>
<td><input type="password" name="userPass"
id="userPass" /></td><td> <span id="userPass_notice"></span></td>
</tr>
<tr>
<td colspan="2" align=right>
<input type='submit' value='sign in'>
</td>
</tr>
</table>
</form>
</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() {
document.getElementById("userName").focus();
};

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

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

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

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

return true;
}

<小时/>

但是当我访问login.jsp时,发现check.js 302。JavaScript不起作用。我应该怎么做才能解决这个问题?任何建议将不胜感激。

最佳答案

像这样找到你的js文件并确认结构。

网页内容
--login.jsp
--js/check.js

这意味着您需要创建 js 文件夹并将 js 文件放入该文件夹中才能工作。

关于javascript - 为什么我的 javascript 在 jsp 页面中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34131202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com