public class LoginPage extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String contextPath = request.getContextPath();
out.println("<form method='post' action='LoginCheck'>");
out.println("Username: <input type='text' name='username'>");
out.println("<br>");
out.println("Password: <input type='password' name='password'>");
out.println("<br>");
out.println("<input type='submit' value='login'>");
out.println("<br>");
out.println("</form>");
}
}
LoginCheck页面此页面用于检查用户的用户名和密码,检查用户名和密码是否正确后,重定向到欢迎页面,如果用户名和密码不正确,则重定向到错误页面,每个页面的代码如下:
public class LoginCheck extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
String username = request.getParameter("username");
String password = request.getParameter("password");
if(username.equals("abs") && password.equals("abs")){
response.sendRedirect("Welcome");
}else{
response.sendRedirect("Error");
}
}
}
如果用户名和密码正确,欢迎使用。
public class Welcome extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("<h3>Welcome user</h3>");
}
}
如果用户名或密码不正确,则出错。
public class ErrorPage extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("<h3>ERROR !!!</h3>");
out.println("<h3>Username or Password you entered is wrong.</h3>");
}
}
您获得HTTP 405的原因是HttpServletResponse.sendRedirect指定如下:
使用指定的重定向位置URL向客户端发送临时重定向响应。
因此,如果您执行response.sendredirect(“welcome”);
,则“告诉”客户端浏览器转到相对URL“welcome”。巧合的是,这个映射到您的welcome
-servlet(我想是这样的)。HTTP只支持GET重定向,因此GET请求由客户端浏览器发出,但welcome
-servlet只支持POST。
public class Welcome extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("<h3>Welcome user</h3>");
}
}
public class LoginCheck extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
String username = request.getParameter("username");
String password = request.getParameter("password");
if(username.equals("abs") && password.equals("abs")){
RequestDispatcher rd = request.getRequestDispatcher("Welcome");
rd.forward(request, response);
}else{
RequestDispatcher rd = request.getRequestDispatcher("Error");
rd.forward(request, response);
}
}
}
然后,我创建了注释代码。 结果 原因:java.lang.StringIndexOutOfBoundsException:长度=0;指数=-6 说真的,我和这个代码斗争了7小时30分钟。
我想在三月计划一个月时间表,而不是使用静态成员。你不需要解释那些评论。“日”和“月时间表”都是类。 在我执行这段代码之前,它有两个错误。一个是“三月[]日”这句话这是一个错误。另一个是'for(int i=0;i 我不明白这些句子错在哪里。语法错误是什么?
随机输入 类PlayingCard():def init(自我、卡值、套装): 类甲板(): deck1=Deck()打印(deck1.卡片) 72 73中的类型错误回溯(最近一次调用)--- 类型错误:init()缺少1个必需的位置参数:'suit'
下面是我遇到错误的程序: //导入扫描程序类导入java。util。扫描仪; 这些是我得到的错误:
{“error”:“invalid_scope”,“error_description”:“AADSTS70011:为输入参数”scope“提供的值无效。作用域 https://graph.microsoft.com/User.Read https://graph.microsoft.com/User.ReadWrite https://graph.microsoft.com/User.ReadB