html表單提交servlet路徑問題
阿新 • • 發佈:2019-01-10
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="./LoginServlet" method="post"> 使用者名稱:<input type="text" name="username"/></br> 密碼:<input type="password" name="password"/></br> <input type="submit" value="登陸"/> </form> </body> </html>
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class LoginServlet */ @WebServlet("/LoginServlet") public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * Default constructor. */ public LoginServlet() { // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String username = request.getParameter("username"); String password = request.getParameter("password"); if (username.equals("hwj") && password.equals("123")) { // 重定向到登陸成功介面 response.sendRedirect("/hwj2/welcome.html"); } else { response.sendRedirect("/hwj2/fail.html"); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
不懂action的路徑為什麼要寫個.??????