在一个网络项目中,我正在创建一个学生注册计划。
我有一个 .jsp 文件供学生注册,以便添加/删除等。
我应该如何修改我的 javascript 代码以检查以确保用户名输入只允许字符串?
这是我的 .jsp 文件的一部分:
<%@page contentType="text/html" 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">
<title>New User Registration</title>
</head>
<body>
<form method="get" action="RegServlet">
<h1>Register!</h1>
Fill out information below:<p>
Username:
<input type="text" name="userName" value =""><p>
<input type="submit" value="Finish!"><p>
<script type="text/javascript">
var var1 = "userName";
if(var1 == (""))
document.write("<bSystem doesn't like an empty string.....Try Again!!!</b>");
else if(var1 == ("notmatch"))
document.write("<b>Inappropriate input!!!.....Try Again!!!</b>");
还有一件事情:
单击“完成!”时,我不断收到 HTTP 状态 404 错误。它应该将我引导到我的 RegServlet.java 文件中指示的 login.jsp 文件,因为 form method="get" action="RegServlet" 应该为我执行此操作。但它似乎告诉我它找不到“/Registrar/RegServlet”路径。我究竟做错了什么?
这是我的一段 RegServlet 代码:
public void doStuff(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InstantiationException, IllegalAccessException, SQLException {
//see if search parameter is present
String userName = request.getParameter("userName");
String passWord = request.getParameter("passWord");
String lastName = request.getParameter("lastName");
String firstName = request.getParameter("firstName");
String age = request.getParameter("age");
char sex = request.getParameter("sex").charAt(0);
String email = request.getParameter("email");
if(!userName.isEmpty()&&!passWord.isEmpty()&&!lastName.isEmpty()&&!firstName.isEmpty()&&!age.isEmpty()&& sex!='F' || sex!='M' &&!email.isEmpty()) {
try {
SaveToDB.createConnection();
toDB.insertNewUser(userName, passWord, lastName, firstName, age, sex, email);
RequestDispatcher view = request.getRequestDispatcher("login.jsp");
view.forward(request, response);
} catch(SQLException sqlEx) {
System.out.println("Error!");
}
} else {
RequestDispatcher view = request.getRequestDispatcher("index.jsp");
view.forward(request, response);
}
提前致谢!