这网页代码是我直接审查网页源代码后照抄的
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>登录页面</title></head>
- <body>
- <form action="user-login" method="get">
- <fieldset>
- <legend>用户登录</legend>
- <p>
- <label>用户名: <input type="text" name="username"/>
- </label>
- </p>
- <p>
- <label>密 码:<input type="password" name="password"/>
- </label>
- </p>
- <p>
- <label><input type="submit" value="登录"/>
- <input type="reset" value="取消"/>
- </label>
- </p>
- </fieldset>
- </form>
- </body>
- </html>
- package com.demo;
-
- import java.io.IOException;
- import java.io.PrintWriter;
-
- 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("/user-login")
- public class LoginServlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
-
- /**
- * @see HttpServlet#HttpServlet()
- */
- public LoginServlet() {
- super();
- // TODO Auto-generated constructor stub
- }
-
- /**
- * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
- */
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- String username=request.getParameter("username");
- String password = request.getParameter("password");
- response.setContentType("text/html;charset=UTF-8");
- PrintWriter out=response.getWriter();
- out.println("");
- out.println("\r\n"
- + "\r\n"
- + "
登录页面 "); - out.println("");
- if("admin".equals(username)&&"12345".equals(password)) {
- out.println("许嘉茵成为我妻子"+username);
- }else {
- out.println("李梓健是她老公");
- }
- out.println("");
- }
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-
- doPost(request,response);
- }
-
- }
只用这两个
我先关掉tomcat,,然后先点击,运行hello.html,输入密码进去,然后再运行Loginservlet.java,就跳转405了
增加方法后
保存代码,然后页面刷新