1

This is my default servlet. Code:

@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 locates in WebContent.

login.jsp code :

<%@ 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 code:

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;
}

But when I access the login.jsp,check.js 302 is found.JavaScript doesn't work. What should I do to solve this problem ? Any suggestion will be appreciated.

Alexis
  • 11
  • 3

1 Answers1

-1

locate your js file like this and confirm the structure.

Web-Content
            --login.jsp
            --js/check.js

that means you need to create js folder and put your js file inside the folder should work.

Ataur Rahman Munna
  • 3,887
  • 1
  • 23
  • 34