我正在使用HTML表单将数据发送到servlet,然后servlet检查提交的数据是否有效(针对某些规则)并使用新的请求属性重新加载页面以返回结果。我几乎可以保证这不是最好的方式,但我认为这不会导致问题。我认为问题在于表单重定向到localhost:8080 / Register而不是localhost:8080 / project-context / Register。有没有办法在HTML中指定路径?我可以用一些JSP表达式来做吗?我宁愿不对项目路径进行硬编码,因为它可能在将来发生变化。 HTML和相关的servlet代码如下:
HTML:
<body>
<% String errorMessage = (String) request.getAttribute("ErrorMessage"); %>
<form name="Register" action="/Register" method="POST">
<%= errorMessage %><br><br>
User name: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
Verify Password: <input type="password" name="vPassword"><br>
Email: <input type="text" name="email"><br>
Verify Email: <input type="text" name="vEmail"><br><br>
<input type="submit" value="Register">
</form>
</body>
的Servlet /爪哇:
@WebServlet("/Register")
public class UserRegistrationServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public UserRegistrationServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
HttpSession session = request.getSession(false);
if (session == null) {
request.getRequestDispatcher("/");
return;
}
request.getRequestDispatcher("/WEB-INF/register.jsp").forward(request, response);
}
catch(Throwable errorMessage) {
request.setAttribute("ErrorMessage", errorMessage.getMessage());
request.setAttribute("Error Cause", errorMessage.getCause());
request.setAttribute("ErrorLocation", this.getServletName());
request.setAttribute("ErrorStackTrace", errorMessage.getStackTrace());
request.getRequestDispatcher("/WEB-INF/errorDisplay.jsp").forward(request, response);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
String ErrorMessage = "";
//Check user name is supplied
if (request.getParameter("Username") == null) {
ErrorMessage = "You must enter a username!";
request.setAttribute("ErrorMessage", ErrorMessage);
request.getRequestDispatcher("/WEB-INF/register.jsp").forward(request, response);
return;
}
//Check user name for maximum length
if (request.getParameter("Username").length() > 16) {
ErrorMessage = "The username you entered was too long! Only 16 characters are allowed.";
request.setAttribute("ErrorMessage", ErrorMessage);
request.getRequestDispatcher("/WEB-INF/register.jsp").forward(request, response);
return;
}
//Check password is supplied
if (request.getParameter("Password") == null) {
ErrorMessage = "You must enter a password!";
request.setAttribute("ErrorMessage", ErrorMessage);
request.getRequestDispatcher("/WEB-INF/register.jsp").forward(request, response);
return;
}
//Check password for complexity
/*--------------------------------------------------------
(?=.*[0-9]) a digit must occur at least once
(?=.*[a-z]) a lower case letter must occur at least once
(?=.*[A-Z]) an upper case letter must occur at least once
(?=[\\S]+$) no whitespace allowed in the entire string
.{6,16} at least 6 to 16 characters
---------------------------------------------------------*/
Pattern passwordPattern = Pattern.compile("((?=.*[0-9])(?=.*[a-z]) (?=.*[A-Z])(?=[\\S]+$).{6,16})", Pattern.CASE_INSENSITIVE);
Matcher passwordMatcher = passwordPattern.matcher(request.getParameter("Password"));
if (passwordMatcher.find() == false) {
if (request.getAttribute("password") == request.getAttribute("vPassword")) {}
else {
ErrorMessage = "The passwords you entered do not match!";
request.setAttribute("ErrorMessage", ErrorMessage);
request.getRequestDispatcher("/WEB-INF/register.jsp").forward(request, response);
return;
}
ErrorMessage = "The password you entered does not abide by the strength rules!";
request.setAttribute("ErrorMessage", ErrorMessage);
request.getRequestDispatcher("/WEB-INF/register.jsp").forward(request, response);
return;
}
//Check email is supplied
if (request.getParameter("Email") == null) {
ErrorMessage = "You must enter an email!";
request.setAttribute("ErrorMessage", ErrorMessage);
request.getRequestDispatcher("/WEB-INF/register.jsp").forward(request, response);
return;
}
//Validate email - *@*
Pattern emailPattern = Pattern.compile(".+@.+\\.[a-z]+", Pattern.CASE_INSENSITIVE);
Matcher emailMatcher = emailPattern.matcher(request.getParameter("Email"));
if (emailMatcher.find() == false) {
if (request.getAttribute("email") == request.getAttribute("vEmail")) {}
else {
ErrorMessage = "The emails you entered did not match!";
request.setAttribute("ErrorMessage", ErrorMessage);
request.getRequestDispatcher("/WEB-INF/register.jsp").forward(request, response);
return;
}
ErrorMessage = "The email you entered is not valid!";
request.setAttribute("ErrorMessage", ErrorMessage);
request.getRequestDispatcher("/WEB-INF/register.jsp").forward(request, response);
return;
}
UserRegistrationBean user = new UserRegistrationBean();
user.setUsername(request.getParameter("Username"));
user.setPassword(request.getParameter("Password"));
user.setEmail(request.getParameter("Email"));
user = UserDAO.register(user);
if (user.getExists() == true) {
ErrorMessage = "The user name you entered has already been registered!";
request.setAttribute("ErrorMessage", ErrorMessage);
request.getRequestDispatcher("/WEB-INF/register.jsp").forward(request, response);
return;
}
}
catch(Throwable errorMessage) {
request.setAttribute("ErrorMessage", errorMessage.getMessage());
request.setAttribute("Error Cause", errorMessage.getCause());
request.setAttribute("ErrorLocation", this.getServletName());
request.setAttribute("ErrorStackTrace", errorMessage.getStackTrace());
request.getRequestDispatcher("/WEB-INF/errorDisplay.jsp").forward(request, response);
}
}
}
提前感谢您提供的任何帮助!
答案 0 :(得分:1)
您需要在行动前添加前缀context path。
<form name="Register" action="${pageContext.servletContext.contextPath}/Register" method="POST">
建议您在引用应用程序中的任何资源时使用contextPath。 例如:
<link rel="stylesheet" type="text/css" ref="${pageContext.servletContext.contextPath}/css/test.css"/>
<script type='text/javascript' src='${pageContext.servletContext.contxtPath}/js/test.js'></script>
<img src="${pageContext.servletContext.contextPath}/images/test.jpg" alt="alt text">
这里使用了contextPath来引用我的样式表,javascript和图像。
HttpServletRequest.getContextPath() $ {pageContext.request.contextPath}也可用于检索contextPath,但ServletContext.getContextPath()的原因指定为here。
答案 1 :(得分:0)
<form name="Register" action="/project-context/Register" method="POST">
在HTML表单中,您使用action
元素的form
属性来指定目标网址
如果要从变量传递URL,请将以下代码放在doGet
方法中:
request.setAttribute("actionAttr", variableWhichHoldsURL);
由您决定此变量的位置。然后把它放在你的servlet代码中:
<% String actionAttr = (String) request.getAttribute("actionAttr"); %>
<form name="Register" action="<%= actionAttr %>" method="POST">
顺便说一句。这正是您在代码中使用errorMessage
所做的事情。